我想使用 FFmpeg 直播带有字幕的 HLS 流。 我有一个媒体。媒体有一个视频轨道、两个音频轨道、四个字幕。 但是,我尝试了这个命令:
ffmpeg -i input0.mkv \
-map 0:v:0 -c:v libx264 -strict -2 -s 426x240 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack240p_%d.ts video/videoTrack240p.m3u8 \
-map 0:v:0 -c:v libx264 -strict -2 -s 1280x720 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack720p_%d.ts video/videoTrack720p.m3u8 \
-map 0:v:0 -c:v libx264 -strict -2 -s 1920x1080 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack1080p_%d.ts video/videoTrack1080p.m3u8 \
-map 0:a:0 -c:a aac -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename audio/audioTrack1_%d.aac audio/audioTrack0.m3u8 \
-map 0:a:1 -c:a aac -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename audio/audioTrack0_%d.aac audio/audioTrack1.m3u8 \
-map 0:s:0 -c:s webvtt -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename subtitle/subtitleTrack0_%d.vtt subtitle/subtitleTrack0.m3u8 \
-map 0:s:1 -c:s webvtt -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename subtitle/subtitleTrack1_%d.vtt subtitle/subtitleTrack1.m3u8 \
-map 0:s:2 -c:s webvtt -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename subtitle/subtitleTrack2_%d.vtt subtitle/subtitleTrack2.m3u8 \
-map 0:s:3 -c:s webvtt -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename subtitle/subtitleTrack3_%d.vtt subtitle/subtitleTrack3.m3u8
它显示此错误:
ffmpeg version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
WARNING: library configuration mismatch
avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
[matroska,webm @ 0x5631128c1460] Could not find codec parameters for stream 5 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0x5631128c1460] Could not find codec parameters for stream 6 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, matroska,webm, from 'input0.mkv':
Metadata:
title : GRAVITY DAZE The Animation Ouverture
encoder : libebml v1.2.3 + libmatroska v1.3.0
creation_time : 2017-03-30T06:22:52.000000Z
Duration: 00:17:23.58, start: 0.000000, bitrate: 2155 kb/s
Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 24 fps, 24 tbr, 1k tbn, 48 tbc (default)
Stream #0:1(jpn): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
Stream #0:2(jpn): Audio: aac (LC), 48000 Hz, stereo, fltp
Stream #0:3(chi): Subtitle: ass (default)
Metadata:
title : CHT
Stream #0:4(chi): Subtitle: ass
Metadata:
title : CHS
Stream #0:5(jpn): Subtitle: hdmv_pgs_subtitle
Stream #0:6(eng): Subtitle: hdmv_pgs_subtitle
[hls @ 0x5631128fc840] Opening 'subtitle/subtitleTrack0_0.vtt' for writing
[hls @ 0x5631128fc840] Opening 'subtitle/subtitleTrack00.vtt' for writing
[mpegts @ 0x56311296bda0] No streams to mux were specified
Could not write header for output file #5 (incorrect codec parameters ?): Invalid argument
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:0 -> #1:0 (h264 (native) -> h264 (libx264))
Stream #0:0 -> #2:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #3:0 (aac (native) -> aac (native))
Stream #0:2 -> #4:0 (aac (native) -> aac (native))
Stream #0:3 -> #5:0 (ass (ssa) -> webvtt (native))
Stream #0:4 -> #6:0 (ass (ssa) -> webvtt (native))
Stream #0:5 -> #7:0 (hdmv_pgs_subtitle (pgssub) -> webvtt (native))
Stream #0:6 -> #8:0 (hdmv_pgs_subtitle (pgssub) -> webvtt (native))
Last message repeated 1 times
如果命令不添加字幕:
ffmpeg -i input0.mkv \
-map 0:v:0 -c:v libx264 -strict -2 -s 426x240 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack240p_%d.ts video/videoTrack240p.m3u8 \
-map 0:v:0 -c:v libx264 -strict -2 -s 1280x720 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack720p_%d.ts video/videoTrack720p.m3u8 \
-map 0:v:0 -c:v libx264 -strict -2 -s 1920x1080 -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename video/videoTrack1080p_%d.ts video/videoTrack1080p.m3u8 \
-map 0:a:0 -c:a aac -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename audio/audioTrack1_%d.aac audio/audioTrack0.m3u8 \
-map 0:a:1 -c:a aac -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -hls_segment_filename audio/audioTrack0_%d.aac audio/audioTrack1.m3u8
流媒体工作正常,没有错误。 我很长时间找不到带字幕的 FFmpeg 流媒体示例。
要创建分段字幕及其相应的 HLS 播放列表文件,您需要使用更通用的
segment
输出格式而不是 hls
。
这是一个使用
VTT
文件名格式和名为 0000.vtt
的播放列表创建分段 chunklist_vtt.m3u8
字幕的示例。
请注意,播放列表的内部格式是通过 FFmpeg 分析输出扩展名“自动”确定的,但可以使用 segment_format
选项显式设置。命令细分:
ffmpeg -y -hide_banner -i input.mp4 \
-vn \ # drop all video streams
-an \ # drop all audio streams
-map s:0 \ # select the first embedded subtitle stream
-f segment \ # use generic segment format
-segment_time 3.0 \ # set segment duration
-segment_list chunklist_vtt.m3u8 \ # set playlist filename (internal format determined by extension)
%04d.vtt # output filename (internal format determined by extension)
通过打印字幕片段的内容,您可以看到这些仅包含原始字幕的片段:
$ cat 0000.vtt
WEBVTT
00:00.000 --> 00:02.880
you may have seen videos in which someone is using
00:02.880 --> 00:06.660
an old computer when it suddenly
makes popping and crackling noises
播放列表也具有默认的 HLS 格式:
$ head -n9 chunklist_vtt.m3u8
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:12
#EXTINF:6.660000,
0000.vtt
#EXTINF:6.120000,
0001.vtt
更详细的解释,请查看相关文档:https://ffmpeg.org/ffmpeg-all.html#segment_002c-stream_005fsegment_002c-ssegment