ffmpeg如何判断音频的采样率进行重采样?

花间隐虎
  • 301
湖南

我想使用 ffmpeg 对用户上传上来的音频文件进行重采样,执行的命令是:

ffmpeg -i 1.mp3 -ac 2 -ar 44100 -y 2.mp3

现在的问题是当用户上传的音频文件采样率小于 44100khz 时,ffmpeg也会转换成 44100khz的音频文件,这将导致音频文件的体积增大不少。

有什么办法可以在命令中判断当音频文件本身的采样率小于 44100khz 时候就使用默认的采样率吗?

回复
阅读 478
1 个回答
Feng_Yu
  • 10.5k
陕西
✓ 已被采纳

先用ffprobe命令(ffmpeg组件之一,通常ffmpeg编译的时候会带有ffmpeg,ffprobe,ffplay三个可执行程序)判断下音频的采样率就行了:

$ ffprobe -hide_banner Switchworks\ -\ Legacy.mp3
Input #0, mp3, from 'Switchworks - Legacy.mp3':
  Metadata:
    encoder         : Lavf56.4.101
    comment         : 163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/VJ/jMFWqZuOwVj5Pz1IFJtK8DQ9IjLVaz87SqH08kkRDK1PjNi5J2vocxaiyN7awAmc4XOlnnTqL/vkKcFo/FXnhbVKkBR+xRqbZw0NCVbBR8REtM8TZvn8qOQYKPEnAH/TZibPsAWTpE8IKCDTN8zVLZt0VBx8h/eDYU5lTLkn93F2tBiKiiu66j/PxiOP3eElwJeEerAxinUrCAin1
    album           : 古树旋律 Deemo
    title           : Legacy
    artist          : Switchworks
    album_artist    : Switchworks
    disc            : 3
    track           : 3
  Duration: 00:02:03.66, start: 0.025057, bitrate: 192 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 192 kb/s

如果你期望格式化解析,也可以考虑使用ffprobe的格式化输出的功能,以下是JSON格式范例:

$ ffprobe -v error -hide_banner -print_format json -show_format -show_streams Switchworks\ -\ Legacy.mp3
{
    "streams": [
        {
            "index": 0,
            "codec_name": "mp3",
            "codec_long_name": "MP3 (MPEG audio layer 3)",
            "codec_type": "audio",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/14112000",
            "start_pts": 353600,
            "start_time": "0.025057",
            "duration_ts": 1745141760,
            "duration": "123.663673",
            "bit_rate": "192000",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0,
                "captions": 0,
                "descriptions": 0,
                "metadata": 0,
                "dependent": 0,
                "still_image": 0
            }
        }
    ],
    "format": {
        "filename": "Switchworks - Legacy.mp3",
        "nb_streams": 1,
        "nb_programs": 0,
        "format_name": "mp3",
        "format_long_name": "MP2/3 (MPEG audio layer 2/3)",
        "start_time": "0.025057",
        "duration": "123.663673",
        "size": "2970352",
        "bit_rate": "192156",
        "probe_score": 51,
        "tags": {
            "encoder": "Lavf56.4.101",
            "comment": "163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/VJ/jMFWqZuOwVj5Pz1IFJtK8DQ9IjLVaz87SqH08kkRDK1PjNi5J2vocxaiyN7awAmc4XOlnnTqL/vkKcFo/FXnhbVKkBR+xRqbZw0NCVbBR8REtM8TZvn8qOQYKPEnAH/TZibPsAWTpE8IKCDTN8zVLZt0VBx8h/eDYU5lTLkn93F2tBiKiiu66j/PxiOP3eElwJeEerAxinUrCAin1xVJS8A5NhdwMRqsTBK8eZPsGjolII7S2FKSVP9EXrxDnZVBmkpE6aDRdSjn0BQR66tFJt6AP+LeQwTLH73JQTHAbdQZFgHj+Ks2XnH9r5hUcvxGpFknFybukNs+MSKxWG/AJ2EaVJxdCUzJEUP5c+sf+t9l8ne4hEtVigkgMn5NMhZSZYMhQ8sn19zjPQzTaADhreAJPjFQiuQXzM5X7J20YShDiM6Fum/Kc0LDvubDjgxs6jtLIrYv6Plpu8Xyo51MQafDkFTP8aUCQ76XwJkKocexmwn0KO4eKDQTvIdHc7Z666NVUd96/yvASubDQP1TK46v0cMlZ6+4QuFxKlW9oPbUWyh3B+NY8V2uFaJsYbsANf8zse+1P53/1uMKsVr4KpMZU7qKls6S2ZDPSt8NdDeSaC6lU+TZdHot3uiVQGMSfWnHc3qLpdUGhmeJUZ/tki7yiwJ/3MB+zXFhB4Oy",
            "album": "古树旋律 Deemo",
            "title": "Legacy",
            "artist": "Switchworks",
            "album_artist": "Switchworks",
            "disc": "3",
            "track": "3"
        }
    }
}

ffprobe所有支持的输出格式参考官方文档: https://ffmpeg.org/ffprobe.ht...

快速上手可以直接看别人的总结: https://ottverse.com/ffprobe-...

如果你期望将ffmpeg作为依赖静态打包到你的发行包里,可能不希望额外增加ffprobe的包,那你也可以直接用ffmpeg的输出来判断一次,但是注意必须忽略命令错误,因为这不是ffmpeg的合法执行参数,命令会非零退出码退出:

$ ffmpeg -hide_banner -i Switchworks\ -\ Legacy.mp3
Input #0, mp3, from 'Switchworks - Legacy.mp3':
  Metadata:
    encoder         : Lavf56.4.101
    comment         : 163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/VJ/jMFWqZuOwVj5Pz1IFJtK8DQ9IjLVaz87SqH08kkRDK1PjNi5J2vocxaiyN7awAmc4XOlnnTqL/vkKcFo/FXnhbVKkBR+xRqbZw0NCVbBR8REtM8TZvn8qOQYKPEnAH/TZibPsAWTpE8IKCDTN8zVLZt0VBx8h/eDYU5lTLkn93F2tBiKiiu66j/PxiOP3eElwJeEerAxinUrCAin1
    album           : 古树旋律 Deemo
    title           : Legacy
    artist          : Switchworks
    album_artist    : Switchworks
    disc            : 3
    track           : 3
  Duration: 00:02:03.66, start: 0.025057, bitrate: 192 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 192 kb/s
At least one output file must be specified

当然你也可以用其他更易用的程序比如mediainfo之类的代替,这就需要增加额外的依赖了

宣传栏