transcode: use a structured object for ffmpeg options

This commit is contained in:
Alex Gleason 2025-02-27 20:06:23 -06:00
parent 82f16e0cfe
commit 25d5db8db6
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,26 +1,19 @@
export async function transcodeVideoStream( export async function transcodeVideoStream(input: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>> {
inputStream: ReadableStream<Uint8Array>, const opts = {
): Promise<ReadableStream<Uint8Array>> { 'i': 'pipe:0', // Read input from stdin
'c:v': 'libx264', // Convert to H.264
'preset': 'veryfast', // Encoding speed
'loglevel': 'fatal', // Suppress logs
'crf': '23', // Compression level (lower = better quality)
'c:a': 'aac', // Convert to AAC audio
'b:a': '128k', // Audio bitrate
'movflags': 'frag_keyframe+empty_moov', // Ensures MP4 streaming compatibility
'f': 'mp4', // Force MP4 format
};
const command = new Deno.Command('ffmpeg', { const command = new Deno.Command('ffmpeg', {
args: [ args: [
'-i', ...Object.entries(opts).flatMap(([k, v]) => [`-${k}`, v]),
'pipe:0', // Read input from stdin
'-c:v',
'libx264', // Convert to H.264
'-preset',
'veryfast', // Encoding speed
'-loglevel',
'fatal', // Suppress logs
'-crf',
'23', // Compression level (lower = better quality)
'-c:a',
'aac', // Convert to AAC audio
'-b:a',
'128k', // Audio bitrate
'-movflags',
'frag_keyframe+empty_moov', // Ensures MP4 streaming compatibility
'-f',
'mp4', // Force MP4 format
'pipe:1', // Output to stdout 'pipe:1', // Output to stdout
], ],
stdin: 'piped', stdin: 'piped',
@ -38,7 +31,7 @@ export async function transcodeVideoStream(
// Pipe the input stream into FFmpeg stdin and ensure completion // Pipe the input stream into FFmpeg stdin and ensure completion
const writer = process.stdin.getWriter(); const writer = process.stdin.getWriter();
const reader = inputStream.getReader(); const reader = input.getReader();
async function pumpInput() { async function pumpInput() {
try { try {