How to split a video into chunks based on duration using FFmpeg?

To split a video into chunks based on duration using FFmpeg is very easy, you just have to use the segment format provided by FFmpeg. With segment format, you can use the -segment_time option provided by FFmpeg to set chunk duration. Consider the following command as an example for your use case.

ffmpeg -i input.mp4 -c copy -map 0 -segment_time 00:10:00 -f segment output%03d.mp4

  • -i input.mp4: Indicates the input file (input.mp4) to be segmented.
  • -c copy: This option is used to make a duplicate copy without re-encoding.
  • -map 0: This will tell the FFmpeg to get all streams from the input file.
  • -segment_time 00:10:00: Indicates the duration for each segment. In this case, it’s set to 10 minutes.
  • -f segment: Sets the output format as segmented files.
  • output%03d.mp4: Indicates the output file name pattern whereas, the %03d represents a sequence number that is assigned to each segment, starting from 001. The segments will be saved as output001.mp4, output002.mp4, and so on.

Here are the detailed steps to follow if you are a beginner in this process:

1. Install FFmpeg on your system

Install FFmpeg using the below links on your system:

2. Launch a Command Prompt or Terminal: On your computer, launch a command prompt or terminal window.

3. Go to the Directory: To find the directory containing your input video file, use the cd command. For instance:
cd path/to/your/video/file

4. Run FFmpeg Command: To divide a video file into separate segments, run the FFmpeg command. The video will be divided into 4-second segments by the aforementioned sample command. By altering the value following -segment_time, you can modify the segment duration.

ffmpeg -i input.mp4 -c:v copy -c:a copy -f segment -segment_time 4 -reset_timestamps 1 -map 0 output_%03d.ts

5. Wait for Segmenting: The segments will be created by FFmpeg after the video has been processed. The length of the original video determines how long it takes.

6. Examine the Output: In the same directory where you executed the FFmpeg command, you will discover several segment files (such as output_001.ts, output_002.ts, etc.) once the segmenting is finished. HLS streaming is possible with these portions.

Note:

  • .ts extensions are commonly used to preserve HLS segments. If necessary, you can set up FFmpeg to store them with various extensions.

  • Typically, you would put these parts into a folder and make an M3U8 playlist file with the segments listed in the proper sequence for HLS streaming.

  • To guarantee that each segment’s timestamp begins at 0, which is necessary for precise segment duration, it’s crucial to use the -reset_timestamps option.

These instructions will help you effectively use FFmpeg to break a video into smaller segments that are appropriate for HLS streaming. Modify the segment’s duration as necessary to satisfy your unique needs.

After splitting you can also merge videos/audios in FFmpeg.

For more visit: Splitting large videos into smaller chunks with FFmpeg