shiveshnavin commited on
Commit
4665111
·
1 Parent(s): b693ac1

Fucking finally ! split render working

Browse files
Files changed (1) hide show
  1. server-plugins/split-render.js +29 -51
server-plugins/split-render.js CHANGED
@@ -1,7 +1,7 @@
1
  import _ from 'lodash'
2
  import fs from 'fs'
3
  import { Plugin } from './plugin.js';
4
- import { exec, spawn } from 'child_process'
5
  import _path from 'path';
6
  import { PerformanceRecorder } from 'common-utils';
7
 
@@ -26,13 +26,13 @@ export class SplitRenderPlugin extends Plugin {
26
  const getVideoDuration = `ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${outFile}"`;
27
  exec(getVideoDuration, (error, stdout, stderr) => {
28
  if (error) {
29
- console.error(`Error getting overlay duration: ${error.message}`);
30
  reject(error);
31
  return;
32
  }
33
 
34
  const overlayDuration = parseFloat(stdout.trim());
35
- console.log(`Overlay duration: ${overlayDuration} seconds`);
36
 
37
  let inputFiles = [];
38
  let sceneIndex = 0;
@@ -52,6 +52,12 @@ export class SplitRenderPlugin extends Plugin {
52
  let publicFilePath = `public/${fileName}`;
53
  inputFiles.push(`-ss 0 -t ${clipDuration} -i "${publicFilePath}"`);
54
  }
 
 
 
 
 
 
55
  }
56
  sceneIndex++;
57
  });
@@ -59,7 +65,7 @@ export class SplitRenderPlugin extends Plugin {
59
  // Add the front overlay video (transparent webm) - this controls the final duration
60
  inputFiles.push(`-ss 0 -t ${overlayDuration} -i "${outFile}"`);
61
  const finalOutFile = `out/final_${jobId}.mp4`
62
-
63
  if (inputFiles.length > 1) {
64
  // Build the ffmpeg command
65
  const inputPart = inputFiles.join(' ');
@@ -67,60 +73,32 @@ export class SplitRenderPlugin extends Plugin {
67
  const backVideoIndex = 0; // First input is the back video
68
 
69
  // Create filter complex for overlay with transparency
70
- const filterComplex = `"[${frontVideoIndex}:v]colorkey=0x000000:0.08:0.02[fg];[${backVideoIndex}:v][fg]overlay=0:0:format=auto"`;
71
 
72
- // Remove -shortest so the output duration matches the overlay video (outFile)
73
- ffmpegCommand = `ffmpeg ${inputPart} -filter_complex ${filterComplex} -c:v libx264 -pix_fmt yuv420p ${finalOutFile}`;
74
- }
75
- else {
76
- resolve('No input files to process. Skipping split-render post process');
77
- return
78
- }
79
-
80
- //run ffmpeg command
81
- if (ffmpegCommand) {
82
- console.log('Running ffmpeg command:', ffmpegCommand);
83
-
84
- const ffmpegProcess = spawn('ffmpeg', ffmpegCommand.split(' ').slice(1), { stdio: 'pipe' });
85
 
86
- let stdoutData = '';
87
- let stderrData = '';
88
-
89
- // Stream stdout in real-time
90
- ffmpegProcess.stdout.on('data', (data) => {
91
- const output = data.toString();
92
- console.log('[FFmpeg stdout]:', output);
93
- stdoutData += output;
94
- });
95
-
96
- // Stream stderr in real-time (FFmpeg outputs progress info to stderr)
97
- ffmpegProcess.stderr.on('data', (data) => {
98
- const output = data.toString();
99
- console.log('[FFmpeg stderr]:', output);
100
- stderrData += output;
101
- });
102
-
103
- ffmpegProcess.on('close', (code) => {
104
- if (code === 0) {
105
- fs.renameSync(outFile, 'raw_' + outFile)
106
- fs.renameSync(finalOutFile, outFile)
107
- console.log('FFmpeg process completed successfully, took ', perf.elapsedString());
108
- resolve(stdoutData || 'FFmpeg completed successfully');
109
- } else {
110
- const fullErrorLog = `FFmpeg process exited with code ${code}\n\nSTDOUT:\n${stdoutData}\n\nSTDERR:\n${stderrData}`;
111
- console.error('FFmpeg failed:', fullErrorLog);
112
  reject(new Error(fullErrorLog));
 
113
  }
114
- });
115
-
116
- ffmpegProcess.on('error', (error) => {
117
- const fullErrorLog = `FFmpeg spawn error: ${error.message}\n\nSTDOUT:\n${stdoutData}\n\nSTDERR:\n${stderrData}`;
118
- console.error('FFmpeg spawn error:', fullErrorLog);
119
- reject(new Error(fullErrorLog));
120
  });
121
  } else {
122
- resolve('No ffmpeg command generated');
 
123
  }
 
 
124
  });
