51

What is the fastest and least resource consuming method for splitting an MP4 file?

I tried ffmpeg but got an error:

$ ffmpeg -vcodec copy -ss 0 -t 00:10:00 -i /home/asafche/Videos/myVideos/MAH00124.MP4 /home/asafche/Videos/myVideos/eh.mp4
FFmpeg version SVN-r0.5.1-4:0.5.1-1ubuntu1.1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
configuration: --extra-version=4:0.5.1-1ubuntu1.1 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Mar 31 2011 18:53:20, gcc: 4.4.3

Seems stream 0 codec frame rate differs from container frame rate: 119.88 (120000/1001) -> 59.94 (60000/1001)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/asafche/Videos/myVideos/MAH00124.MP4':
  Duration: 00:15:35.96, start: 0.000000, bitrate: 5664 kb/s
    Stream #0.0(und): Video: h264, yuv420p, 1280x720, 59.94 tbr, 59.94 tbn, 119.88 tbc
    Stream #0.1(und): Audio: aac, 48000 Hz, stereo, s16
Output #0, mp4, to '/home/asafche/Videos/myVideos/eh.mp4':
    Stream #0.0(und): Video: libx264, yuv420p, 1280x720, q=2-31, 90k tbn, 59.94 tbc
    Stream #0.1(und): Audio: 0x0000, 48000 Hz, stereo, s16, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Unsupported codec for output stream #0.1

What am I doing wrong and how do I properly split an mp4 file?

0

9 Answers 9

99

With ffmpeg you can split file using the following command:

ffmpeg -i ORIGINALFILE.mp4 -acodec copy -vcodec copy -ss START -t LENGTH OUTFILE.mp4

where START is starting positing in seconds or in format hh:mm:ss LENGTH is the chunk length in seconds or in format hh:mm:ss

So you will need to run this command few times depending on how long your video. If let's say your video is 31 minutes long and you want so split into 15 min chunks here is how you run it:

ffmpeg -i ORIGINALFILE.mp4 -acodec copy -vcodec copy -ss 0 -t 00:15:00 OUTFILE-1.mp4
ffmpeg -i ORIGINALFILE.mp4 -acodec copy -vcodec copy -ss 00:15:00 -t 00:15:00 OUTFILE-2.mp4
ffmpeg -i ORIGINALFILE.mp4 -acodec copy -vcodec copy -ss 00:30:00 -t 00:15:00 OUTFILE-3.mp4

There is a python script that you can use that does this automatically(i.e. takes video file, chunk size in seconds and generates individual playable video files): https://github.com/c0decracker/video-splitter

8
  • 1
    Thanks, but i need further help with this. Look for the edit i did in my answer. Apr 18, 2011 at 3:34
  • 1
    This worked for me: ffmpeg -ss 00:05:00 -t 00:16:00 -i largfile.MP4 -acodec copy -vcodec copy smallfile.MP4 can someone explain why? Apr 18, 2011 at 4:22
  • I am going to speculate here a bit, but it looks like your source file is encoded with framerate that libx264 does not support, so it skips frames. I think this should work fine, but there might be some mishaps with audio because of this.
    – Alex
    Apr 18, 2011 at 18:14
  • maybe. will see... Apr 18, 2011 at 21:27
  • 1
    Asaf's command worked for me too. It would seem that the codec options should come after the inputfile.
    – daithib8
    Jul 30, 2011 at 15:02
11

Use mkvmerge from the mkvtoolnix package. Use something like

mkvmerge -o outputprefix --split 1G origfile.mp4

This would split your file in 1 GB blocks. You can use time-indications as well.

4
  • I've never used it before but tried: brilliant! instant speed and everything keeps being readable. Thanks!
    – tuk0z
    Jun 26, 2017 at 11:02
  • This is particularly useful if you want to specify a size. Thanks! Oct 25, 2019 at 15:47
  • From the documentation, you can also do --split duration:00:60:00.000 - see mkvtoolnix.download/doc/mkvmerge.html#d4e850
    – Kurtoid
    Aug 22, 2020 at 6:02
  • It did split the files, but the split files does not seems to be valid video files. My input file was mp4. Sep 18, 2022 at 9:25
10

If you prefer using a gui there is avidemux available in the repositories. Defining the cut points is much easier this way !

The selection markers define the part of the video that will be exported, using "copy" in audio and video avoids reencoding. You can also choose the container or change it (avi, mp4, mkv, etc).

I've done a bunch of cuts on some videos from a camera and it was instantaneous !

1
5

Not sure what your need is, but you can type this in a terminal: To open a terminal, press Ctrl + Alt + T

split -b 4M file.mp4 part_file

This will split the files into chunks of 4 megabytes. TO recreate the file again, type

cat part_file[a-c] > file.mp4

