Spaces:
Running
Running
Commit ·
2811019
1
Parent(s): 15268b5
WIP bubble
Browse files- server-plugins/static-video.js +72 -0
- utils/AvatarRender.js +11 -7
- utils/HtmlRender.js +11 -3
- utils/bubble/Bubble.js +28 -12
- utils/bubble/bubble-templates.js +1 -1
server-plugins/static-video.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import path from 'path';
|
| 2 |
+
import fs from 'fs';
|
| 3 |
+
import { Plugin } from './plugin.js';
|
| 4 |
+
import { FFMpegUtils } from 'common-utils';
|
| 5 |
+
|
| 6 |
+
/**
|
| 7 |
+
* 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.
|
| 8 |
+
*/
|
| 9 |
+
export class StaticVideo extends Plugin {
|
| 10 |
+
constructor(name, options) {
|
| 11 |
+
super(name, options);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
async applyPrerender(originalManuscript, jobId) {
|
| 16 |
+
const transcript = originalManuscript.transcript || [];
|
| 17 |
+
const targetWidth = +this.options.width || +this.options.targetWidth || 1080;
|
| 18 |
+
const targetHeight = +this.options.height || +this.options.targetHeight || 1920;
|
| 19 |
+
const targetAspect = targetWidth / targetHeight;
|
| 20 |
+
let fullPlaceholder = undefined
|
| 21 |
+
let fullPlaceholderMeta = undefined
|
| 22 |
+
|
| 23 |
+
for (let item of transcript) {
|
| 24 |
+
let sectionidx = transcript.indexOf(item)
|
| 25 |
+
if (!item.mediaAbsPaths || !item.mediaAbsPaths.length) continue;
|
| 26 |
+
for (let mediaObj of item.mediaAbsPaths) {
|
| 27 |
+
let mediaIdx = item.mediaAbsPaths.indexOf(mediaObj)
|
| 28 |
+
|
| 29 |
+
try {
|
| 30 |
+
let mediaPath = mediaObj.path;
|
| 31 |
+
|
| 32 |
+
if (!mediaPath || !fs.existsSync(mediaPath)) {
|
| 33 |
+
const flattenedPath = this.mediaPathFlatten(mediaPath);
|
| 34 |
+
if (fs.existsSync(flattenedPath)) {
|
| 35 |
+
mediaObj.path = flattenedPath;
|
| 36 |
+
mediaPath = flattenedPath;
|
| 37 |
+
this.log(`Using flattened media path: ${flattenedPath}`);
|
| 38 |
+
} else {
|
| 39 |
+
this.log(`Media path does not exist: ${mediaPath}. Trying at flattened path: ${flattenedPath} failed.`);
|
| 40 |
+
continue;
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
// 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
|
| 45 |
+
if (!fullPlaceholder) {
|
| 46 |
+
fullPlaceholder = mediaPath
|
| 47 |
+
fullPlaceholderMeta = await FFMpegUtils.getMediaMetadata(mediaPath);
|
| 48 |
+
if (!fullPlaceholderMeta || !fullPlaceholderMeta.video || !fullPlaceholderMeta.video.width || !fullPlaceholderMeta.video.height) {
|
| 49 |
+
this.log(`No video stream found for ${fullPlaceholder}`);
|
| 50 |
+
continue;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
const meta = fullPlaceholderMeta
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
const srcW = parseInt(meta.video.width);
|
| 58 |
+
const srcH = parseInt(meta.video.height);
|
| 59 |
+
|
| 60 |
+
const ext = path.extname(mediaPath);
|
| 61 |
+
const base = path.basename(mediaPath, ext);
|
| 62 |
+
const outPath = path.join(path.dirname(mediaPath), `static-${sectionidx}-${mediaIdx}${ext}`);
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
mediaObj.path = outPath;
|
| 66 |
+
} catch (err) {
|
| 67 |
+
this.log(`Error cropping media ${mediaObj?.path}: ${err}`);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
}
|
utils/AvatarRender.js
CHANGED
|
@@ -50,7 +50,7 @@ export class AvatarRenderer {
|
|
| 50 |
const transitionType = (typeof transitionTypeRaw === 'string' && transitionTypeRaw && transitionTypeRaw.toLowerCase) ? transitionTypeRaw.toLowerCase() : null;
|
| 51 |
const transitionDuration = (section.transition_duration_sec !== undefined && section.transition_duration_sec !== null) ? section.transition_duration_sec : (section.transitionDurationSec !== undefined ? section.transitionDurationSec : null);
|
| 52 |
const firstMedia = sectionVideos[0];
|
| 53 |
-
if (transitionType && firstMedia) {
|
| 54 |
// record transition to be logged during final concatenation
|
| 55 |
transitions.push({ type: transitionType, duration: transitionDuration, from: prevSectionLastMedia ? prevSectionLastMedia : 'start', to: firstMedia });
|
| 56 |
}
|
|
@@ -160,16 +160,20 @@ export class AvatarRenderer {
|
|
| 160 |
if (match.duration) duration = parseFloat(match.duration) || duration;
|
| 161 |
if (match.type && typeof match.type === 'string') transType = match.type.toLowerCase();
|
| 162 |
}
|
|
|
|
|
|
|
| 163 |
}
|
| 164 |
if (duration > 0) {
|
| 165 |
const prevPath = sectionCaptionedVideos[i];
|
| 166 |
const nextPath = sectionCaptionedVideos[i + 1];
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
}
|
| 174 |
}
|
| 175 |
}
|
|
|
|
| 50 |
const transitionType = (typeof transitionTypeRaw === 'string' && transitionTypeRaw && transitionTypeRaw.toLowerCase) ? transitionTypeRaw.toLowerCase() : null;
|
| 51 |
const transitionDuration = (section.transition_duration_sec !== undefined && section.transition_duration_sec !== null) ? section.transition_duration_sec : (section.transitionDurationSec !== undefined ? section.transitionDurationSec : null);
|
| 52 |
const firstMedia = sectionVideos[0];
|
| 53 |
+
if (transitionType && firstMedia && transitionType != 'none') {
|
| 54 |
// record transition to be logged during final concatenation
|
| 55 |
transitions.push({ type: transitionType, duration: transitionDuration, from: prevSectionLastMedia ? prevSectionLastMedia : 'start', to: firstMedia });
|
| 56 |
}
|
|
|
|
| 160 |
if (match.duration) duration = parseFloat(match.duration) || duration;
|
| 161 |
if (match.type && typeof match.type === 'string') transType = match.type.toLowerCase();
|
| 162 |
}
|
| 163 |
+
} else {
|
| 164 |
+
transType = undefined
|
| 165 |
}
|
| 166 |
if (duration > 0) {
|
| 167 |
const prevPath = sectionCaptionedVideos[i];
|
| 168 |
const nextPath = sectionCaptionedVideos[i + 1];
|
| 169 |
+
if (transType) {
|
| 170 |
+
const transFile = await this.generateTransitionClip(prevPath, nextPath, duration, transType, i, jobId, outDir, controller, onLog, resolution, fps);
|
| 171 |
+
if (transFile) {
|
| 172 |
+
tempFiles.push(transFile);
|
| 173 |
+
concatParts.push(transFile);
|
| 174 |
+
} else {
|
| 175 |
+
onLog && onLog(`Skipping transition for index ${i} due to generation error\n`);
|
| 176 |
+
}
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|
utils/HtmlRender.js
CHANGED
|
@@ -21,15 +21,23 @@ export class HtmlRender {
|
|
| 21 |
|
| 22 |
await this.init();
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if (html) {
|
| 25 |
-
await
|
| 26 |
} else if (htmlFile) {
|
| 27 |
-
await
|
| 28 |
} else {
|
| 29 |
throw new Error('Either html or htmlFile must be provided.');
|
| 30 |
}
|
| 31 |
|
| 32 |
-
await
|
| 33 |
}
|
| 34 |
|
| 35 |
async renderVideo(html, htmlFile, outFilePath, durationSec, options = {}) {
|
|
|
|
| 21 |
|
| 22 |
await this.init();
|
| 23 |
|
| 24 |
+
const vw = (options.viewport && options.viewport.width) || 1080;
|
| 25 |
+
const vh = (options.viewport && options.viewport.height) || 1920;
|
| 26 |
+
const context = await this.browser.newContext({
|
| 27 |
+
viewport: { width: vw, height: vh }
|
| 28 |
+
});
|
| 29 |
+
const page = await context.newPage();
|
| 30 |
+
|
| 31 |
+
|
| 32 |
if (html) {
|
| 33 |
+
await page.setContent(html, { waitUntil: 'networkidle' });
|
| 34 |
} else if (htmlFile) {
|
| 35 |
+
await page.goto(`file://${htmlFile}`, { waitUntil: 'networkidle' });
|
| 36 |
} else {
|
| 37 |
throw new Error('Either html or htmlFile must be provided.');
|
| 38 |
}
|
| 39 |
|
| 40 |
+
await page.screenshot({ path: outFilePath, ...options });
|
| 41 |
}
|
| 42 |
|
| 43 |
async renderVideo(html, htmlFile, outFilePath, durationSec, options = {}) {
|
utils/bubble/Bubble.js
CHANGED
|
@@ -328,12 +328,14 @@ class BubbleMaker {
|
|
| 328 |
|
| 329 |
try {
|
| 330 |
await htmlRenderer.init();
|
| 331 |
-
|
| 332 |
-
|
| 333 |
await htmlRenderer.page.setViewportSize({ width: vw, height: vh });
|
| 334 |
|
| 335 |
for (const b of htmlBubbles) {
|
| 336 |
let silentVideoName = path.basename(silentVideo)
|
|
|
|
|
|
|
| 337 |
const isVideo = typeof b?.bubbleHtml.durationSec === 'number' && b?.bubbleHtml.durationSec > 0;
|
| 338 |
const outPath = path.join('public', `bubble_${silentVideoName}_${htmlBubbles.indexOf(b)}.` + (isVideo ? 'mp4' : 'png'));
|
| 339 |
|
|
@@ -368,24 +370,34 @@ class BubbleMaker {
|
|
| 368 |
`;
|
| 369 |
}
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
if (isVideo) {
|
| 372 |
console.log(`Rendering HTML bubble to video ${outPath} for ${b?.bubbleHtml?.durationSec}s`);
|
| 373 |
-
await htmlRenderer.renderVideo(finalHtml, finalFile, outPath, b?.bubbleHtml?.durationSec || b.durationSec, { fps: manuscriptMeta.fps, viewport: { width:
|
| 374 |
} else {
|
| 375 |
console.log(`Rendering HTML bubble to image ${outPath}`);
|
| 376 |
-
await htmlRenderer.renderFrame(finalHtml, finalFile, outPath, { omitBackground: true });
|
| 377 |
}
|
| 378 |
|
| 379 |
// Add the rendered asset
|
| 380 |
b.mediaAbsPaths = [outPath];
|
| 381 |
|
| 382 |
// Ensure it covers full screen since it was rendered at video dimensions
|
| 383 |
-
if (!b.bubbleExtra)
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
}
|
| 390 |
} catch (e) {
|
| 391 |
console.log('Error in generating html media', e)
|
|
@@ -424,7 +436,9 @@ class BubbleMaker {
|
|
| 424 |
const padX = typeof extra.paddingX === 'number' ? extra.paddingX : 5;
|
| 425 |
const padY = typeof extra.paddingY === 'number' ? extra.paddingY : 5;
|
| 426 |
let ow = null, oh = null;
|
| 427 |
-
if (extra.size === 'half')
|
|
|
|
|
|
|
| 428 |
else if (extra.size !== 'full') ow = Math.round(vw * 0.30);
|
| 429 |
|
| 430 |
let overlayMeta;
|
|
@@ -533,7 +547,9 @@ class BubbleMaker {
|
|
| 533 |
const padX = typeof extra.paddingX === 'number' ? extra.paddingX : 5;
|
| 534 |
const padY = typeof extra.paddingY === 'number' ? extra.paddingY : 5;
|
| 535 |
let ow = null, oh = null;
|
| 536 |
-
if (extra.size === 'half')
|
|
|
|
|
|
|
| 537 |
else if (extra.size !== 'full') ow = Math.round(vw * 0.30);
|
| 538 |
|
| 539 |
let overlayMeta;
|
|
|
|
| 328 |
|
| 329 |
try {
|
| 330 |
await htmlRenderer.init();
|
| 331 |
+
let vw = meta.video.width;
|
| 332 |
+
let vh = meta.video.height;
|
| 333 |
await htmlRenderer.page.setViewportSize({ width: vw, height: vh });
|
| 334 |
|
| 335 |
for (const b of htmlBubbles) {
|
| 336 |
let silentVideoName = path.basename(silentVideo)
|
| 337 |
+
const bubbleExtra = b.bubbleExtra || {};
|
| 338 |
+
const bubbleTemplateName = b.templateName
|
| 339 |
const isVideo = typeof b?.bubbleHtml.durationSec === 'number' && b?.bubbleHtml.durationSec > 0;
|
| 340 |
const outPath = path.join('public', `bubble_${silentVideoName}_${htmlBubbles.indexOf(b)}.` + (isVideo ? 'mp4' : 'png'));
|
| 341 |
|
|
|
|
| 370 |
`;
|
| 371 |
}
|
| 372 |
|
| 373 |
+
|
| 374 |
+
let width = vw
|
| 375 |
+
let height = vh
|
| 376 |
+
if (bubbleTemplateName && b.bubbleExtra == 'half') {
|
| 377 |
+
width = vw
|
| 378 |
+
height = Math.round(width * 9 / 16)
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
if (isVideo) {
|
| 382 |
console.log(`Rendering HTML bubble to video ${outPath} for ${b?.bubbleHtml?.durationSec}s`);
|
| 383 |
+
await htmlRenderer.renderVideo(finalHtml, finalFile, outPath, b?.bubbleHtml?.durationSec || b.durationSec, { fps: manuscriptMeta.fps, viewport: { width: width, height: height } });
|
| 384 |
} else {
|
| 385 |
console.log(`Rendering HTML bubble to image ${outPath}`);
|
| 386 |
+
await htmlRenderer.renderFrame(finalHtml, finalFile, outPath, { omitBackground: true, viewport: { width: width, height: height } });
|
| 387 |
}
|
| 388 |
|
| 389 |
// Add the rendered asset
|
| 390 |
b.mediaAbsPaths = [outPath];
|
| 391 |
|
| 392 |
// Ensure it covers full screen since it was rendered at video dimensions
|
| 393 |
+
if (!b.bubbleExtra) {
|
| 394 |
+
b.bubbleExtra = {};
|
| 395 |
+
b.bubbleExtra.size = 'full';
|
| 396 |
+
b.bubbleExtra.positionX = 'left';
|
| 397 |
+
b.bubbleExtra.positionY = 'top';
|
| 398 |
+
b.bubbleExtra.paddingX = 0;
|
| 399 |
+
b.bubbleExtra.paddingY = 0;
|
| 400 |
+
}
|
| 401 |
}
|
| 402 |
} catch (e) {
|
| 403 |
console.log('Error in generating html media', e)
|
|
|
|
| 436 |
const padX = typeof extra.paddingX === 'number' ? extra.paddingX : 5;
|
| 437 |
const padY = typeof extra.paddingY === 'number' ? extra.paddingY : 5;
|
| 438 |
let ow = null, oh = null;
|
| 439 |
+
if (extra.size === 'half') {
|
| 440 |
+
oh = Math.round(vh * 0.45);
|
| 441 |
+
}
|
| 442 |
else if (extra.size !== 'full') ow = Math.round(vw * 0.30);
|
| 443 |
|
| 444 |
let overlayMeta;
|
|
|
|
| 547 |
const padX = typeof extra.paddingX === 'number' ? extra.paddingX : 5;
|
| 548 |
const padY = typeof extra.paddingY === 'number' ? extra.paddingY : 5;
|
| 549 |
let ow = null, oh = null;
|
| 550 |
+
if (extra.size === 'half') {
|
| 551 |
+
oh = Math.round(vh * 0.45);
|
| 552 |
+
}
|
| 553 |
else if (extra.size !== 'full') ow = Math.round(vw * 0.30);
|
| 554 |
|
| 555 |
let overlayMeta;
|
utils/bubble/bubble-templates.js
CHANGED
|
@@ -48,7 +48,7 @@ export const BaseBubbleTemplates = {
|
|
| 48 |
bubbleExtra: {
|
| 49 |
positionX: 'center',
|
| 50 |
positionY: 'top',
|
| 51 |
-
size: '
|
| 52 |
paddingX: 10, // number in percentage
|
| 53 |
paddingY: 10 // number in percentage
|
| 54 |
}
|
|
|
|
| 48 |
bubbleExtra: {
|
| 49 |
positionX: 'center',
|
| 50 |
positionY: 'top',
|
| 51 |
+
size: 'half', // | 'half' // useful for images and videos
|
| 52 |
paddingX: 10, // number in percentage
|
| 53 |
paddingY: 10 // number in percentage
|
| 54 |
}
|