🎥 Make videos programmatically with React
handle if time requested is longer than the video
Jonny Burger0c1f87dbc03f
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.tsindex 7bd879292a..509566f461 100644--- a/packages/renderer/src/extract-frame-from-video.ts+++ b/packages/renderer/src/extract-frame-from-video.ts@@ -1,8 +1,56 @@import execa from 'execa';import {FfmpegExecutable} from 'remotion';+import {Readable} from 'stream';import {frameToFfmpegTimestamp} from './frame-to-ffmpeg-timestamp';-export const extractFrameFromVideo = ({+export function streamToString(stream: Readable) {+ const chunks: Buffer[] = [];+ return new Promise<string>((resolve, reject) => {+ stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));+ stream.on('error', (err) => reject(err));+ stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));+ });+}++export const getLastFrameOfVideo = async ({+ ffmpegExecutable,+ offset,+ src,+}: {+ ffmpegExecutable: FfmpegExecutable;+ offset: number;+ src: string;+}): Promise<Readable> => {+ if (offset > 100) {+ throw new Error(+ 'could not get last frame of ' ++ src ++ '. Tried to seek 100ms before the end of the video and no frame was found. The video container has a duration that is longer than it contains video.'+ );+ }++ const actualOffset = `-${offset + 10}ms`;+ const {stdout, stderr} = execa(ffmpegExecutable ?? 'ffmpeg', [+ '-sseof',+ actualOffset,+ '-i',+ src,+ '-frames:v',+ '1',+ '-f',+ 'image2pipe',+ '-',+ ]);++ const info = await streamToString(stderr as Readable);+ if (info.includes('Output file is empty')) {+ return getLastFrameOfVideo({ffmpegExecutable, offset: offset + 10, src});+ }++ return stdout as Readable;+};++export const extractFrameFromVideo = async ({time,src,ffmpegExecutable,@@ -10,9 +58,9 @@ export const extractFrameFromVideo = ({time: number;src: string;ffmpegExecutable: FfmpegExecutable;-}) => {+}): Promise<Readable | null> => {const ffmpegTimestamp = frameToFfmpegTimestamp(time);- const {stdout} = execa(ffmpegExecutable ?? 'ffmpeg', [+ const {stdout, stderr} = execa(ffmpegExecutable ?? 'ffmpeg', ['-ss',ffmpegTimestamp,'-i',@@ -24,5 +72,14 @@ export const extractFrameFromVideo = ({'-',]);- return stdout;+ const info = await streamToString(stderr as Readable);+ if (info.includes('Output file is empty')) {+ return getLastFrameOfVideo({+ ffmpegExecutable,+ offset: 0,+ src,+ });+ }++ return stdout as Readable;};diff --git a/packages/renderer/src/test/extract-frame-from-video.test.ts b/packages/renderer/src/test/extract-frame-from-video.test.tsnew file mode 100644index 0000000000..64220891b2--- /dev/null+++ b/packages/renderer/src/test/extract-frame-from-video.test.ts@@ -0,0 +1,38 @@+import path from 'path';+import {Readable} from 'stream';+import {+ extractFrameFromVideo,+ streamToString,+} from '../extract-frame-from-video';++const src = path.join(+ __dirname,+ '..',+ '..',+ '..',+ 'example',+ 'public',+ 'framer.mp4'+);++test('Should be able to extract a frame from a video', async () => {+ const stdout = await extractFrameFromVideo({+ ffmpegExecutable: null,+ src,+ time: 1,+ });++ const str = await streamToString(stdout as Readable);+ expect(str.length).toBe(10607);+});++test('Should get the last frame if out of range', async () => {+ const stdout = await extractFrameFromVideo({+ ffmpegExecutable: null,+ src,+ time: 100,+ });++ const str = await streamToString(stdout as Readable);+ expect(str.length).toBe(10805);+});