Notice that i have typed a-cin the bracket, because the split created 3 files. in your case you might have to see more. I will explain with an example.

$ ls -lh
-rwxr-xr-x 1 root        root        9.3M 2011-04-17 20:09 file.mp4

This shows that I have a single file, of 9.3 MB. To split, I type:

$ split -b 2M file.mp4 part_file

After splitting, I get the files as:

$ ls -lh
-rwxr-xr-x 1 root        root        9.3M 2011-04-17 20:09 file.mp4
-rw-r--r-- 1 thetuxracer thetuxracer 2.0M 2011-04-17 20:12 part_fileaa
-rw-r--r-- 1 thetuxracer thetuxracer 2.0M 2011-04-17 20:12 part_fileab
-rw-r--r-- 1 thetuxracer thetuxracer 2.0M 2011-04-17 20:12 part_fileac
-rw-r--r-- 1 thetuxracer thetuxracer 2.0M 2011-04-17 20:12 part_filead
-rw-r--r-- 1 thetuxracer thetuxracer 1.3M 2011-04-17 20:12 part_fileae

Notice the two characters appended to the filename.

To get the file back:

$ cat part_filename[start-end][start-end] > file.mp4
3
  • 3
    This method will be very quick but won't allow you to play the individual chunks. You can use ffmpeg to extract parts of a video file but to split it automatically into many small chunks which are all individually playable without reassembling them into a whole you'll probably need to write a script with an mp4 file. I can give you an example if you like, but if its just to move the file around then this answer is good.
    – Slipstream
    Apr 17, 2011 at 15:11
  • @Slipstream - you got it right - i need to split it for youtube. my camera is producing MP4 but i need to split those cause they are longer then the 15 min allowed. Apr 17, 2011 at 17:24
  • then you want alex's answer below...
    – Slipstream
    Apr 17, 2011 at 22:28
5

I thought it may be helpful to go beyond the original question of "how do I properly split an mp4 file?" by pointing out how to do it efficiently. For a large video, it may be especially helpful, as I found out on a 13.7 GB file when following Alex's answer above.

The trick is to use the -ss option before the -i so that ffmpeg skips over the specified time in the input instead of seeking through it. See the ffmpeg documentation https://www.ffmpeg.org/ffmpeg.html#Main-options for more details, but the relevant explanation is:

-ss position (input/output)
When used as an input option (before -i), seeks in this input file to position. 
...
When used as an output option (before an output url), decodes but discards input until the timestamps reach position.
1

The easiest and foolproof method is to use MP4Box:

  1. # apt install gpac
  2. $ MP4Box -split 15 /home/asafche/Videos/myVideos/MAH00124.MP4 (the parameter is time in seconds). For other split possibilities, for example by size see https://gpac.wp.imt.fr/mp4box/mp4box-documentation/ or MP4Box -h general

Concatenation is similarly simple.

1
  • 1
    This should be marked as the right answer. MP4Box is much simpler and 100x faster than using handbrake which does a re-encoding.
    – Polymerase
    Dec 23, 2022 at 0:20
1

Another open-source GUI tool, that can split videos, and do much more, is handbrake

For example, here I'm splitting my video on 24.53 (mm:ss), and also cropping the frame on the right side: enter image description here

You can see the preview of the output easily - with the "Preview" button.

0

If this is an MP4 part 2, then with a very meticulous approach, you can proceed the file without encoder/decoder. This answer is intended for people with a programming background and an hex editor that want to keep the file ""intact"".

The first part of the file is a mdat, the second is a moov, containing metadata used by the decoder to locate chunks, which contain samples (encoded images and sound, not important to know). Then there are STCO, STTS, STSC, etc. This is called "atoms". They indicate what are the offset, length, etc, of each of the chunks. If you do a clean job, you can make several instances of your video, and different version with different chunks. The file will then be playable in players, and digested on video websites. If you're a coder, then build an MP4 parser or use an existing one to keep track of the parts and their offsets. That will help you to know more about the format, and to rebuild files automatically.

-1

This is the command which worked for me. It allows for precision cutting as well, with times given in HH:MM:SS.NNN format.

ffmpeg -ss 00:00:10.234 -i input.mp4 -t 10 -c:v libx264 -c:a copy out.mp4

I'm on Linux Mint, working with mp4 files which were converted from VOB files (converted with ffmpeg), from an old home camcorder. Can you guess whose GF has a daughter about to graduate?

Found the solution here, courtesy of slhck.

https://superuser.com/a/458804

1
  • I'm uncertain how this answer garnered a downvote. I found this question in the same search which found what worked for me; I shared the solution--giving credit where due. Maybe it was downvoted for the typo I just fixed. Can't please 'em all all the time, I suppose...
    – zedmelon
    Aug 14, 2023 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .