【问题标题】:Using ffprobe to check if file is audio or video only使用 ffprobe 检查文件是音频还是视频
【发布时间】:2015-11-23 13:00:21
【问题描述】:

我可以运行 ffprobe 命令来查看我拥有的 mov 文件是纯音频还是包含视频?我有各种 mov 文件,其中一些是音频配音,一些是完整的视频。

【问题讨论】:

    标签: video ffmpeg ffprobe


    【解决方案1】:

    输出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

    【讨论】:

      【解决方案2】:

      您可以以 JSON 或 XML 格式输出流信息:

      ffprobe -show_streams -print_format json input.mov

      您将获得一个带有 codec_type 属性的流数组,其值如 audiovideo 等。

      【讨论】:

        【解决方案3】:

        要以编程方式确定视频文件是否有音频,请使用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;
        }
        

        【讨论】:

          【解决方案4】:

          将 ffprobe 与 json 一起使用,如下所示:

          ffmpeg -v quiet -print_format json -show_format -show_streams {FILENAME}
          

          在流索引上搜索索引 [duration]。如果是数字 && 是 > 0,他们,我认为这是一个视频。

          只搜索“视频”这个词的问题是,JPG 有一个“视频”流,所以这不是一个坏主意。对我来说,我使用搜索持续时间值...效果很好!

          【讨论】:

            【解决方案5】:

            执行此操作的一种快速方法是检查输出中是否包含“视频”一词。这是一个例子:

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

            我对几个不同的文件进行了尝试,它似乎对我尝试过的文件有效,但我确信有更好的解决方案。

            【讨论】:

            • 远非理想,您的文件可能在文件名中包含video,例如。
            猜你喜欢
            • 1970-01-01
            • 2013-06-13
            • 2012-09-05
            • 1970-01-01
            • 2013-07-11
            • 2021-02-11
            • 1970-01-01
            • 1970-01-01
            • 2011-12-16
            相关资源
            最近更新 更多