Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rtmp output support #3

Merged
merged 1 commit into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function outputToFFmpegParams(output: string) {
if (output.match(/([\n\r\s])/)) {
throw new Error("The output path can't contain spaces")
}

if (output.endsWith(".mp4")) {
return `./recordings/${output}`
} else if (output.startsWith("rtmp://")) {
return `-pix_fmt yuv420p -f flv ${output}`
} else {
throw new Error(
"Invalid output string. Should be either xxxx.mp4 or rtmp://xxxx",
)
}
}
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import * as puppeteer from "puppeteer"
import { execSync } from "child_process"
import { outputToFFmpegParams } from "./ffmpeg"

const url = process.env.URL
const duration = process.env.DURATION || 10
const output = process.env.OUTPUT || "output.mp4"
const rate = process.env.RATE || 6000


async function main() {
if (!url) {
console.error("URL environment variable is required")
process.exit(1)
}
const ffmpegOutputParams = outputToFFmpegParams(output)

const browser = await puppeteer.launch({
executablePath: "/usr/bin/chromium-browser",
headless: false,
Expand Down Expand Up @@ -46,13 +52,17 @@ async function main() {
} catch (e) {}
`,
)

const ffmpegCmd =
`ffmpeg -y -hide_banner -async 1 -nostdin -s 1280x720 -r 30 -draw_mouse 0
-f x11grab -i $DISPLAY
-f pulse -ac 2 -i default
-c:v libx264 -preset ultrafast -minrate 6000 -maxrate 6000 -g 12000
-c:v libx264 -preset ultrafast -b:v ${rate}k -minrate ${rate}k -maxrate ${rate}k -g 30
-c:a aac -b:a 192k -ac 2 -ar 44100
-ss 00:00:05 -t ${duration} ./recordings/output.mp4`.replaceAll(/[\n\r\s]+/gm, " ")
-ss 00:00:05 -t ${duration} ${ffmpegOutputParams}`.replaceAll(
/[\n\r\s]+/gm,
" ",
)

execSync(ffmpegCmd)

Expand Down