How to Convert MP4 to HLS Playlist Using FFmpeg

Converting an MP4 video into an HTTP Live Streaming (HLS) playlist using FFmpeg on Linux allows you to deliver adaptive bitrate streaming to your viewers, optimizing playback for various internet speeds. This guide provides a straightforward, step-by-step terminal command to split your video into standard .ts segments and generate the necessary .m3u8 index file. By the end of this article, you will have a web-ready streaming setup deployed directly from your Linux command line.

Prerequisite: Install FFmpeg

Before running the conversion command, ensure you have FFmpeg installed on your Linux system. You can verify your installation or install it using your package manager.

  • Ubuntu/Debian: sudo apt update && sudo apt install ffmpeg
  • CentOS/RHEL: sudo dnf install ffmpeg
  • Verification: ffmpeg -version

The Core FFmpeg Command

To convert a standard input.mp4 file into an HLS playlist, navigate to the directory containing your video and run the following command:

ffmpeg -i input.mp4 -codec:v libx264 -codec:a aac -map 0 -f hls -hls_time 10 -hls_playlist_type vod -hls_segment_filename "stream_%03d.ts" playlist.m3u8

Breakdown of the Command Options

Understanding what each flag does will help you customize the output to match your specific streaming requirements:

  • -i input.mp4: Specifies the path to your source MP4 video file.
  • -codec:v libx264: Encodes the video stream using the H.264 codec, which is widely compatible with almost all HLS-capable players.
  • -codec:a aac: Encodes the audio stream to AAC, the standard audio format required for HLS streaming.
  • -map 0: Includes all streams (video and audio) from the first input file into the output.
  • -f hls: Forces the output format to be HLS.
  • -hls_time 10: Sets the target segment length to 10 seconds. You can lower this number (e.g., to 4 or 6) if you want to optimize for lower latency, though 10 seconds is standard for Video-on-Demand (VOD).
  • -hls_playlist_type vod: Creates a Video-on-Demand playlist. This ensures the player knows the file has a fixed duration and embeds the #EXT-X-PLAYLIST-TYPE:VOD tag.
  • -hls_segment_filename "stream_%03d.ts": Defines the naming convention for the individual video chunks. The %03d acts as a sequential counter, generating files named stream_000.ts, stream_001.ts, and so on.
  • playlist.m3u8: The name of the master index file that the web player will load.

Verifying the Output Files

Once the processing completes, check your target directory. You will see a collection of files generated by the command:

  • playlist.m3u8: The text-based index file that coordinates the playback order.
  • Multiple .ts files: The actual fragmented video data blocks.

To serve these files to the public, you simply upload the entire batch of .ts files alongside the .m3u8 file to your web server or cloud storage bucket, ensuring the directory structure remains intact.