如何使用avprobe/ffprobe检测音频采样率?

14

我正在使用通过Homebrew安装的libav 9.6。

$ avprobe -version
avprobe version 9.6, Copyright (c) 2007-2013 the Libav developers
  built on Jun  8 2013 02:44:19 with Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
avprobe 9.6
libavutil     52.  3. 0 / 52.  3. 0
libavcodec    54. 35. 0 / 54. 35. 0
libavformat   54. 20. 3 / 54. 20. 3
libavdevice   53.  2. 0 / 53.  2. 0
libavfilter    3.  3. 0 /  3.  3. 0
libavresample  1.  0. 1 /  1.  0. 1
libswscale     2.  1. 1 /  2.  1. 1

尽管取样率在命令行输出的标准输出中显示,但-show_format选项根本不会在音频文件中呈现取样率信息。

这是BASH终端输出:

$ avprobe  -v verbose -show_format -of json  sample.gsm
avprobe version 9.6, Copyright (c) 2007-2013 the Libav developers
  built on Jun  8 2013 02:44:19 with Apple LLVM version 4.2 (clang-425.0.24)
(based on LLVM 3.2svn)
  configuration: --prefix=/usr/local/Cellar/libav/9.6 --enable-shared
--enable-pthreads --enable-gpl --enable-version3 --enable-nonfree
--enable-hardcoded-tables --enable-avresample --enable-vda --enable-gnutls
--enable-runtime-cpudetect --disable-indev=jack --cc=cc --host-cflags=
--host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame
--enable-libxvid --enable-avplay
  libavutil     52.  3. 0 / 52.  3. 0
  libavcodec    54. 35. 0 / 54. 35. 0
  libavformat   54. 20. 3 / 54. 20. 3
  libavdevice   53.  2. 0 / 53.  2. 0
  libavfilter    3.  3. 0 /  3.  3. 0
  libavresample  1.  0. 1 /  1.  0. 1
  libswscale     2.  1. 1 /  2.  1. 1
[gsm @ 0x7f8012806600] Estimating duration from bitrate, this may be inaccurate
Input #0, gsm, from 'sample.gsm':
  Duration: 00:03:52.32, start: 0.000000, bitrate: 13 kb/s
    Stream #0.0: Audio: gsm, 8000 Hz, mono, s16, 13 kb/s
{  "format" : {
    "filename" : "sample.gsm",
    "nb_streams" : 1,
    "format_name" : "gsm",
    "format_long_name" : "raw GSM",
    "start_time" : "0.000000",
    "duration" : "232.320000",
    "size" : "383328.000000",
    "bit_rate" : "13200.000000"
  }}

而且这是Python代码示例:

>>> filename = 'sample.gsm'
>>> result = subprocess.check_output(['avprobe', '-show_format', '-of',
'json', filename])
avprobe version 9.6, Copyright (c) 2007-2013 the Libav developers
  built on Jun  8 2013 02:44:19 with Apple LLVM version 4.2
(clang-425.0.24) (based on LLVM 3.2svn)
[gsm @ 0x7fe0b1806600] Estimating duration from bitrate, this may be
inaccurate
Input #0, gsm, from 'sample.gsm':
  Duration: 00:03:52.32, start: 0.000000, bitrate: 13 kb/s
    Stream #0.0: Audio: gsm, 8000 Hz, mono, s16, 13 kb/s
>>> print result
{  "format" : {
    "filename" : "sample.gsm",
    "nb_streams" : 1,
    "format_name" : "gsm",
    "format_long_name" : "raw GSM",
    "start_time" : "0.000000",
    "duration" : "232.320000",
    "size" : "383328.000000",
    "bit_rate" : "13200.000000"
}}

我知道采样率可能是一个流特定的显示,可以在 -show_format 选项结果中显示。但即使可以在重新编码时使用 -ar 进行设置,也没有其他选项可以检测特定音频流的采样率。

我向 libav 提交了一张工单,但我只是好奇是否有其他方法从 libav 探测工具中提取采样率。非常感谢您的回答。

PS:针对 ffmpeg 的上游项目(ffprobe)来说,这将是相同的问题。

4个回答

19

-show_format 显示容器级别的信息——即适用于所有流的内容。采样率是单个流的属性,因此 -show_format 不显示它是完全正常的。您需要使用 -show_streams


1
太好了!感谢Anton的解释!-show_streams有效 :) - Devy
2
为了进一步缩小输出到音频流,我们可以添加-select_stream a,得到类似以下的命令:ffprobe -select_streams a -show_streams input.flac - Quar

3

我发现json库可以将ffprobe的输出解析为字典,并且详细说明了你的代码如何在Python中存储信息。这里是一个函数,如果您愿意,它可以打印媒体信息:

import json
from subprocess import check_output

def get_media_info(filename, print_result=True):
    """
    Returns:
        result = dict with audio info where:
        result['format'] contains dict of tags, bit rate etc.
        result['streams'] contains a dict per stream with sample rate, channels etc.
    """
    result = check_output(['ffprobe',
                            '-hide_banner', '-loglevel', 'panic',
                            '-show_format',
                            '-show_streams',
                            '-of',
                            'json', filename])

    result = json.loads(result)

    if print_result:
        print('\nFormat')

        for key, value in result['format'].items():
            print('   ', key, ':', value)

        print('\nStreams')
        for stream in result['streams']:
            for key, value in stream.items():
                print('   ', key, ':', value)

        print('\n')

    return result

2
这个命令即使对于视频也可以起到作用:
ffprobe -v error -select_streams a -of default=noprint_wrappers=1:nokey=1 -show_entries stream=sample_rate input.wav

2

参考jadujoel的答案,我们可以使用解析json的jq在shell中完成相同的操作。

ffprobe -hide_banner -loglevel panic -show_format -show_streams -of json input.wav | \
  jq '.streams[0].sample_rate'

"44100" 表示音频采样率,即每秒钟采集的样本数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接