編譯
環境
macOS Ventrua 13.4
ffmpeg 7.7.1
Visual Studio Code Version: 1.99.0 (Universal)
操作
FFmpeg 下載源碼
$ cd ffmpeg-x.y.z
$ ./configure
nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.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.# 聽勸 + 考慮到后面需要調試
$ mkdir build
$ ./configure --prefix=./build --disable-x86asm --enable-debug --disable-stripping
$ make
編譯成功后結果如下圖
ffmpeg
常用庫簡介
來自參考1
庫 | 介紹 |
---|---|
avcodec | 音視頻編解碼核心庫 |
avformat | 音視頻容器格式的封裝和解析 |
avutil | 核心工具庫 |
swscal | 圖像格式轉換的模塊 |
swresample | 音頻重采樣 |
avfilter | 音視頻濾鏡庫,如視頻加水印、音頻變聲 |
avdevice | 輸入輸出設備庫,提供設備數據的輸入與輸出,和硬件設備交互 |
準備
獲取媒體信息
可以通過ffmpeg
包含的工具命令ffprobe
# 獲取mp3的信息
$ ffprobe -show_format 天空之城.mp3
輸出結果如下,可知這個音頻文件采樣率是44.1KHz,雙聲道,碼率是128kb
一個采樣率為44.1KHz,采樣大小為16bit,雙聲道的 PCM編碼的WAV文件,它的數據速率則為44.1K×16×2=1411.2Kb/s。我們常說128K的MP3,對應的WAV的參數,就是這個 1411.2Kb/s,這個參數也被稱為數據帶寬,它和ADSL中的帶寬是一個概念。
Input #0, mp3, from '天空之城.mp3':Duration: 00:03:00.35, start: 0.025057, bitrate: 128 kb/sStream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/sMetadata:encoder : LAME3.99rSide data:replaygain: track gain - -3.300000, track peak - unknown, album gain - unknown, album peak - unknown,
[FORMAT]
filename=天空之城.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=180.349388
size=2886135
bit_rate=128024
解封裝
將mp3文件轉成pcm,選擇雙聲道,采樣率44100
# -y 允許覆蓋
# -i 源文件
# -acodec pcm_s16le 編碼器
# -f s16le 強制文件格式
# -ac 2 雙聲道
# -ar 44100 采樣率
$ ffmpeg -y -i 天空之城.mp3 -acodec pcm_s16le -f s16le -ac 2 -ar 44100 天空之城.pcm
mp3壓縮率差不多在1:10到1:12,原來大小是2.9M,轉換成31.8M,
下載VLC來播放pcm文件
$ /Applications/VLC.app/Contents/MacOS/VLC --demux=rawaud --rawaud-channels 2 --rawaud-samplerate 44100 天空之城.pcm
調試ffmpeg
本文在mac環境上使用vscode來調試ffmpeg
用vscode打開ffmpeg目錄,選中ffmpeg_g
點擊左側的調試功能,在出現的界面創建一個launch.json file
在出現的下拉選擇框中選擇Install extension
選擇CodeLLDB
安裝完成后再次點擊,配置launch.json
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "ffmpeg","type": "cppdbg","request": "launch","program": "${workspaceFolder}/ffmpeg_g","args": ["-f", "avfoundation", "-list_devices", "true", "-i", "\"\""],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "lldb"}]
}
設置斷點,點擊調試
與在Xcode中調試不一樣的是在交互窗口輸入命令前要加一個 -exec
。
參考
- ffmpeg 各庫的介紹、解碼流程、常用結構體
- ffmpeg處理pcm和mp3互轉
- mac:VLC播放YUV文件和pcm文件(命令行)
- The macOS platform uses vscode to debug ffmpeg
- https://ffmpeg.xianwaizhiyin.net/base-knowledge/base-knowledge.html