音视频学习笔记 04 从视频里面提取音频

84 阅读4分钟

从视频里面提取音频

ffmpeg -i xxx.mp4 (输入的带有音频的多媒体文件)
			-vnvideo no 过滤视频元素)
			-c:a libfdk_aac-codecaudio 编码器选择)
			-ar 44100 (采样率)
			-ac 2(双声道)
			-profile:a acc_he_v2 (设置audio的配置文件,用后面的格式)
			audio.aac(文件名)

ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he_v2 3.aac

结果报错

bin ./ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he_v2 3.aac
ffmpeg version 7.1.1 Copyright (c) 2000-2025 the FFmpeg developers
  built with Apple clang version 17.0.0 (clang-1700.0.13.5)
  configuration: --prefix=/Users/mac/ffmpeg-7.1.1 --disable-static --enable-shared --enable-debug=3
  libavutil      59. 39.100 / 59. 39.100
  libavcodec     61. 19.101 / 61. 19.101
  libavformat    61.  7.100 / 61.  7.100
  libavdevice    61.  3.100 / 61.  3.100
  libavfilter    10.  4.100 / 10.  4.100
  libswscale      8.  3.100 /  8.  3.100
  libswresample   5.  3.100 /  5.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '7.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isomiso2avc1mp41mp42
  Duration: 00:13:04.04, start: 0.000000, bitrate: 993 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080, 932 kb/s, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 90k tbn (default)
      Metadata:
        handler_name    : VideoHandler
        vendor_id       : [0][0][0][0]
        encoder         : dailymotion/hls.js
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 56 kb/s (default)
      Metadata:
        handler_name    : SoundHandler
        vendor_id       : [0][0][0][0]
[aost#0:0 @ 0x7fde5500b440] Unknown encoder 'libfdk_aac'
[aost#0:0 @ 0x7fde5500b440] Error selecting an encoder
Error opening output file 3.aac.
Error opening output files: Encoder not found

老师说, 编译时加上 enable-libfdk_aac, ok,重新编译

通过编译选项查看 ./configure -h | grep 发现有 --enable-libfdk-aac 选项,说明编码器名字输入不对

ffmpeg -i 7.mp4  -vn -c:a libfdk-aac -ar 44100 -ac 2 -profile:a aac_he_v2 3.aac
➜  bin ./ffmpeg -i 7.mp4  -vn -c:a libfdk-aac -ar 44100 -ac 2 -profile:a aac_he_v2 3.aac
dyld[92894]: Library not loaded: /Users/mac/ffmpeg-7.1.1/lib/libavdevice.61.dylib
  Referenced from: <79E18789-1414-38B5-9D3B-64399766E500> /Users/mac/ffmpeg-7.1.1_1/bin/ffmpeg
  Reason: tried: '/Users/mac/ffmpeg-7.1.1/lib/libavdevice.61.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/mac/ffmpeg-7.1.1/lib/libavdevice.61.dylib' (no such file), '/Users/mac/ffmpeg-7.1.1/lib/libavdevice.61.dylib' (no such file)
[1]    92894 abort      ./ffmpeg -i 7.mp4 -vn -c:a libfdk-aac -ar 44100 -ac 2 -profile:a aac_he_v2 

猜测是编译时确实没有把对应 编码器 编译选项打开,老实重新编译,

FFmpeg-n7.1.1: ./configure --prefix=/Users/mac/ffmpeg-7.1.1 --disable-static --enable-shared --enable-libfdk-aac --enable-debug=3
➜  FFmpeg-n7.1.1 ./configure --prefix=/Users/mac/ffmpeg-7.1.1 --disable-static --enable-shared --enable-libfdk-aac --enable-debug=3                     
ERROR: libfdk_aac not found

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem

参考: www.jianshu.com/p/b6ad3b706…

home-brew 安装 brew install fdk-aac

➜  FFmpeg-n7.1.1 brew install fdk-aac
==> Fetching fdk-aac
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles//fdk-aac-2.0.3.sonoma
########################################################################### 100.0%
==> Pouring fdk-aac-2.0.3.sonoma.bottle.tar.gz
🍺  /usr/local/Cellar/fdk-aac/2.0.3: 16 files, 3.3MB
==> Running `brew cleanup fdk-aac`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

安装完 执行上面 ./configure --prefix=/Users/mac/ffmpeg-7.1.1 --disable-static --enable-shared --enable-libfdk-aac --enable-debug=3 顺利通过,接下来就是 编译 make -j 4 和 安装 make install 了 .

我这里是 mbp 2018 macOS 15.5, 直觉上是5 分钟左右编译完成。

        cpb: bitrate max/min/avg: 48109/48109/48109 buffer size: 0 vbv_delay: N/A
[out#0/adts @ 0x7faac110ddc0] video:0KiB audio:4605KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000%
➜  bin ./ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he_v2 7.aac

确认编码器名称确实是 libfdk_aac

ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he_v2 7.aac

尝试 opus 编码

./ffmpeg -i 7.mp4  -vn -c:a libopus -ar 44100 -ac 2 1.opus

结果

[aost#0:0 @ 0x7fb475313f00] Unknown encoder 'libopus'
[aost#0:0 @ 0x7fb475313f00] Error selecting an encoder
Error opening output file 1.opus.
Error opening output files: Encoder not found

重新编译 ffmpeg

./configure --prefix=/Users/mac/ffmpeg-7.1.1  --enable-debug=3 --disable-static --enable-shared --enable-libfdk-aac  --enable-libx264 --enable-filter=delogo --enable-gpl --enable-nonfree --enable-libopus

再次执行

./ffmpeg -i 7.mp4  -vn -c:a libopus  -ac 2 1.opus

提取成功

使用不同编码提前音频的全部命令

./ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he_v2 1.aac
./ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_he 2.aac
./ffmpeg -i 7.mp4  -vn -c:a libfdk_aac -ar 44100 -ac 2 -profile:a aac_low 3.aac
./ffmpeg -i 7.mp4  -vn -c:a libopus  -ac 2 1.opus

参考: ffmpeg.org/ffmpeg-code…