Published on

Split Audio/Video Files into Multiple Parts with FFmpeg

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Why Split Media Files?

Splitting audio and video files is useful for:

  • Sharing Smaller Segments: Send or upload manageable file sizes.
  • Content Organization: Separate sections of a file for better structuring.
  • Optimizing Playback: Create clips for presentations or tutorials.

What Does Splitting Mean?

Splitting involves dividing a single media file into smaller segments based on time intervals or predefined durations. FFmpeg provides a powerful and flexible way to achieve this.


Splitting Media Files with FFmpeg

Split by Duration

To split a file into parts of a specific duration:

ffmpeg -i input.mp4 -c copy -map 0 -segment_time 00:05:00 -f segment output_%03d.mp4
  • -i input.mp4: The source file.
  • -c copy: Copy streams without re-encoding.
  • -map 0: Include all streams (audio, video, subtitles).
  • -segment_time 00:05:00: Split into 5-minute segments.
  • -f segment: Use the segment format.
  • output_%03d.mp4: Output file names with sequential numbering.

Example: Split an Audio File into 30-Second Segments

ffmpeg -i audio.mp3 -c copy -map 0 -segment_time 00:00:30 -f segment part_%03d.mp3

Split by Start and Stop Times

To split based on specific time ranges:

ffmpeg -i input.mp4 -ss 00:00:00 -to 00:05:00 -c copy part1.mp4
ffmpeg -i input.mp4 -ss 00:05:00 -to 00:10:00 -c copy part2.mp4
  • -ss: Start time.
  • -to: Stop time.
  • part1.mp4, part2.mp4: Output files for each segment.

Splitting with Re-Encoding

If re-encoding is necessary (e.g., for compatibility):

ffmpeg -i input.mp4 -ss 00:00:00 -to 00:05:00 -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k part1.mp4
  • -c:v libx264: Use the H.264 codec for video.
  • -c:a aac: Use the AAC codec for audio.
  • -crf 23: Set quality (lower is better).

Automating Splitting for Multiple Files

To split all .mp4 files in a directory into 10-minute segments:

for file in *.mp4; do 
  ffmpeg -i "$file" -c copy -map 0 -segment_time 00:10:00 -f segment "${file%.mp4}_part_%03d.mp4"
  done

Best Practices

  1. Ensure Accurate Durations: Use precise timestamps or durations.
  2. Test Output Files: Verify each segment for accuracy and quality.
  3. Use Copy Mode When Possible: Avoid re-encoding to save time and maintain quality.

Conclusion

Splitting media files with FFmpeg is a versatile process that can accommodate various use cases, from creating short clips to organizing lengthy content. With simple commands, you can customize output segments to meet your needs. Start splitting your media files today!