125
  } else {
126
  resolve('No output file found');
 
1
  import _ from 'lodash'
2
  import fs from 'fs'
3
  import { Plugin } from './plugin.js';
4
+ import { exec } from 'child_process'
5
  import _path from 'path';
6
  import { PerformanceRecorder } from 'common-utils';
7
 
 
26
  const getVideoDuration = `ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${outFile}"`;
27
  exec(getVideoDuration, (error, stdout, stderr) => {
28
  if (error) {
29
+ console.error(`[SplitRenderPlugin] Error getting overlay duration: ${error.message}`);
30
  reject(error);
31
  return;
32
  }
33
 
34
  const overlayDuration = parseFloat(stdout.trim());
35
+ console.log(`[SplitRenderPlugin] Overlay duration: ${overlayDuration} seconds`);
36
 
37
  let inputFiles = [];
38
  let sceneIndex = 0;
 
52
  let publicFilePath = `public/${fileName}`;
53
  inputFiles.push(`-ss 0 -t ${clipDuration} -i "${publicFilePath}"`);
54
  }
55
+ else if (type === 'image') {
56
+ let fileName = _path.basename(path);
57
+ let publicFilePath = `public/${fileName}`;
58
+ // loop
59
+ inputFiles.push(`-loop 1 -ss 0 -t ${Math.min(durationInSeconds, overlayDuration)} -i "${publicFilePath}"`);
60
+ }
61
  }
62
  sceneIndex++;
63
  });
 
65
  // Add the front overlay video (transparent webm) - this controls the final duration
66
  inputFiles.push(`-ss 0 -t ${overlayDuration} -i "${outFile}"`);
67
  const finalOutFile = `out/final_${jobId}.mp4`
68
+ let ffmpegProcess
69
  if (inputFiles.length > 1) {
70
  // Build the ffmpeg command
71
  const inputPart = inputFiles.join(' ');
 
73
  const backVideoIndex = 0; // First input is the back video
74
 
75
  // Create filter complex for overlay with transparency
76
+ const filterComplex = `[${frontVideoIndex}:v]colorkey=0x000000:0.08:0.02[fg];[${backVideoIndex}:v][fg]overlay=0:0:format=auto`;
77
 
78
+ // Build single ffmpeg command string
79
+ const ffmpegCommand = `ffmpeg ${inputPart} -filter_complex "${filterComplex}" -c:v libx264 -pix_fmt yuv420p -y ${finalOutFile}`;
80
+ console.log('[SplitRenderPlugin] Running ffmpeg command:', ffmpegCommand);
 
 
 
 
 
 
 
 
 
 
81
 
82
+ exec(ffmpegCommand, (error, stdout, stderr) => {
83
+ if (error) {
84
+ const fullErrorLog = `FFmpeg process failed: ${error.message}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}`;
85
+ console.error('[SplitRenderPlugin] FFmpeg failed:', fullErrorLog);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  reject(new Error(fullErrorLog));
87
+ return;
88
  }
89
+ console.log('[SplitRenderPlugin] FFmpeg process completed successfully, took ', perf.elapsedString());
90
+ console.log('[SplitRenderPlugin] [FFmpeg stdout]:', stdout);
91
+ if (stderr) {
92
+ console.log('[SplitRenderPlugin] [FFmpeg stderr]:', stderr);
93
+ }
94
+ resolve(stdout || 'FFmpeg completed successfully');
95
  });
96
  } else {
97
+ resolve('No input files to process. Skipping split-render post process');
98
+ return;
99
  }
100
+
101
+
102
  });
103
  } else {
104
  resolve('No output file found');