Steven
Steven

Reputation: 331

Change video resolution with JS

I'm developing a video storage service for users and I need that large videos (v.g. 4K) can be compressed to 1080p before saving them. Is there a JS library (browser or Node) that helps with this task? Maybe a webservice?

I also accept language suggestions.

Upvotes: 2

Views: 5476

Answers (2)

smith david
smith david

Reputation: 1

.format("h264") √ .format("mp4") ×

or add .outputOptions(['-movflags isml+frag_keyframe']) .format("mp4")

Upvotes: -2

Mat Sz
Mat Sz

Reputation: 2552

When it comes to downscaling video, the most accessible option is ffmpeg.

There is a package that makes using ffmpeg in node.js easier: https://www.npmjs.com/package/fluent-ffmpeg

For example, downscaling a video to 1080p and 720p:

    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)
            .output(basename + '-1280x720.mp4')
            .videoCodec('libx264')  
            .size('1280x720')

            .output(basename + '-1920x1080.mp4')
            .videoCodec('libx264')
            .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();
    });

(source: https://gist.github.com/dkarchmer/635496ff9280011b3eef)

You don't need any node packages to run ffmpeg, you could make use of the child_process API in node.js.

The ffmpeg package has to be installed on the server that will be running your application.

Upvotes: 10

Related Questions