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.1What am I doing wrong and how do I properly split an mp4 file?
28 Answers
With ffmpeg you can split file using the following command:
ffmpeg -i ORIGINALFILE.mp4 -acodec copy -vcodec copy -ss START -t LENGTH OUTFILE.mp4where 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.mp4There 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):
8If 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 !
1Use mkvmerge from the mkvtoolnix package. Use something like
mkvmerge -o outputprefix --split 1G origfile.mp4This would split your file in 1 GB blocks. You can use time-indications as well.
3Not 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 I will explain with an example.a-cin the bracket, because the split created 3 files. in your case you might have to see more.
$ ls -lh
-rwxr-xr-x 1 root root 9.3M 2011-04-17 20:09 file.mp4This shows that I have a single file, of 9.3 MB. To split, I type:
$ split -b 2M file.mp4 part_fileAfter 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_fileaeNotice the two characters appended to the filename.
To get the file back:
$ cat part_filename[start-end][start-end] > file.mp4 3 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 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. 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.
The easiest and foolproof method is to use MP4Box:
# apt install gpac$ MP4Box -split 15 /home/asafche/Videos/myVideos/MAH00124.MP4(the parameter is time in seconds). For other split possibilities, for example by size see orMP4Box -h general
Concatenation is similarly simple.
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:
You can see the preview of the output easily - with the "Preview" button.