How to create an m3u8 playlist from a list of mp4 files?

I have a bunch of mp4 files from which I want to prepare an m3u8 playlist, is there any way to do it using FFmpeg?

1 Like

You accomplish your task using a single FFmpeg command in which you can read all the files and combine all the streams from different files using a filter, and output m3u8 using segment format from FFmpeg but it would be hacky and prone to errors.

Follow the steps given below:

1. Download FFmpeg

This tutorial assumes that you have installed FFmpeg on your system but if not refer:

2. Put your MP4 files in order

Put every MP4 file you have in one directory. The files in this directory will appear in the m3u8 playlist in the order that they are listed.

3. Execute FFmpeg

Locate your MP4 files by opening a terminal or command prompt and going to the directory. Next, execute the given FFmpeg command:

ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i input3.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate3.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|intermediate3.ts" -c copy -bsf:a aac_adtstoasc -f mpegts intermediate_all.ts
ffmpeg -i intermediate_all.ts -c copy -bsf:a aac_adtstoasc -f hls -hls_time 10 -hls_list_size 0 output.m3u8

  • Every MP4 file is converted to MPEG-TS format (intermediateX.ts) in the first three lines.
  • All intermediate files are combined into a single file (intermediate_all.ts) by the fourth line.
  • The concatenated file is transformed into a m3u8 playlist (output.m3u8) in the fifth line. The segment duration is set to 10 seconds using the -hls_time argument. This option can be changed to suit your needs.

4. Completed playlist creation

The output.m3u8 m3u8 playlist file will be located in the same directory as your MP4 files following the execution of this command. The separate segments made during the conversion are referred to in this playlist.

5. Host and Stream

You can utilize a content delivery network (CDN) that supports HLS or host the m3u8 file and the appropriate segments on a web server to make your video available for streaming.

Your MP4 files are now ready to stream via HTTP Live Streaming in a m3u8 playlist. As necessary, change the parameters to fit your particular use case.

Let me know if you have further queries related to this.

Thank you

1 Like

Can you please provide me the command you said in the comment, I think you forgot to write it down.

1 Like

Here is the solution to your problem just follow the steps given below:
• Just combine all the mp4 files into a single mp4 file using FFmpeg Concat filter.
• Run the highlighted command (segment format) which takes the mp4 file as input and provides m3u8 as output.

Refer:

Thanks for your response it works for me!