標題好長⋯ 總之這就是本文的目的。
VideoToolbox (VTEncoderXPCService) 竟然還會使用 eGPU 硬體資源,這點讓我很驚喜
正文開始。
首先使用 ffmpeg -codecs 查看所有能使用的編解碼器,結合 grep/egrep 指令只列出我們需要的內容,比如
ffmpeg -codecs | egrep -i '(codecs|=|videotoolbox)'
內容節錄如下
Codecs:
D..... = Decoding supported
.E.... = Encoding supported
..V... = Video codec
..A... = Audio codec
..S... = Subtitle codec
...I.. = Intra frame-only codec
....L. = Lossy compression
.....S = Lossless compression
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (encoders: libx264 libx264rgb h264_videotoolbox )
DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (encoders: libx265 hevc_videotoolbox )
以 h264_videotoolbox 為例,將 avi 檔轉為 mp4 檔,指令為
ffmpeg -i input.avi -c:v h264_videotoolbox output.mp4
參考結果如下
[avi @ 0x7fe9ce00ce00] non-interleaved AVI
Guessed Channel Layout for Input Stream #0.1 : mono
Input #0, avi, from 'input.avi':
Metadata:
encoder : Lavf58.45.100
Duration: 04:05:00.60, start: 0.000000, bitrate: 18588 kb/s
Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1920x1080, 18327 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (h264_videotoolbox))
Stream #0:1 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[swscaler @ 0x7fe9d11d0000] deprecated pixel format used, make sure you did set range correctly
Output #0, mp4, to 'output.mp4':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Video: h264 (h264_videotoolbox) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080, q=2-31, 200 kb/s, 30 fps, 15360 tbn, 30 tbc
Metadata:
encoder : Lavc58.91.100 h264_videotoolbox
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc58.91.100 aac
More than 1000 frames duplicated 23040kB time=00:03:18.08 bitrate= 952.9kbits/s dup=994 drop=0 speed=3.75x
More than 10000 frames duplicated 162048kB time=00:33:11.74 bitrate= 666.5kbits/s dup=9998 drop=0 speed=3.24x
frame=441018 fps= 78 q=-0.0 Lsize= 1083032kB time=04:05:00.56 bitrate= 603.5kbits/s dup=73771 drop=0 speed=2.61x
video:954215kB audio:123834kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.462156%
[aac @ 0x7fe9cd80cc00] Qavg: 141.648
也可以結合上一篇文章提到的「使用 FFmpeg 將行車記錄器分段影片合併」 ( https://pingnote.blogspot.com/2021/02/ffmpeg-concat.html ),在合併影片的同時以硬體加速編碼轉成 mp4 檔,指令為
ffmpeg -f concat -i filelist.txt -c:v h264_videotoolbox merge.mp4
若要進一步查看有哪些選項可以設定,則可以使用
ffmpeg -h encoder=h264_videotoolbox
內容節錄如下
Encoder h264_videotoolbox [VideoToolbox H.264 Encoder]:
General capabilities: delay
Threading capabilities: none
Supported pixel formats: videotoolbox_vld nv12 yuv420p
h264_videotoolbox AVOptions:
-profile <int> E..V...... Profile (from 0 to 5) (default 0)
baseline 1 E..V...... Baseline Profile
main 2 E..V...... Main Profile
high 3 E..V...... High Profile
extended 4 E..V...... Extend Profile
-level <int> E..V...... Level (from 0 to 52) (default 0)
1.3 13 E..V...... Level 1.3, only available with Baseline Profile
3.0 30 E..V...... Level 3.0
3.1 31 E..V...... Level 3.1
3.2 32 E..V...... Level 3.2
4.0 40 E..V...... Level 4.0
4.1 41 E..V...... Level 4.1
4.2 42 E..V...... Level 4.2
5.0 50 E..V...... Level 5.0
5.1 51 E..V...... Level 5.1
5.2 52 E..V...... Level 5.2
-coder <int> E..V...... Entropy coding (from 0 to 2) (default 0)
cavlc 1 E..V...... CAVLC entropy coding
vlc 1 E..V...... CAVLC entropy coding
cabac 2 E..V...... CABAC entropy coding
ac 2 E..V...... CABAC entropy coding
-a53cc <boolean> E..V...... Use A53 Closed Captions (if available) (default true)
-allow_sw <boolean> E..V...... Allow software encoding (default false)
-require_sw <boolean> E..V...... Require software encoding (default false)
-realtime <boolean> E..V...... Hint that encoding should happen in real-time if not faster (e.g. capturing from camera). (default false)
-frames_before <boolean> E..V...... Other frames will come before the frames in this session. This helps smooth concatenation issues. (default false)
-frames_after <boolean> E..V...... Other frames will come after the frames in this session. This helps smooth concatenation issues. (default false)
最後,參考 YouTube 對於上傳影片時建議的編碼設定,將轉檔指令修改為
ffmpeg -hide_banner -loglevel level \
-f concat -i filelist.txt \
-c:v h264_videotoolbox -profile:v high -coder cabac -bf 2 -r 30 -g 15 -b:v 10M \
-c:a aac_at -b:a 256K \
merge.mp4
補充說明其中的幾項參數
- 將 -g 參數 (GOP, Group Of Pictures) 設為 -r 參數 (Frame Rate) 的一半;由於我的原始影片 Frame Rate 只有 30,所以將 GOP 設為 15
- -aac_at 中的 at 指的是 Apple 另一個 Framework:AudioToolbox
參考資料
- 上傳影片時建議使用的編碼設定 - YouTube 說明
https://support.google.com/youtube/answer/1722171 - ffmpeg Documentation
https://ffmpeg.org/ffmpeg.html - -codecs
- Show all codecs known to libavcodec.
- Note that the term ’codec’ is used throughout this documentation as a shortcut for what is more correctly called a media bitstream format.
- -encoders
- Show all available encoders.
- -h, -?, -help, --help [arg]
- Show help. An optional parameter may be specified to print help about a specific item.
- encoder=encoder_name
- Print detailed information about the encoder named encoder_name.
- FFmpeg Codecs Documentation
https://ffmpeg.org/ffmpeg-codecs.html - Encode/H.264 – FFmpeg
https://trac.ffmpeg.org/wiki/Encode/H.264 - HWAccelIntro – FFmpeg
https://trac.ffmpeg.org/wiki/HWAccelIntro - Audio Toolbox
https://developer.apple.com/documentation/audiotoolbox - AudioToolbox 的初探與實作
https://sunxiaoshan.medium.com/audiotoolbox-的初探與實作-fb48962363c2 - Video Toolbox
https://developer.apple.com/documentation/videotoolbox
沒有留言:
張貼留言