ffmpeg 编码的 hls 使音频和视频逐渐不同步

标签 ffmpeg http-live-streaming video-encoding

当尝试将源 .mp4 编码为多种比特率和大小以进行自适应 HLSv3 播放时,生成的音频和视频会逐渐不同步。稍后擦洗时,它似乎会重置,并保持同步。

我通过 homebrew-ffmpeg 使用最新的 ffmpeg 4.2.1,使用 libfdk-aac 编解码器。请注意,ffmpeg 4.1 也会发生这种情况。

ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1-with-options_1 --enable-shared --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libaom --enable-libmp3lame --enable-libopus --enable-libsnappy --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --disable-libjack --disable-indev=jack --enable-opencl --enable-videotoolbox --disable-htmlpages --enable-libfdk-aac --enable-nonfree
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100

音频和视频流略微关闭,会导致这个问题吗?
ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 INPUT_FILE.mp4 80.480400ffprobe -v error -select_streams a:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 INPUT_FILE.mp4 80.469333
以下是源文件的一些元数据:
General
Complete name                            : INPUT_VIDEO.mp4
Format                                   : MPEG-4
Format profile                           : Base Media / Version 2
Codec ID                                 : mp42 (mp42/mp41)
File size                                : 236 MiB
Duration                                 : 1 min 21 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 24.2 Mb/s
Encoded date                             : UTC 2019-08-27 22:13:53
Tagged date                              : UTC 2019-08-27 22:15:15
TIM                                      : 00;00;00;00
TSC                                      : 30000
TSZ                                      : 1001

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Main@L4.1
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 2 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 1 min 21 s
Bit rate mode                            : Variable
Bit rate                                 : 23.8 Mb/s
Maximum bit rate                         : 768 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 29.970 (30000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.383
Stream size                              : 233 MiB (99%)
Language                                 : English
Encoded date                             : UTC 2019-08-27 22:13:53
Tagged date                              : UTC 2019-08-27 22:13:53
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 1 min 21 s
Source duration                          : 1 min 21 s
Bit rate mode                            : Constant
Bit rate                                 : 317 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 3.10 MiB (1%)
Source stream size                       : 3.10 MiB (1%)
Language                                 : English
Encoded date                             : UTC 2019-08-27 22:13:53
Tagged date                              : UTC 2019-08-27 22:13:53

和编码命令:
ffmpeg \
-i INPUT_VIDEO \
-dn \
-sn \
-filter_complex \
"[0:v]fps=fps=24000/1001, \
setpts=(PTS-STARTPTS), \
split=12[vsplit1][vsplit2][vsplit3][vsplit4][vsplit5][vsplit6][vsplit7][vsplit8][vsplit9][vsplit10][vsplit11][vsplit12]; \
[vsplit1]scale=-1:1080[video_1080_4.1]; \
[vsplit2]scale=-1:720[video_720_4.1]; \
[vsplit3]scale=-1:720[video_720_3.1]; \
[vsplit4]scale=-1:540[video_540_3.1]; \
[vsplit5]scale=-1:432[video_432_3.1]; \
[vsplit6]scale=-1:270[video_270_3.0]; \
[vsplit7]scale=-1:270[video_270_3.1]; \
[vsplit8]scale=-1:144[video_144_4.1]; \
[vsplit9]scale=-1:144[video_144_3.0]; \
[vsplit10]scale=1920:1080[base_1080]; \
[vsplit11]scale=1280:720[base_720]; \
[vsplit12]scale=640:360[base_360]" \
\
-map "[video_1080_4.1]" \
-r:v:0 "24000/1001" \
-c:v:0 "libx264" \
-x264-params:0 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:0 "slow" \
-profile:v:0 "high" \
-level:v:0 "4.1" \
-refs:v:0 "2" \
-b-pyramid:v:0 "strict" \
-tune:v:0 "film" \
-b:v:0 "4800000" \
-maxrate:v:0 "4800000" \
-bufsize:v:0 "6*4800000/8" \
-vsync:v:0 "cfr" \
-bsf:v:0 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_720_4.1]" \
-r:v:1 "24000/1001" \
-c:v:1 "libx264" \
-x264-params:1 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:1 "slow" \
-profile:v:1 "main" \
-level:v:1 "4.1" \
-refs:v:1 "2" \
-b-pyramid:v:1 "strict" \
-tune:v:1 "film" \
-b:v:1 "3200000" \
-maxrate:v:1 "3200000" \
-bufsize:v:1 "6*3200000/8" \
-vsync:v:1 "cfr" \
-bsf:v:1 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_720_3.1]" \
-r:v:2 "24000/1001" \
-c:v:2 "libx264" \
-x264-params:2 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:2 "slow" \
-profile:v:2 "main" \
-level:v:2 "3.1" \
-refs:v:2 "2" \
-b-pyramid:v:2 "strict" \
-tune:v:2 "film" \
-b:v:2 "2200000" \
-maxrate:v:2 "2200000" \
-bufsize:v:2 "6*2200000/8" \
-vsync:v:2 "cfr" \
-bsf:v:2 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_540_3.1]" \
-r:v:3 "24000/1001" \
-c:v:3 "libx264" \
-x264-params:3 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:3 "slow" \
-profile:v:3 "main" \
-level:v:3 "3.1" \
-refs:v:3 "2" \
-b-pyramid:v:3 "strict" \
-tune:v:3 "film" \
-b:v:3 "1400000" \
-maxrate:v:3 "1400000" \
-bufsize:v:3 "6*1400000/8" \
-vsync:v:3 "cfr" \
-bsf:v:3 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_432_3.1]" \
-r:v:4 "24000/1001" \
-c:v:4 "libx264" \
-x264-params:4 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:4 "slow" \
-profile:v:4 "main" \
-level:v:4 "3.1" \
-refs:v:4 "2" \
-b-pyramid:v:4 "strict" \
-tune:v:4 "film" \
-b:v:4 "900000" \
-maxrate:v:4 "900000" \
-bufsize:v:4 "6*900000/8" \
-vsync:v:4 "cfr" \
-bsf:v:4 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_270_3.0]" \
-r:v:5 "24000/1001" \
-c:v:5 "libx264" \
-x264-params:5 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:5 "slow" \
-profile:v:5 "baseline" \
-level:v:5 "3.0" \
-refs:v:5 "2" \
-b-pyramid:v:5 "strict" \
-tune:v:5 "film" \
-b:v:5 "400000" \
-maxrate:v:5 "400000" \
-bufsize:v:5 "6*400000/8" \
-vsync:v:5 "cfr" \
-bsf:v:5 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_270_3.1]" \
-r:v:6 "24000/1001" \
-c:v:6 "libx264" \
-x264-params:6 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:6 "slow" \
-profile:v:6 "main" \
-level:v:6 "3.1" \
-refs:v:6 "2" \
-b-pyramid:v:6 "strict" \
-tune:v:6 "film" \
-b:v:6 "200000" \
-maxrate:v:6 "200000" \
-bufsize:v:6 "6*200000/8" \
-vsync:v:6 "cfr" \
-bsf:v:6 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_144_4.1]" \
-r:v:7 "24000/1001" \
-c:v:7 "libx264" \
-x264-params:7 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:7 "slow" \
-profile:v:7 "high" \
-level:v:7 "4.1" \
-refs:v:7 "2" \
-b-pyramid:v:7 "strict" \
-tune:v:7 "film" \
-b:v:7 "64000" \
-maxrate:v:7 "64000" \
-bufsize:v:7 "6*64000/8" \
-vsync:v:7 "cfr" \
-bsf:v:7 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_144_3.0]" \
-r:v:8 "24000/1001" \
-c:v:8 "libx264" \
-x264-params:8 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:8 "slow" \
-profile:v:8 "baseline" \
-level:v:8 "3.0" \
-refs:v:8 "2" \
-b-pyramid:v:8 "strict" \
-tune:v:8 "film" \
-b:v:8 "56000" \
-maxrate:v:8 "56000" \
-bufsize:v:8 "6*56000/8" \
-vsync:v:8 "cfr" \
-bsf:v:8 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[base_1080]" \
-c:v:9 "libx264" \
-preset:v:9 "slow" \
\
-map "[base_720]" \
-c:v:10 "libx264" \
-preset:v:10 "slow" \
\
-map "[base_360]" \
-c:v:11 "libx264" \
-preset:v:11 "slow" \
\
-map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 \
-c:a "libfdk_aac" \
-ar "48000" \
-ab "128k" \
-af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" \
\
-f tee \
-flags +global_header \
"[select=\'v:0,a:0,v:1,a:1,v:2,a:2,v:3,a:3,v:4,a:4,v:5,a:5,v:6,a:6,v:7,a:7,v:8,a:8\':f=hls:hls_flags=discont_start+temp_file:hls_time=6:hls_list_size=0:var_stream_map=\'v:0,a:0 v:1,a:1 v:2,a:2 v:3,a:3 v:4,a:4 v:5,a:5 v:6,a:6 v:7,a:7 v:8,a:8\':master_pl_name=playlist.m3u8:hls_segment_filename=INPUT_VIDEO__%v_%03d.ts]INPUT_VIDEO/out_%v.m3u8|[select=\'v:9,a:9\':f=mp4:movflags=+faststart]INPUT_VIDEO/large-1920-1080.mp4|[select=\'v:10,a:10\':f=mp4:movflags=+faststart]INPUT_VIDEO/med-1280-720.mp4|[select=\'v:11,a:11\':f=mp4:movflags=+faststart]INPUT_VIDEO/sm-640-360.mp4"

