是否可以运行 ffprobe 命令来查看我拥有的 mov 文件是纯音频还是包含视频?我有各种 mov 文件,其中一些是音频配音,其中一些是完整的视频。
5 回答
要输出codec_type
ffprobe -loglevel error -show_entries stream=codec_type -of default=nw=1 input.foo
示例结果:
codec_type=video
codec_type=audio
如果您有多个音频或视频流,输出将显示多个视频或音频条目。
与上面相同,但只输出值
ffprobe -loglevel error -show_entries stream=codec_type -of default=nw=1=nk=1 input.foo
或者:
ffprobe -loglevel error -show_entries stream=codec_type -of csv=p=0 input.foo
示例结果:
video
audio
包括流索引
ffprobe -loglevel error -show_entries stream=index,codec_type -of csv=p=0 input.foo
示例结果:
0,video
1,audio
在此示例中,视频是第一个流,音频是第二个流,这是常态,但并非总是如此。
如果没有音频则不输出
ffprobe -loglevel error -select_streams a -show_entries stream=codec_type -of csv=p=0 input.foo
带音频输入的示例结果:
audio
如果输入没有音频,那么将没有输出(空输出),这可能对脚本使用很有用。
JSON 输出示例
ffprobe -loglevel error -show_entries stream=codec_type -of json input.mkv
示例结果:
{
"programs": [
],
"streams": [
{
"codec_type": "video"
},
{
"codec_type": "audio"
}
]
}
其他输出格式
如果您想要不同的输出格式(ini、flat、compact、csv、xml),请参阅FFprobe 文档:Writers。
您可以以 JSON 或 XML 格式输出流信息:
ffprobe -show_streams -print_format json input.mov
您将获得一个流数组,其codec_type属性具有诸如audio等的值video。
要以编程方式找出视频文件是否有音频,avformat_open_input()请如下所示使用 - 如果audio_index大于或等于零,则视频文件有音频。
if (avformat_open_input(&pFormatCtx, filename, nullptr, nullptr) != 0) {
fprintf(stderr, "Couldn't open video file!\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
fprintf(stderr, "Couldn't find stream information!\n");
return -1;
}
av_dump_format(pFormatCtx, 0, videoState->filename, 0);
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && video_index < 0)
video_index = i;
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && audio_index < 0)
audio_index = i;
}
将 ffprobe 与 json 一起使用,如下所示:
ffmpeg -v quiet -print_format json -show_format -show_streams {FILENAME}
在流索引上搜索索引 [duration]。如果是数字 && 是 > 0,他们,我认为这是一个视频。
只搜索“视频”这个词的问题是,JPG 有一个“视频”流,所以这不是一个坏主意。对我来说,我使用搜索持续时间值......效果很好!
一种快速的方法是检查“视频”一词是否在输出中。这是一个例子:
>>> cmd = shlex.split('%s -i %s' % (FFPROBE, video_path))
>>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> output = p.communicate()[1]
>>> 'Video' in output
True
我对几个不同的文件进行了尝试,它似乎适用于我尝试过的文件,但我确信有更好的解决方案。