Splitting and merging of .m4v video files with ffmpeg

While constructing content for a Plex Media Server (PMS) at home I’ve recently need to both split and merge .m4v files containing h264/aac encoded material.  These are my notes to myself of what I did using ffmpeg after much googling. If you’re not me, hope these notes help you too 🙂

Splitting an m4v file

Hypothetically, I’ve ended with a file called TwoEpisodesMyShow.m4v that contains multiple episodes of a TV show in a single m4v. For the PMS to properly recognise the individual episodes I need to split this into separate per-episode .m4v files.

Splitting can be effected by using ffmpeg to copy video and audio tracks (without any transcoding) between nominated start and end times of the source .m4v file.

Assume, for example, I wish to extract “s01e01 MyShow.m4v” and “s01e02 MyShow.m4v” from TwoEpisodesMyShow.m4v.  Episode 1 runs from the beginning of TwoEpisodesMyShow.m4v up to the 1hr 30min 00sec mark, and episode 2 runs from the 1hr 30min 02.5sec mark to the end.

Episode 1 can be extracted using ffmpeg and the “-to hh:mm:ss[.xxx]” option as follows:

ffmpeg -i "TwoEpisodesMyShow.m4v" -acodec copy -vcodec copy -to 01:30:00 "s01e01 MyShow.m4v"

Episode 2 can be extracted using ffmpeg with the “-ss hh:mm:ss[.xxx]” option as follows:

ffmpeg -ss 01:30:02.5 -i "TwoEpisodesMyShow.m4v" -acodec copy -vcodec copy "s01e02 MyShow.m4v"


  • The above example only copies one video and one audio stream, and throws away any subtitle stream. If the source has multiple streams, check the -map option to force copying of all required streams.
  • Copying (using the copy pseudo-codecs for the -acodec and -vcodec options) is fast, and performs no transcoding. I’ve seen comments in other forums regarding possible loss of sync between audio and video when using these options. Check the results.
  • The -ss option seeks to an approximate position if used as an input option (placed before the -i), and seeks (much more slowly) to an exact position if used as an output option (placed after the -i).
    • (I’ve seen suggestions in other forums that one can combine two -ss to achieve a fast, accurate search by using -ss before the -i to roughly position ffmpeg just before the desired search point, then another -ss after the -i to perform a more accurate search from the first -ss point onwards.)
  • The -to option simply means “stop writing output at specified time”.
  • If there were additional episodes inside the source file, use both -ss and -to to identify the start and end times for extraction.

Merging multiple .m4v files

Hypothetically, I have a h264/aac encoded movie split across three files: MyShow1.m4v, MyShow2.m4v and MyShow3.m4v. What I want is all three stitched together into a single file, MyShow.m4v.

Found http://bruno.defraine.net/techtips/joining-h264-aac-video-files, like the idea so much I used it successfully like so:

ffmpeg -i "MyShow1.m4v"  -c copy -bsf:v h264_mp4toannexb "1.ts"
ffmpeg -i "MyShow2.m4v"  -c copy -bsf:v h264_mp4toannexb "2.ts"
ffmpeg -i "MyShow3.m4v"  -c copy -bsf:v h264_mp4toannexb "3.ts"
ffmpeg -i concat:"1.ts|2.ts|3.ts" -c copy -bsf:a aac_adtstoasc "MyShow.m4v"
rm 1.ts 2.ts 3.ts

And that’s it!

As noted, given source files with the same height and width, aspect ratio, and so forth, ffmpeg can easily convert each file to an MPEG transport stream (TS) — a format that supports concatenation — and then concatenate and transcode back to .m4v in one final step.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: