|
本帖最后由 xcy1020 于 2018-11-30 21:45 编辑
一、准备:
安装FFmpeg
二、
原视频信息:
- Input #0, matroska,webm, from 'test.mkv':
- Metadata:
- title : Test
- encoder : libebml v1.3.4 + libmatroska v1.4.5
- creation_time : 2018-11-06 06:53:47
- Duration: 01:29:13.35, start: 0.000000, bitrate: 1894 kb/s
- Stream #0:0: Video: h264 (High 10), yuv420p10le, 720x480 [SAR 1:1 DAR 3:2], 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
- Metadata:
- BPS : 1250909
- BPS-eng : 1250909
- DURATION : 01:29:13.348000000
- DURATION-eng : 01:29:13.348000000
- NUMBER_OF_FRAMES: 160440
- NUMBER_OF_FRAMES-eng: 160440
- NUMBER_OF_BYTES : 837069482
- NUMBER_OF_BYTES-eng: 837069482
- _STATISTICS_WRITING_APP: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_APP-eng: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_DATE_UTC: 2018-11-06 06:53:47
- _STATISTICS_WRITING_DATE_UTC-eng: 2018-11-06 06:53:47
- _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- Stream #0:1(chi): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
- Metadata:
- title : Cantonese.粤语
- BPS : 320000
- BPS-eng : 320000
- DURATION : 01:29:13.344000000
- DURATION-eng : 01:29:13.344000000
- NUMBER_OF_FRAMES: 250938
- NUMBER_OF_FRAMES-eng: 250938
- NUMBER_OF_BYTES : 214133761
- NUMBER_OF_BYTES-eng: 214133761
- _STATISTICS_WRITING_APP: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_APP-eng: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_DATE_UTC: 2018-11-06 06:53:47
- _STATISTICS_WRITING_DATE_UTC-eng: 2018-11-06 06:53:47
- _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- Stream #0:2(chi): Audio: aac (LC), 48000 Hz, stereo, fltp
- Metadata:
- title : Mandarin.国语
- BPS : 320000
- BPS-eng : 320000
- DURATION : 01:29:13.344000000
- DURATION-eng : 01:29:13.344000000
- NUMBER_OF_FRAMES: 250938
- NUMBER_OF_FRAMES-eng: 250938
- NUMBER_OF_BYTES : 214133761
- NUMBER_OF_BYTES-eng: 214133761
- _STATISTICS_WRITING_APP: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_APP-eng: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_DATE_UTC: 2018-11-06 06:53:47
- _STATISTICS_WRITING_DATE_UTC-eng: 2018-11-06 06:53:47
- _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- Stream #0:3(chi): Subtitle: subrip (default)
- Metadata:
- title : chs.简体中文
- BPS : 46
- BPS-eng : 46
- DURATION : 01:28:30.782000000
- DURATION-eng : 01:28:30.782000000
- NUMBER_OF_FRAMES: 933
- NUMBER_OF_FRAMES-eng: 933
- NUMBER_OF_BYTES : 30850
- NUMBER_OF_BYTES-eng: 30850
- _STATISTICS_WRITING_APP: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_APP-eng: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_DATE_UTC: 2018-11-06 06:53:47
- _STATISTICS_WRITING_DATE_UTC-eng: 2018-11-06 06:53:47
- _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- Stream #0:4(eng): Subtitle: subrip
- Metadata:
- title : eng.English
- BPS : 30
- BPS-eng : 30
- DURATION : 01:28:30.782000000
- DURATION-eng : 01:28:30.782000000
- NUMBER_OF_FRAMES: 905
- NUMBER_OF_FRAMES-eng: 905
- NUMBER_OF_BYTES : 20335
- NUMBER_OF_BYTES-eng: 20335
- _STATISTICS_WRITING_APP: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_APP-eng: mkvmerge v9.5.0 ('Quiet Fire') 32bit
- _STATISTICS_WRITING_DATE_UTC: 2018-11-06 06:53:47
- _STATISTICS_WRITING_DATE_UTC-eng: 2018-11-06 06:53:47
- _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
- _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
复制代码
生成视频和字幕1(中文)
- ffmpeg -i test.mkv -c:v copy -map 0:0 -bsf:v h264_mp4toannexb -scodec webvtt -map 0:3 -an -f hls -hls_time 5 -hls_list_size 0 test/video-only/video-only.m3u8
复制代码
生成视频和字幕2(英文)
- ffmpeg -i test.mkv -c:v copy -map 0:0 -bsf:v h264_mp4toannexb -scodec webvtt -map 0:4 -an -f hls -hls_time 5 -hls_list_size 0 test/video-only/video-only.m3u8
复制代码
生成音频:
第一条(粤语):
- ffmpeg -i test.mkv -c:a copy -map 0:1 -bsf:v h264_mp4toannexb -vn -sn -f hls -hls_time 5 -hls_list_size 0 test/audio/cantonese/cantonese.m3u8
复制代码
第二条(国语):
- ffmpeg -i test.mkv -c:a copy -map 0:2 -bsf:v h264_mp4toannexb -vn -sn -f hls -hls_time 5 -hls_list_size 0 test/audio/cantonese/cantonese.m3u8
复制代码
三、
手动创建Master Playlist:
- #EXTM3U
- #EXT-X-VERSION:7
- #EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="h264",NAME="MainVideo", DEFAULT=YES,URI="https://example.com/test/video-only/video-only.m3u8"
- #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac",NAME="Mandarin", DEFAULT=NO,LANGUAGE="zh",CHANNELS="2",CHARACTERISTICS="public.accessibility.describes-video",URI="https://example.com/test/audio/mandarin/mandarin.m3u8"
- #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac",NAME="Cantonese", DEFAULT=YES,LANGUAGE="zh",CHANNELS="2",CHARACTERISTICS="public.accessibility.describes-video",URI="https://example.com/test/audio/cantonese/cantonese.m3u8"
- #EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="ChineseSimple",LANGUAGE="zh",DEFAULT=YES,FORCED=YES,CHARACTERISTICS="public.accessibility.transcribes-spoken-dialog, public.accessibility.describes-music-and-sound",URI="https://example.com/test/subs/chs/chs_vtt.m3u8"
- #EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="English",LANGUAGE="en",DEFAULT=NO,FORCED=NO,CHARACTERISTICS="public.accessibility.transcribes-spoken-dialog, public.accessibility.describes-music-and-sound",URI="https://example.com/test/subs/eng/eng_vtt.m3u8"
- #EXT-X-STREAM-INF:BANDWIDTH=76800,CODECS="mp4a.40.2,avc1.64000c",VIDEO="h264",AUDIO="aac",SUBTITLES="subs",RESOLUTION=720x480,FRAME-RATE=29.97
- https://example.com/test/video-only/video-only.m3u8
复制代码
注:
1.字幕及M3U8文件编码必须UTF-8
2.EXT-X-STREAM-INF中的BANDWIDTH没看懂怎么算 希望有大佬解疑答惑
3.Dplayer开启了字幕功能,字幕只支持WebVTT格式,可以用FFmpeg转
- ffmpeg -i test.srt -scodec webvtt test.vtt
复制代码
如播放test.m3u8, 同目录下添加名为test.m3u8.vtt的WebVTT格式字幕即可
test.mp4则需添加test.mp4.vtt字幕。
参考文档:
1.RFC 8216 https://datatracker.ietf.org/doc/rfc8216/
2.RFC 8216 https://tools.ietf.org/html/rfc8216
3.HLS(m3u8) 草案 译 (待修订) https://blog.csdn.net/biezhihua/article/details/52161800
4.使用ffmpeg搭建HLS直播系统 https://**blogs.com/tocy/p/using-ffmpeg-build-hls-live-system.html
文件基于大佬@冻猫 原贴地址:https://hostloc.wiki/forum.php?mod=viewthread&tid=438265&highlight=dplayer
H5ai_Dplayer_HLS.zip
(293.3 KB, 下载次数: 124)
后记:
貌似这个兼容性,良莠不齐,Chrome支持,Firefox不支持,手机端Chrome和Firefox均只有音频,没有视频,字幕不加载,可能是自己的Master Playlist写得不好,技术有限。 |
|