最佳答案

试试这个命令:

ffmpeg \
-i INPUT_VIDEO \
-dn \
-sn \
-filter_complex \
"[0:v]fps=fps=24000/1001:start_time=0:round=near, \
split=12[vsplit1][vsplit2][vsplit3][vsplit4][vsplit5][vsplit6][vsplit7][vsplit8][vsplit9][vsplit10][vsplit11][vsplit12]; \
[vsplit1]scale=-1:1080[video_1080_4.1]; \
[vsplit2]scale=-1:720[video_720_4.1]; \
[vsplit3]scale=-1:720[video_720_3.1]; \
[vsplit4]scale=-1:540[video_540_3.1]; \
[vsplit5]scale=-1:432[video_432_3.1]; \
[vsplit6]scale=-1:270[video_270_3.0]; \
[vsplit7]scale=-1:270[video_270_3.1]; \
[vsplit8]scale=-1:144[video_144_4.1]; \
[vsplit9]scale=-1:144[video_144_3.0]; \
[vsplit10]scale=1920:1080[base_1080]; \
[vsplit11]scale=1280:720[base_720]; \
[vsplit12]scale=640:360[base_360]" \
\
-map "[video_1080_4.1]" \
-r:v:0 "24000/1001" \
-c:v:0 "libx264" \
-x264-params:0 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:0 "slow" \
-profile:v:0 "high" \
-level:v:0 "4.1" \
-refs:v:0 "2" \
-b-pyramid:v:0 "strict" \
-tune:v:0 "film" \
-b:v:0 "4800000" \
-maxrate:v:0 "4800000" \
-bufsize:v:0 "6*4800000/8" \
-vsync:v:0 "cfr" \
-bsf:v:0 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_720_4.1]" \
-r:v:1 "24000/1001" \
-c:v:1 "libx264" \
-x264-params:1 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:1 "slow" \
-profile:v:1 "main" \
-level:v:1 "4.1" \
-refs:v:1 "2" \
-b-pyramid:v:1 "strict" \
-tune:v:1 "film" \
-b:v:1 "3200000" \
-maxrate:v:1 "3200000" \
-bufsize:v:1 "6*3200000/8" \
-vsync:v:1 "cfr" \
-bsf:v:1 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_720_3.1]" \
-r:v:2 "24000/1001" \
-c:v:2 "libx264" \
-x264-params:2 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:2 "slow" \
-profile:v:2 "main" \
-level:v:2 "3.1" \
-refs:v:2 "2" \
-b-pyramid:v:2 "strict" \
-tune:v:2 "film" \
-b:v:2 "2200000" \
-maxrate:v:2 "2200000" \
-bufsize:v:2 "6*2200000/8" \
-vsync:v:2 "cfr" \
-bsf:v:2 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_540_3.1]" \
-r:v:3 "24000/1001" \
-c:v:3 "libx264" \
-x264-params:3 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:3 "slow" \
-profile:v:3 "main" \
-level:v:3 "3.1" \
-refs:v:3 "2" \
-b-pyramid:v:3 "strict" \
-tune:v:3 "film" \
-b:v:3 "1400000" \
-maxrate:v:3 "1400000" \
-bufsize:v:3 "6*1400000/8" \
-vsync:v:3 "cfr" \
-bsf:v:3 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_432_3.1]" \
-r:v:4 "24000/1001" \
-c:v:4 "libx264" \
-x264-params:4 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:4 "slow" \
-profile:v:4 "main" \
-level:v:4 "3.1" \
-refs:v:4 "2" \
-b-pyramid:v:4 "strict" \
-tune:v:4 "film" \
-b:v:4 "900000" \
-maxrate:v:4 "900000" \
-bufsize:v:4 "6*900000/8" \
-vsync:v:4 "cfr" \
-bsf:v:4 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_270_3.0]" \
-r:v:5 "24000/1001" \
-c:v:5 "libx264" \
-x264-params:5 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:5 "slow" \
-profile:v:5 "baseline" \
-level:v:5 "3.0" \
-refs:v:5 "2" \
-b-pyramid:v:5 "strict" \
-tune:v:5 "film" \
-b:v:5 "400000" \
-maxrate:v:5 "400000" \
-bufsize:v:5 "6*400000/8" \
-vsync:v:5 "cfr" \
-bsf:v:5 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_270_3.1]" \
-r:v:6 "24000/1001" \
-c:v:6 "libx264" \
-x264-params:6 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:6 "slow" \
-profile:v:6 "main" \
-level:v:6 "3.1" \
-refs:v:6 "2" \
-b-pyramid:v:6 "strict" \
-tune:v:6 "film" \
-b:v:6 "200000" \
-maxrate:v:6 "200000" \
-bufsize:v:6 "6*200000/8" \
-vsync:v:6 "cfr" \
-bsf:v:6 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_144_4.1]" \
-r:v:7 "24000/1001" \
-c:v:7 "libx264" \
-x264-params:7 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:7 "slow" \
-profile:v:7 "high" \
-level:v:7 "4.1" \
-refs:v:7 "2" \
-b-pyramid:v:7 "strict" \
-tune:v:7 "film" \
-b:v:7 "64000" \
-maxrate:v:7 "64000" \
-bufsize:v:7 "6*64000/8" \
-vsync:v:7 "cfr" \
-bsf:v:7 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[video_144_3.0]" \
-r:v:8 "24000/1001" \
-c:v:8 "libx264" \
-x264-params:8 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \
-preset:v:8 "slow" \
-profile:v:8 "baseline" \
-level:v:8 "3.0" \
-refs:v:8 "2" \
-b-pyramid:v:8 "strict" \
-tune:v:8 "film" \
-b:v:8 "56000" \
-maxrate:v:8 "56000" \
-bufsize:v:8 "6*56000/8" \
-vsync:v:8 "cfr" \
-bsf:v:8 "h264_metadata=fixed_frame_rate_flag=1" \
\
-map "[base_1080]" \
-c:v:9 "libx264" \
-preset:v:9 "slow" \
\
-map "[base_720]" \
-c:v:10 "libx264" \
-preset:v:10 "slow" \
\
-map "[base_360]" \
-c:v:11 "libx264" \
-preset:v:11 "slow" \
\
-map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 \
-c:a "libfdk_aac" \
-ar "48000" \
-ab "128k" \
-af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" \
\
-f tee \
-flags +global_header \
"[select=\'v:0,a:0,v:1,a:1,v:2,a:2,v:3,a:3,v:4,a:4,v:5,a:5,v:6,a:6,v:7,a:7,v:8,a:8\':f=hls:hls_flags=discont_start+temp_file:hls_time=6:hls_list_size=0:var_stream_map=\'v:0,a:0 v:1,a:1 v:2,a:2 v:3,a:3 v:4,a:4 v:5,a:5 v:6,a:6 v:7,a:7 v:8,a:8\':master_pl_name=playlist.m3u8:hls_segment_filename=INPUT_VIDEO__%v_%03d.ts]INPUT_VIDEO/out_%v.m3u8|[select=\'v:9,a:9\':f=mp4:movflags=+faststart]INPUT_VIDEO/large-1920-1080.mp4|[select=\'v:10,a:10\':f=mp4:movflags=+faststart]INPUT_VIDEO/med-1280-720.mp4|[select=\'v:11,a:11\':f=mp4:movflags=+faststart]INPUT_VIDEO/sm-640-360.mp4"
看到我已经去掉了一个不必要的 setpts从复杂链中过滤,并修改fps过滤器为您提供真正的 CBR 输出,正确的起始 PTS 为 0 并舍入到接近(通过 start_time=0:round=near 设置)。
请测试并报告。

关于ffmpeg 编码的 hls 使音频和视频逐渐不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021105/

相关文章:

video - ffmpeg 只设置宽度

c++ - 需要帮助纠正 FFmpeg DLL 的访问冲突

video - 提高 ffmpeg 视频捕获性能?

ffmpeg - 使用输入流(或 url)时 ffmpeg 可以搜索吗?

h.264 - Apple HLS 中的 PES 数据包内的访问单元如何对齐?

ffmpeg - HTML5 Progressive Streaming -- 无后续范围请求

windows - AVPlayer 在 tvOS 13.4 上使用 HLS 直播流 url 失败(适用于 tvOS 13.3)

Android如何录制、上传、转码、下载、播放

python图像帧到视频

iphone - ffmpeg 转换命令输出到 iPad 的 h.264