当我使用
ffmpeg
将一些纯音频flv文件转码为hls
段时,最后一个.ts
段包含一些解码错误。似乎当最后一段的持续时间足够短(大约 1.5 秒)时就会发生这种情况。
这是我的转码命令:
ffmpeg -i 111.flv -c:a aac -b:a 128k -f hls -hls_time 10.00 -hls_list_size 9999 tmp.m3u8
并输出:
ffmpeg version n4.0.1-5-gb5106c5 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
configuration: --prefix=/home/zx/ffmpeg_build --extra-cflags=-I/home/zx/ffmpeg_build/include --extra-ldflags='-L/home/zx/ffmpeg_build/lib -ldl' --bindir=/home/zx/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-version3 --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libspeex --enable-libvpx --enable-libx264 --disable-shared --enable-static --disable-debug
libavutil 56. 14.100 / 56. 14.100
libavcodec 58. 18.100 / 58. 18.100
libavformat 58. 12.100 / 58. 12.100
libavdevice 58. 3.100 / 58. 3.100
libavfilter 7. 16.100 / 7. 16.100
libswscale 5. 1.100 / 5. 1.100
libswresample 3. 1.100 / 3. 1.100
libpostproc 55. 1.100 / 55. 1.100
Input #0, flv, from '111.flv':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: isomiso2
date : 2018-05-31 01:31
encoder : Lavf57.25.100
Duration: 00:12:11.40, start: 0.000000, bitrate: 135 kb/s
Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[hls @ 0x25d4280] Opening 'tmp0.ts' for writing
[mpegts @ 0x26feb80] frame size not set
Output #0, hls, to 'tmp.m3u8':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: isomiso2
date : 2018-05-31 01:31
encoder : Lavf58.12.100
Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.18.100 aac
[hls @ 0x25d4280] Opening 'tmp1.ts' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp2.ts' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp3.ts' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp4.ts' for writing
......
[hls @ 0x25d4280] Opening 'tmp71.ts' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp72.ts' for writingx
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp73.ts' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
[hls @ 0x25d4280] Opening 'tmp.m3u8.tmp' for writing
size=N/A time=00:12:11.39 bitrate=N/A speed=84.7x
video:0kB audio:11421kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[aac @ 0x25a0580] Qavg: 567.976
转码后,
m3u8
文件中最后一段的时长为1.39秒:
....
#EXTINF:9.984000,
tmp71.ts
#EXTINF:10.005333,
tmp72.ts
#EXTINF:1.386667,
tmp73.ts
#EXT-X-ENDLIST
但是当我使用
ffprobe
解析最后一段时,出现错误:
ffprobe -i tmp73.ts
ffprobe version n4.0.1-5-gb5106c5 Copyright (c) 2007-2018 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
configuration: --prefix=/home/zx/ffmpeg_build --extra-cflags=-I/home/zx/ffmpeg_build/include --extra-ldflags='-L/home/zx/ffmpeg_build/lib -ldl' --bindir=/home/zx/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-version3 --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libspeex --enable-libvpx --enable-libx264 --disable-shared --enable-static --disable-debug
libavutil 56. 14.100 / 56. 14.100
libavcodec 58. 18.100 / 58. 18.100
libavformat 58. 12.100 / 58. 12.100
libavdevice 58. 3.100 / 58. 3.100
libavfilter 7. 16.100 / 7. 16.100
libswscale 5. 1.100 / 5. 1.100
libswresample 3. 1.100 / 3. 1.100
libpostproc 55. 1.100 / 55. 1.100
tmp73.ts: Invalid data found when processing input
输入flv:
http://45.76.56.248/hls_transcode_fail.flv
md5sum: 4627bbb27bcb22c143ad4dfa47359650
由于我的统计模块依赖于
ffprobe
的输出,所以这个异常会导致整个转码任务失败。我的临时解决方案是在统计阶段跳过最后一段太短的段。但我需要一种更合理、更稳健的方法来解决或绕过这个问题。任何建议将不胜感激。
不知何故,ffmpeg 无法将某些元数据添加到 HLS 流的最后一个音频片段,导致稍后尝试使用 ffprobe 或 ffmpeg 探测文件时出现
Invalid data found when processing input
错误,因为它们无法确定音频的格式文件,您可以通过在命令中设置 -loglevel trace
来查看该文件的播放情况。
解决方法是在输入之前添加
-f mpegts
,以跳过格式探测并强制其将文件识别为 MPEG-TS 片段。
解决方案是运行类似
ffmpeg -f mpegts -i /tmp/original.ts -c copy /fixed.ts
的命令来修复有故障的段。
您会看到固定段中的文件大小增加,因为丢失的信息被添加到文件中。
不知道为什么会发生这种情况,但我花了很多时间来弄清楚该怎么办,希望它能帮助别人。