Add ffmpeg module

This commit is contained in:
Alex Gleason 2025-02-27 21:34:05 -06:00
parent bd47ae5930
commit bb13a8dc71
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 51 additions and 0 deletions

View file

View file

@ -0,0 +1,20 @@
import { ffmpeg } from './ffmpeg.ts';
Deno.test('ffmpeg', async () => {
await using file = await Deno.open(new URL('./buckbunny.mp4', import.meta.url));
const output = ffmpeg(file.readable, {
'i': 'pipe:0',
'c:v': 'libx264',
'preset': 'veryfast',
'loglevel': 'fatal',
'crf': '23',
'c:a': 'aac',
'b:a': '128k',
'movflags': 'frag_keyframe+empty_moov',
'f': 'mp4',
});
await Deno.mkdir(new URL('./tmp', import.meta.url), { recursive: true });
await Deno.writeFile(new URL('./tmp/buckbunny-transcoded.mp4', import.meta.url), output);
});

View file

@ -0,0 +1,31 @@
export type FFmpegFlags = {
'i': string;
'c:v': string;
'preset': string;
'loglevel': string;
'crf': string;
'c:a': string;
'b:a': string;
'movflags': string;
'f': string;
[key: string]: string;
};
export function ffmpeg(input: BodyInit, flags: FFmpegFlags): ReadableStream<Uint8Array> {
const command = new Deno.Command('ffmpeg', {
args: [
...Object.entries(flags).flatMap(([k, v]) => [`-${k}`, v]),
'pipe:1', // Output to stdout
],
stdin: 'piped',
stdout: 'piped',
});
// Spawn the FFmpeg process
const child = command.spawn();
// Pipe the input stream into FFmpeg stdin and ensure completion
new Response(input).body!.pipeTo(child.stdin);
return child.stdout;
}