Skip to content

Instantly share code, notes, and snippets.

@rick4470
Last active March 12, 2023 15:36
Show Gist options
  • Save rick4470/0e051cbceae6fd591fd3c02a8ab417cc to your computer and use it in GitHub Desktop.
Save rick4470/0e051cbceae6fd591fd3c02a8ab417cc to your computer and use it in GitHub Desktop.
How to Batch Process video conversions using FFMPEG with Node.js
const spawn = require('child_process').spawn;
const parent = process.argv[2];
let videos = [];
if(process.argv[2]){
// Parent Path
const start = parseInt(process.argv[3]);
const end = parseInt(process.argv[4]);
for (let i = start; i <= end; i++) {
videos.push(i);
}
videos.reverse();
processVideos();
}else{
// Parent Path is required
console.log('Parent Folder is required');
}
function resizeVideo(video, quality) {
const p = new Promise((resolve, reject) => {
const ffmpeg = spawn('ffmpeg', ['-i', `${parent}/${video}.mp4`, '-codec:v', 'libx264', '-profile:v', 'main', '-preset', 'slow', '-b:v', '400k', '-maxrate', '400k', '-bufsize', '800k', '-vf', `scale=-2:${quality}`, '-threads', '0', '-b:a', '128k', `${parent}/transcoded/${video}_${quality}.mp4`]);
ffmpeg.stderr.on('data', (data) => {
console.log(`${data}`);
});
ffmpeg.on('close', (code) => {
resolve();
});
});
return p;
}
function processVideos() {
let video = videos.pop();
if (video) {
resizeVideo(video, 720).then(() => {
// 720p video all done
resizeVideo(video, 480).then(() => {
// 480p video all done
resizeVideo(video, 360).then(() => {
// 360p video all done
console.log(`Completed Video Number - ${video}`);
processVideos();
});
});
});
}
}
@motioncircus
Copy link

Hi Rick,

I'm a video producer working with an amazing editing application, called AutoEdit3, written by Pietro Passarelli but there's a problem with batch processing video conversions and I'm trying to help him figure it out.

The application takes source video files, transcodes them to separate video proxy files with ffmpeg and an audio file, which is then uploaded for transcribing in a cloud service. The resulting transcription is downloaded and added to a JSON file for use in editing.

However
The selection of multiple files in the file browser results in multiple ffmpeg processes which reduces the amount possible to batch at one time but more importantly, the process often hangs indefinitely. Pietro suspects it may be because of a 'race condition' when two or more transcription files return at the same instant and are blocked from updating the JSON file.

I suggested trying a serial process instead of multiple simultaneous ffmpeg processes and Pietro has tried to implement this but without success. Here's a link to the testing branch that he tried to fix it in.

I reach out to you in the hope that you may have an insight into a workaround.

Cheers and have a great weekend
Nigel
Byron Bay
Australia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment