How to split and demux a file with FFmpeg

If you have a large video or audio file and want to split it into several smaller files, you can use ffmpeg to obtain that goal. What you need are the start timecodes and the length of the segments you want to cut. You can tell ffmpeg the start position of a segment with the -ss option and the length of a segment with the -t option. Timecodes are of the form hh:mm:ss.ms, i.e. 2:03:05.07. If no start position is given, the segment will start at the beginning of the original file, if no length option is given, the segment will end at the end of the original file. Use the -c copy option to make sure that all streams from the original files will be passed through to the segments.

In this example it is assumed you have a large video file called longmovie.mp4 which you want to split in segments from the beginning to 0:05:00, from 1:00:00 to 1:05:00 and from 1:30:00 to the end with all streams passed through. Here is the command that does it for you:

$ ffmpeg -i longmovie.mp4 -c copy -t 0:05:00 beginning.mp4 -c copy -ss 1:00:00 -t 0:05:00 middle.mp4 -c copy -ss 1:30:00 end.mp4

To extract a certain stream from a file, you also use the -c[:v|a|s] copy and additionally the -map <fileindex>:<streamindex> option for file and stream selection. In this example it is assumed you want to demultiplex the video file muxed.mp4 that contains a video stream, two audio streams and two subtitles which you want to store in separate files. You can use

$ ffmpeg -i muxed.mp4

to find out which stream has which index. After that, use the -map <fileindex>:<streamindex> option to address individual streams.

$ ffmpeg -i muxed.mp4 -c:v copy -map 0:0 video.m4v -c:a copy -map 0:1 audio1.m4a -c:a copy -map 0:2 audio2.m4a -c:s copy -map 0:3 subtitle1.srt -c:s copy -map 0:4 subtitle2.srt

About h0nk3ym0nk3y

Yeah, whatever, never mind

Posted on September 27, 2012, in Audio / Video and tagged , , , , . Bookmark the permalink. 3 Comments.

  1. aimkeepachieve

    You may also try iDealshare VideoGo which can split video or audio files without re-encoding and with the original video and audio quality.

  2. Thanks! I just needed to remux a wmv to mkv, and it was as simple as

    ffmpeg -i video.wmv -c copy video.mkv

  3. Thank you very much! I’m on android and using the ffmpegui app.

Leave a comment