首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用avprobe / ffprobe检测音频采样率?

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

Stack Overflow用户
提问于 2013-08-08 22:31:03
回答 3查看 10K关注 0票数 9

我使用的是libav 9.6,是通过Homebrew安装的。

代码语言:javascript
复制
$ 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终端的输出:

代码语言:javascript
复制
$ 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代码示例:

代码语言:javascript
复制
>>> 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提交了a ticket,但我只是好奇是否有其他方法可以从libav探测实用程序中提取采样率。我很感谢你事先的回答。

PS:在这种情况下,对于ffmpeg (ffprobe)的上游项目也会有同样的问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-08 23:17:57

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

票数 15
EN

Stack Overflow用户

发布于 2019-07-08 20:47:44

我发现json库可以将from的输出解析到字典中,并详细介绍了在python中存储信息的代码。下面是一个函数,可以执行此操作并打印媒体信息(如果愿意):

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-12-28 13:58:43

Enis Berk's answer之后,我们可以在shell中使用jq (它解析json)来做同样的事情。

代码语言:javascript
复制
ffprobe -hide_banner -loglevel panic -show_format -show_streams -of json input.wav | \
  jq '.streams[0].sample_rate'

"44100"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18128727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档