transcode: simplify pipe

This commit is contained in:
Alex Gleason 2025-02-27 20:16:25 -06:00
parent 044930cc8d
commit 462c00a3e7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 5 additions and 30 deletions

View file

@ -2,7 +2,7 @@ import { transcodeVideo } from './transcode.ts';
Deno.test('transcodeVideo', async () => { Deno.test('transcodeVideo', async () => {
await using file = await Deno.open(new URL('./buckbunny.mp4', import.meta.url)); await using file = await Deno.open(new URL('./buckbunny.mp4', import.meta.url));
const output = await transcodeVideo(file.readable); const output = transcodeVideo(file.readable);
await Deno.writeFile(new URL('./buckbunny-transcoded.mp4', import.meta.url), output); await Deno.writeFile(new URL('./buckbunny-transcoded.mp4', import.meta.url), output);
}); });

View file

@ -1,4 +1,4 @@
export async function transcodeVideo(input: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>> { export function transcodeVideo(input: ReadableStream<Uint8Array>): ReadableStream<Uint8Array> {
const opts = { const opts = {
'i': 'pipe:0', // Read input from stdin 'i': 'pipe:0', // Read input from stdin
'c:v': 'libx264', // Convert to H.264 'c:v': 'libx264', // Convert to H.264
@ -18,38 +18,13 @@ export async function transcodeVideo(input: ReadableStream<Uint8Array>): Promise
], ],
stdin: 'piped', stdin: 'piped',
stdout: 'piped', stdout: 'piped',
stderr: 'piped',
}); });
// Spawn the FFmpeg process // Spawn the FFmpeg process
const process = command.spawn(); const child = command.spawn();
// Capture stderr for debugging
const stderrPromise = new Response(process.stderr).text().then((text) => {
if (text.trim()) console.error('FFmpeg stderr:', text);
});
// 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(); input.pipeTo(child.stdin);
const reader = input.getReader();
async function pumpInput() { return child.stdout;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
await writer.write(value);
}
} finally {
writer.close(); // Close stdin to signal FFmpeg that input is done
}
}
// Start pumping input asynchronously
pumpInput();
// Ensure stderr logs are captured
stderrPromise.catch(console.error);
return process.stdout;
} }