From 5750f2e57d88b751283171ed8fc0e08aa6995334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9andre=20Daumont?= <1338620+digiz3d@users.noreply.github.com> Date: Sat, 6 May 2023 19:42:05 +0200 Subject: [PATCH] feat: add rtmp output support (#3) --- src/ffmpeg.ts | 15 +++++++++++++++ src/index.ts | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/ffmpeg.ts diff --git a/src/ffmpeg.ts b/src/ffmpeg.ts new file mode 100644 index 0000000..d29088b --- /dev/null +++ b/src/ffmpeg.ts @@ -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", + ) + } +} diff --git a/src/index.ts b/src/index.ts index 97277d1..42dca58 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -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)