Search code examples
ffmpegvideo-streaminghttp-live-streamingvideo.js

How to create chunks of videos (.ts) and .m3u8 file from a mp4 video?


I need to create chunks of videos (.ts) of fixed duration (say 5 or 10 seconds) from a mp4 video. And also I need the chunks in different formats (260p, 480p, 720p, 1080p).

I'm able to create the chunks and m3u8 using the command below:

ffmpeg -i input.mp4 -g 60 -hls_time 10 out.m3u8

but not able to create for different resolutions as mentioned above.


Solution

  • Use the scale filter and the HLS muxer:

    ffmpeg -i input.mp4 -filter_complex "[0]scale=-2:260[260];[0]scale=-2:480[480];[0]scale=-2:720[720];[0]scale=-2:1080[1080]" -map "[260]" -map 0:a -map "[480]" -map 0:a -map "[720]" -map 0:a -map "[1080]" -map 0:a -f hls -hls_time 10 -var_stream_map "v:0,a:0,name:260 v:1,a:1,name:480 v:2,a:2,name:720 v:3,a:3,name:1080" -hls_segment_filename "%v_%03d.ts" -master_pl_name "master.m3u8" "output_%v.m3u8"