Arad
Arad

Reputation: 12734

How to add a subtitle to an HLS playlist using FFmpeg?

I have a video (video.mp4) and a subtitle file (subtitle.srt).

I'm trying to use FFmpeg to generate a VoD HLS playlist with 3 different resolutions for the video; and I also want the subtitle file to turn into segmented .vtt files.

I've tried the following command, but it doesn't work, it gives me a "Conversion failed!" message without any further information:

ffmpeg -i video.mp4 -i subtitle.srt \
-preset slow -g 60 -sc_threshold 0 \
-map 0 -map 0 -map 0 -map 1 \
-s:v:0 640x360 -c:v:0 h264 -b:v:0 500k \
-s:v:1 854x480 -c:v:1 h264 -b:v:1 1000k \
-s:v:2 1280x720 -c:v:2 h264 -b:v:2 2000K \
-c:a copy -c:s webvtt \
-f hls -hls_playlist_type vod -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
-master_pl_name master.m3u8 -hls_time 6 -hls_list_size 0 -hls_allow_cache 1 -start_number 1 \
-hls_segment_filename "output/hls/%v/seg-%d.ts" output/hls/%v/index.m3u8

I'm struggling to find any useful information in FFmpeg docs or anywhere else regarding how subtitles can be added to an HLS playlist. What should the command be like exactly? What am I missing in mine?

Upvotes: 4

Views: 5982

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 1261

The documentation isn't great, but see near the bottom of the section for -var_stream_map. You need to add a mapping for the subtitles to the relevant variant stream groups.

So your example above will become:

-var_stream_map "v:0,a:0,s:0 v:1,a:1,s:0 v:2,a:2,s:0"

If you wish, you can set the subtitle group name by appending ,sgroup=examplegroupname.

Finally, there is an undocument option hls_subtitle_path which allows you to set the subtitle manifest name (which defaults to the main m3u8 name plus _vtt.m3u8.

Upvotes: 4

Related Questions