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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With