Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: get video and audio format information of an mp4 file

I'm new to node.js. I need to extract audio and video format (codec) information for a given video file. I know how to do that in bash thanks to mplayer like this:

$ mplayer -vo null -ao null -identify -frames 0 myvideo.mp4 2>/dev/null | grep FORMAT

ID_VIDEO_FORMAT=H264
ID_AUDIO_FORMAT=MP4A

I'm wondering if there's an npm module (library) that allows me to get same information. Alternatively I could launch the above command form node.js and read / parse stdout (I think there's plenty of examples around). However I'd rather use a node.js "native" solution.

like image 286
lviggiani Avatar asked Dec 08 '22 23:12

lviggiani


1 Answers

Ok, found it...

The module is fluent-ffmpeg (npm install fluent-ffmpeg), and here is how to get information:

var ffmpeg = require('fluent-ffmpeg');

ffmpeg.ffprobe('/path/to/my/video.mp4',function(err, metadata) {
  console.log(metadata);
});

or a more tailored example:

var ffmpeg = require('fluent-ffmpeg');

ffmpeg.ffprobe('/path/to/my/video.mp4',function(err, metadata) {
    var audioCodec = null;
    var videoCodec = null;
    metadata.streams.forEach(function(stream){
        if (stream.codec_type === "video")
            videoCodec = stream.codec_name;
        else if (stream.codec_type === "audio")
            audioCodec = stream.codec_name;
    });

    console.log("Video codec: %s\nAudio codec: %s", videoCodec, audioCodec);
});

Output:

Video codec: h264
Audio codec: aac
like image 92
lviggiani Avatar answered Jan 05 '23 01:01

lviggiani