damianociarla / node-ffmpeg

ffmpeg module for nodejs
MIT License
600 stars 140 forks source link

node-ffmpeg

FFmpeg module for Node. This library provides a set of functions and utilities to abstract commands-line usage of ffmpeg. To use this library requires that ffmpeg is already installed (including all necessary encoding libraries like libmp3lame or libx264)

You can install this module using npm:

npm install ffmpeg

Usage

To start using this library, you must include it in your project and then you can either use the callback function or through the promise library:

var ffmpeg = require('ffmpeg');

Use the callback function

    try {
        new ffmpeg('/path/to/your_movie.avi', function (err, video) {
            if (!err) {
                console.log('The video is ready to be processed');
            } else {
                console.log('Error: ' + err);
            }
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

Use the approach with the library promise

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {
            console.log('The video is ready to be processed');
        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

The video object

Each time you create a new instance, this library provides a new object to retrieve the information of the video, the ffmpeg configuration and all methods to make the necessary conversions:

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {
            // Video metadata
            console.log(video.metadata);
            // FFmpeg configuration
            console.log(video.info_configuration);
        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

Preset functions

The video object contains a set of functions that allow you to perform specific operations independent of the settings for the conversion. In all the functions you can use the approach with the callback function or with the promise object

video.fnExtractSoundToMP3 (destionationFileName, callback)

This function extracts the audio stream of a video into an mp3 file

Params:

Example:

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {
            // Callback mode
            video.fnExtractSoundToMP3('/path/to/your_audio_file.mp3', function (error, file) {
                if (!error)
                    console.log('Audio file: ' + file);
            });
        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

video.fnExtractFrameToJPG(destinationFolder, settings, callback)

This function takes care of extracting one or more frames from the video that is being developed. At the end of the operation will return an array containing the list of extracted images

Params:

Example:

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {
            // Callback mode
            video.fnExtractFrameToJPG('/path/to/save_your_frames', {
                frame_rate : 1,
                number : 5,
                file_name : 'my_frame_%t_%s'
            }, function (error, files) {
                if (!error)
                    console.log('Frames: ' + files);
            });
        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

video.fnAddWatermark(watermarkPath, newFilepath, settings, callback)

This function takes care of adding a watermark to the video that is being developed. You can specify the exact position in which position the image

Params:

Example:

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {
            // Callback mode
            video.fnAddWatermark('/path/to/retrieve/watermark_file.png', '/path/to/save/your_file_video.mp4', {
                position : 'SE'
            }, function (error, file) {
                if (!error)
                    console.log('New video file: ' + file);
            });
        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }

Custom settings

In addition to the possibility of using the preset, this library provides a variety of settings with which you can modify to your liking settings for converting video

Add custom options

If the ffmpeg parameters are not present in the list of available function you can add it manually through the following function

video.addCommand(command, argument)

Example:

    // In this example will be changed the output to avi format
    video.addCommand('-f', 'avi');

Save the file

After setting the desired parameters have to start the conversion process. To do this you must call the function 'save'. This method takes as input the final destination of the file and optionally a callback function. If the function callback is not specified it's possible use the promise object.

video.save(destionationFileName, callback)

Example:

    try {
        var process = new ffmpeg('/path/to/your_movie.avi');
        process.then(function (video) {

            video
            .setVideoSize('640x?', true, true, '#fff')
            .setAudioCodec('libfaac')
            .setAudioChannels(2)
            .save('/path/to/save/your_movie.avi', function (error, file) {
                if (!error)
                    console.log('Video file: ' + file);
            });

        }, function (err) {
            console.log('Error: ' + err);
        });
    } catch (e) {
        console.log(e.code);
        console.log(e.msg);
    }