Skip to content

Commit

Permalink
merge into captions
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed Aug 15, 2022
1 parent 3f1b37f commit b34bbfa
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/renderer/src/assets/download-map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs, {mkdirSync} from 'fs';
import path from 'path';
import type {TAsset} from 'remotion';
import type {TAsset, TCaption} from 'remotion';
import {deleteDirectory} from '../delete-directory';
import {tmpDir} from '../tmp-dir';

Expand Down Expand Up @@ -64,6 +64,7 @@ export type DownloadMap = {

export type RenderAssetInfo = {
assets: TAsset[][];
captions: TCaption[][];
imageSequenceName: string;
firstFrameIndex: number;
downloadMap: DownloadMap;
Expand Down
1 change: 0 additions & 1 deletion packages/renderer/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ export const uncompressMediaAsset = (
export type Assets = MediaAsset[];

export interface DownloadableAsset {
isRemote: boolean;
src: string;
}
2 changes: 1 addition & 1 deletion packages/renderer/src/captions-to-ffmpeg-inputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TCaption} from 'remotion';
import type {TCaption} from 'remotion';

interface CaptionFfmpegInputs {
captionFilters: string[];
Expand Down
16 changes: 14 additions & 2 deletions packages/renderer/src/render-frames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import type {SmallTCompMetadata, TAsset} from 'remotion';
import type {SmallTCompMetadata, TAsset, TCaption} from 'remotion';
import {Internals} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
import {downloadAndMapAssetsToFileUrl} from './assets/download-and-map-assets-to-file';
Expand Down Expand Up @@ -235,6 +235,9 @@ const innerRenderFrames = ({
});

const assets: TAsset[][] = new Array(framesToRender.length).fill(undefined);
const captions: TCaption[][] = new Array(framesToRender.length).fill(
undefined
);
let stopped = false;
cancelSignal?.(() => {
stopped = true;
Expand Down Expand Up @@ -312,10 +315,19 @@ const innerRenderFrames = ({
frame,
page: freePage,
});
const collectedCaptions = await puppeteerEvaluateWithCatch<TAsset[]>({
pageFunction: () => {
return window.remotion_collectCaptions();
},
args: [],
frame,
page: freePage,
});
const compressedAssets = collectedAssets.map((asset) =>
compressAsset(assets.filter(truthy).flat(1), asset)
);
assets[index] = compressedAssets;
captions[index] = collectedCaptions;
compressedAssets.forEach((asset) => {
downloadAndMapAssetsToFileUrl({
asset,
Expand All @@ -332,14 +344,14 @@ const innerRenderFrames = ({
onFrameUpdate(framesRendered, frame);
cleanupPageError();
freePage.off('error', errorCallbackOnFrame);
return compressedAssets;
})
);

const happyPath = progress.then(() => {
const returnValue: RenderFramesOutput = {
assetsInfo: {
assets,
captions,
imageSequenceName: `element-%0${filePadLength}d.${imageFormat}`,
firstFrameIndex: framesToRender[0],
downloadMap,
Expand Down
8 changes: 8 additions & 0 deletions packages/renderer/src/stitch-frames-to-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-fi
import {markAllAssetsAsDownloaded} from './assets/download-and-map-assets-to-file';
import type {DownloadMap, RenderAssetInfo} from './assets/download-map';
import type {Assets} from './assets/types';
import {captionsToFfmpegInputs} from './captions-to-ffmpeg-inputs';
import type {Codec} from './codec';
import {DEFAULT_CODEC} from './codec';
import {codecSupportsMedia} from './codec-supports-media';
Expand Down Expand Up @@ -256,6 +257,11 @@ export const spawnFfmpeg = async (
})
: null;

const captions = captionsToFfmpegInputs({
captions: options.assetsInfo.captions,
assetsCount: 1,
});

if (mediaSupport.audio && !mediaSupport.video) {
if (!audioCodecName) {
throw new TypeError(
Expand Down Expand Up @@ -317,6 +323,7 @@ export const spawnFfmpeg = async (
['-i', options.assetsInfo.imageSequenceName],
]),
audio ? ['-i', audio] : null,
...(captions ? captions.captionInputs : []),
(options.numberOfGifLoops ?? null) === null
? null
: [
Expand Down Expand Up @@ -344,6 +351,7 @@ export const spawnFfmpeg = async (
audioCodecName ? ['-c:a', audioCodecName] : null,
// Set max bitrate up to 1024kbps, will choose lower if that's too much
audioCodecName ? ['-b:a', '512K'] : null,
captions.captionFilters,
// Ignore metadata that may come from remote media
['-map_metadata', '-1'],
[
Expand Down

0 comments on commit b34bbfa

Please sign in to comment.