import path from 'path'; import fs from 'fs'; import { Plugin } from './plugin.js'; import { FFMpegUtils } from 'common-utils'; /** * The static video plugin aims to join all the audio segments of a transcript into a single audio file. Then use the static video in the first section of the transcript as a placeholder for the entire transcript. This is useful for cases where the user wants to use a single static video for the entire transcript. Basically transforming all the sections into a single section with a single static video and a single audio file. The static video is used as a placeholder for the entire transcript, and the audio is used to play the entire transcript. The static video is not modified in any way, it is simply used as a placeholder for the entire transcript. The audio is generated by concatenating all the audio segments of the transcript into a single audio file. The output of this plugin is a modified transcript with a single section containing the static video and the concatenated audio file. The bubbles across all sections are also moved to the single combined section by reading each section duration, bubble duration and addind appropriate offsets to the bubble start and end times. The output of this plugin is a modified transcript with a single section containing the static video and the concatenated audio file, and the bubbles across all sections are also moved to the single combined section by reading each section duration, bubble duration and adding appropriate offsets to the bubble start and end times. The bubbles are also sorted by their start time. Transitions are removed during this process. */ export class StaticVideo extends Plugin { constructor(name, options) { super(name, options); } async applyPrerender(originalManuscript, jobId) { const transcript = originalManuscript.transcript || []; const targetWidth = +this.options.width || +this.options.targetWidth || 1080; const targetHeight = +this.options.height || +this.options.targetHeight || 1920; const targetAspect = targetWidth / targetHeight; let fullPlaceholder = undefined let fullPlaceholderMeta = undefined for (let item of transcript) { let sectionidx = transcript.indexOf(item) if (!item.mediaAbsPaths || !item.mediaAbsPaths.length) continue; for (let mediaObj of item.mediaAbsPaths) { let mediaIdx = item.mediaAbsPaths.indexOf(mediaObj) try { let mediaPath = mediaObj.path; if (!mediaPath || !fs.existsSync(mediaPath)) { const flattenedPath = this.mediaPathFlatten(mediaPath); if (fs.existsSync(flattenedPath)) { mediaObj.path = flattenedPath; mediaPath = flattenedPath; this.log(`Using flattened media path: ${flattenedPath}`); } else { this.log(`Media path does not exist: ${mediaPath}. Trying at flattened path: ${flattenedPath} failed.`); continue; } } // The first media path is used as the placeholder for the entire transcript. This is useful for cases where the user wants to use a single static video for the entire transcript if (!fullPlaceholder) { fullPlaceholder = mediaPath fullPlaceholderMeta = await FFMpegUtils.getMediaMetadata(mediaPath); if (!fullPlaceholderMeta || !fullPlaceholderMeta.video || !fullPlaceholderMeta.video.width || !fullPlaceholderMeta.video.height) { this.log(`No video stream found for ${fullPlaceholder}`); continue; } } const meta = fullPlaceholderMeta const srcW = parseInt(meta.video.width); const srcH = parseInt(meta.video.height); const ext = path.extname(mediaPath); const base = path.basename(mediaPath, ext); const outPath = path.join(path.dirname(mediaPath), `static-${sectionidx}-${mediaIdx}${ext}`); mediaObj.path = outPath; } catch (err) { this.log(`Error cropping media ${mediaObj?.path}: ${err}`); } } } } }