Skip to content

Instantly share code, notes, and snippets.

@dkarchmer
Last active March 14, 2024 17:32
Show Gist options
  • Save dkarchmer/635496ff9280011b3eef to your computer and use it in GitHub Desktop.
Save dkarchmer/635496ff9280011b3eef to your computer and use it in GitHub Desktop.
Example of NodeJS script to resize videos (to 1080P and 720P) using fluent-ffmpeg
(function () {
var ffmpeg = require('fluent-ffmpeg');
function baseName(str) {
var base = new String(str).substring(str.lastIndexOf('/') + 1);
if(base.lastIndexOf(".") != -1) {
base = base.substring(0, base.lastIndexOf("."));
}
return base;
}
var args = process.argv.slice(2);
args.forEach(function (val, index, array) {
var filename = val;
var basename = baseName(filename);
console.log(index + ': Input File ... ' + filename);
ffmpeg(filename)
// Generate 720P video
.output(basename + '-1280x720.mp4')
.videoCodec('libx264')
.noAudio()
.size('1280x720')
// Generate 1080P video
.output(basename + '-1920x1080.mp4')
.videoCodec('libx264')
.noAudio()
.size('1920x1080')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('progress', function(progress) {
console.log('... frames: ' + progress.frames);
})
.on('end', function() {
console.log('Finished processing');
})
.run();
});
})();
Copy link

ghost commented Nov 21, 2019

Hello, could you give me an example of how to use this code.

@dkarchmer
Copy link
Author

@augustocesar84 This is very old code and I no longer work on anything close to this, but this is already an example, and from the code, I would guess you just call it with

node ffmpeg-resize.js myvideo.mp4

But you will need to setup node with a package.json that includes the fluent-ffmpeg package, but who knows how much they have changed since 2015. You should check the project: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg

@dkarchmer
Copy link
Author

dkarchmer commented Nov 23, 2019

@augustocesar84
Note also I did create a docker image at the time (https://github.com/dkarchmer/docker-fluent-ffmpeg), so if you use docker, it should just be

docker pull dkarchmervue/fluent-ffmpeg
docker run --rm -ti -v ${PWD}:/work dkarchmervue/fluent-ffmpeg node ffmpeg-resize.js myvideo.mp4

But as I said, that was a long time ago, so it may or may not work. You are on your own here if does not.

@shubhamkourav
Copy link

this code hasn't worked on the synchronization

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