Spaces:
Running
Running
Commit ·
4665111
1
Parent(s): b693ac1
Fucking finally ! split render working
Browse files123456718 unmodified lines2627282930313233343536373813 unmodified lines5253545556571 unmodified line596061626364651 unmodified line676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import _ from 'lodash'import fs from 'fs'import { Plugin } from './plugin.js';import { exec, spawn } from 'child_process'import _path from 'path';import { PerformanceRecorder } from 'common-utils';18 unmodified linesconst getVideoDuration = `ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${outFile}"`;exec(getVideoDuration, (error, stdout, stderr) => {if (error) {console.error(`Error getting overlay duration: ${error.message}`);reject(error);return;}const overlayDuration = parseFloat(stdout.trim());console.log(`Overlay duration: ${overlayDuration} seconds`);let inputFiles = [];let sceneIndex = 0;13 unmodified lineslet publicFilePath = `public/${fileName}`;inputFiles.push(`-ss 0 -t ${clipDuration} -i "${publicFilePath}"`);}}sceneIndex++;});1 unmodified line// Add the front overlay video (transparent webm) - this controls the final durationinputFiles.push(`-ss 0 -t ${overlayDuration} -i "${outFile}"`);const finalOutFile = `out/final_${jobId}.mp4`if (inputFiles.length > 1) {// Build the ffmpeg commandconst inputPart = inputFiles.join(' ');1 unmodified lineconst backVideoIndex = 0; // First input is the back video// Create filter complex for overlay with transparencyconst filterComplex = `"[${frontVideoIndex}:v]colorkey=0x000000:0.08:0.02[fg];[${backVideoIndex}:v][fg]overlay=0:0:format=auto"`;// Remove -shortest so the output duration matches the overlay video (outFile)ffmpegCommand = `ffmpeg ${inputPart} -filter_complex ${filterComplex} -c:v libx264 -pix_fmt yuv420p ${finalOutFile}`;}else {resolve('No input files to process. Skipping split-render post process');return}//run ffmpeg commandif (ffmpegCommand) {console.log('Running ffmpeg command:', ffmpegCommand);const ffmpegProcess = spawn('ffmpeg', ffmpegCommand.split(' ').slice(1), { stdio: 'pipe' });let stdoutData = '';let stderrData = '';// Stream stdout in real-timeffmpegProcess.stdout.on('data', (data) => {const output = data.toString();console.log('[FFmpeg stdout]:', output);stdoutData += output;});// Stream stderr in real-time (FFmpeg outputs progress info to stderr)ffmpegProcess.stderr.on('data', (data) => {const output = data.toString();console.log('[FFmpeg stderr]:', output);stderrData += output;});ffmpegProcess.on('close', (code) => {if (code === 0) {fs.renameSync(outFile, 'raw_' + outFile)fs.renameSync(finalOutFile, outFile)console.log('FFmpeg process completed successfully, took ', perf.elapsedString());resolve(stdoutData || 'FFmpeg completed successfully');} else {const fullErrorLog = `FFmpeg process exited with code ${code}\n\nSTDOUT:\n${stdoutData}\n\nSTDERR:\n${stderrData}`;console.error('FFmpeg failed:', fullErrorLog);reject(new Error(fullErrorLog));}});ffmpegProcess.on('error', (error) => {const fullErrorLog = `FFmpeg spawn error: ${error.message}\n\nSTDOUT:\n${stdoutData}\n\nSTDERR:\n${stderrData}`;console.error('FFmpeg spawn error:', fullErrorLog);reject(new Error(fullErrorLog));});} else {resolve('No ffmpeg command generated');}});} else {resolve('No output file found');123456718 unmodified lines2627282930313233343536373813 unmodified lines5253545556575859606162631 unmodified line656667686970711 unmodified line737475767778798081828384858687888990919293949596979899100101102103104import _ from 'lodash'import fs from 'fs'import { Plugin } from './plugin.js';import { exec } from 'child_process'import _path from 'path';import { PerformanceRecorder } from 'common-utils';18 unmodified linesconst getVideoDuration = `ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${outFile}"`;exec(getVideoDuration, (error, stdout, stderr) => {if (error) {console.error(`[SplitRenderPlugin] Error getting overlay duration: ${error.message}`);reject(error);return;}const overlayDuration = parseFloat(stdout.trim());console.log(`[SplitRenderPlugin] Overlay duration: ${overlayDuration} seconds`);let inputFiles = [];let sceneIndex = 0;13 unmodified lineslet publicFilePath = `public/${fileName}`;inputFiles.push(`-ss 0 -t ${clipDuration} -i "${publicFilePath}"`);}else if (type === 'image') {let fileName = _path.basename(path);let publicFilePath = `public/${fileName}`;// loopinputFiles.push(`-loop 1 -ss 0 -t ${Math.min(durationInSeconds, overlayDuration)} -i "${publicFilePath}"`);}}sceneIndex++;});1 unmodified line// Add the front overlay video (transparent webm) - this controls the final durationinputFiles.push(`-ss 0 -t ${overlayDuration} -i "${outFile}"`);const finalOutFile = `out/final_${jobId}.mp4`let ffmpegProcessif (inputFiles.length > 1) {// Build the ffmpeg commandconst inputPart = inputFiles.join(' ');1 unmodified lineconst backVideoIndex = 0; // First input is the back video// Create filter complex for overlay with transparencyconst filterComplex = `[${frontVideoIndex}:v]colorkey=0x000000:0.08:0.02[fg];[${backVideoIndex}:v][fg]overlay=0:0:format=auto`;// Build single ffmpeg command stringconst ffmpegCommand = `ffmpeg ${inputPart} -filter_complex "${filterComplex}" -c:v libx264 -pix_fmt yuv420p -y ${finalOutFile}`;console.log('[SplitRenderPlugin] Running ffmpeg command:', ffmpegCommand);exec(ffmpegCommand, (error, stdout, stderr) => {if (error) {const fullErrorLog = `FFmpeg process failed: ${error.message}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}`;console.error('[SplitRenderPlugin] FFmpeg failed:', fullErrorLog);reject(new Error(fullErrorLog));return;}console.log('[SplitRenderPlugin] FFmpeg process completed successfully, took ', perf.elapsedString());console.log('[SplitRenderPlugin] [FFmpeg stdout]:', stdout);if (stderr) {console.log('[SplitRenderPlugin] [FFmpeg stderr]:', stderr);}resolve(stdout || 'FFmpeg completed successfully');});} else {resolve('No input files to process. Skipping split-render post process');return;}});} else {resolve('No output file found');
server-plugins/split-render.js+29-51