使用 ffprobe 检查文件是仅音频还是视频
Using ffprobe to check if file is audio or video only
有没有 ffprobe 命令我可以 运行 查看我拥有的 mov 文件是纯音频还是也包含视频?我有各种mov文件,其中一些是音频配音,一些是完整视频。
一种快速的方法是检查输出中是否包含单词 'Video'。这是一个例子:
>>> 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
我对几个不同的文件进行了尝试,它似乎适用于我尝试过的文件,但我确信有更好的解决方案。
可以在JSON或XML中输出流信息:
ffprobe -show_streams -print_format json input.mov
您将获得一个包含 codec_type
属性的流数组,其值如 audio
、video
等
输出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 Documentation: Writers。
要以编程方式查明视频文件是否有音频,请使用 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 有一个“视频”流,所以这不是一个坏主意。对我来说,我使用搜索持续时间值...效果很好!
有没有 ffprobe 命令我可以 运行 查看我拥有的 mov 文件是纯音频还是也包含视频?我有各种mov文件,其中一些是音频配音,一些是完整视频。
一种快速的方法是检查输出中是否包含单词 'Video'。这是一个例子:
>>> 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
我对几个不同的文件进行了尝试,它似乎适用于我尝试过的文件,但我确信有更好的解决方案。
可以在JSON或XML中输出流信息:
ffprobe -show_streams -print_format json input.mov
您将获得一个包含 codec_type
属性的流数组,其值如 audio
、video
等
输出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 Documentation: Writers。
要以编程方式查明视频文件是否有音频,请使用 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 有一个“视频”流,所以这不是一个坏主意。对我来说,我使用搜索持续时间值...效果很好!