Node.js:获取mp4文件的视频和音频格式信息 [英] Node.js: get video and audio format information of an mp4 file

查看:1639
本文介绍了Node.js:获取mp4文件的视频和音频格式信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是node.js的新手。
我需要提取给定视频文件的音频和视频格式(编解码器)信息。
我知道如何在bash中做到这一点,感谢mplayer这样:

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我想知道是否有一个npm模块(库)可以让我获得相同的信息。或者,我可以启动上面的命令格式node.js和read / parse stdout(我认为有很多例子)。但是我宁愿使用node.js原生解决方案。

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...

模块是fluent-ffmpeg( npm install fluent-ffmpeg ),以下是获取信息的方法:

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);
});

或更具针对性的示例:

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);
});

输出:

Video codec: h264
Audio codec: aac

这篇关于Node.js:获取mp4文件的视频和音频格式信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