1. Overview
Splitting an MP4 (MPEG-4 Part 14) file into multiple parts offers several benefits. In particular, some social media platforms and messaging applications allow file sharing but with a restriction on the file size. In such cases, we can split the original MP4 file into multiple parts and share them separately. This way, splitting a large MP4 video can make downloading, uploading, editing, processing, and distributing the file easier.
In this tutorial, we’ll explore three ways to split an MP4 file in Linux. Since we’ve already seen how ffmpeg can split videos, we won’t be discussing that here.
2. Using MP4Box
MP4Box is a video editing tool primarily utilized for working with MP4 files. Additionally, it’s a part of the GPAC module in Linux.
Using the MP4Box tool, we can perform various operations on MP4 files:
- creating MP4 files
- extracting tracks
- editing
- splitting an MP4 file into multiple parts
- inspecting
- adding subtitles
- converting a video file into MP4 format
2.1. Install MP4Box
To install the MP4Box tool in Debian-based systems, first, we install the GPAC package using the apt command from the Linux terminal:
$ sudo apt-get install gpac
Now, let’s check the installation status of the MP4Box tool:
$ MP4Box -version
MP4Box - GPAC version 2.0-rev2.0.0+dfsg1-2
(c) 2000-2022 Telecom Paris distributed under LGPL v2.1+ - http://gpac.io
...output truncated...
At this point, we’ve successfully installed the MP4Box tool in our system.
2.2. Acquire Video Information
Now, the first step is to navigate to the directory where we store the original MP4 file:
$ cd /home/sam/Video/Test/
Before splitting it, let’s see some details about the content and structure of the MP4 file:
$ MP4Box -info sample1.mp4
# Movie Info - 2 tracks - TimeScale 600
Duration 00:00:30.526
Fragmented: no
Progressive (moov before mdat)
Major Brand mp42 - version 0 - compatible brands: mp42 mp41 isom avc1
Created: GMT Fri Aug 7 09:13:05 2015
File has root IOD (140 bytes)
Scene PL 0xff - Graphics PL 0xff - OD PL 0xff
Visual PL: AVC/H264 Profile (0x7f)
Audio PL: AAC Profile @ Level 2 (0x29)
...output truncated...
As we can see, the original MP4 file is approximately 30 seconds long and contains two tracks.
2.3. Split Based on Duration
So, let’s split the MP4 file using a user-specified duration:
$ MP4Box -split 10 sample1.mp4
splitting: file 1 done
splitting: file 2 done
splitting: file 3 done
In this scenario, we split the original MP4 file into three files, each with an approximately 10-second duration. Furthermore, we can change the duration value based on the requirement.
2.4. Split Based on File Size
Next, we split the MP4 file by specifying the file size:
$ MP4Box -split-size 1M -out output.mp4 sample1.mp4
Split by size but output not a template, using output_$num$.mp4 as output
[Reframer] split computed using previous estimation of file size (958996)
splitting: file 1 done
[Reframer] split computed using previous estimation of file size (937382)
splitting: file 2 done
[Reframer] split computed using previous estimation of file size (847287)
splitting: file 3 done
splitting: file 4 done
Here, we get four files, each with a maximum size of 1 MB.
2.5. Split Based on Track ID
Finally, let’s split the MP4 file using the track ID number:
$ MP4Box -split-tracks 1,2 -out output.mp4 sample1.mp4
Splitting tracks: 1,2
Creating output file output_1.mp4
Creating output file output_2.mp4
As we already know, the original MP4 file contains two tracks. Therefore, we get two output files.
3. Using mkvtoolnix
An alternative approach to split an MP4 file is to utilize the mkvtoolnix tool in Linux. It’s primarily developed for handling MKV (Matroska Video) files.
Like the MP4Box tool, mkvtoolnix offers options such as merging files, extracting tracks, splitting files, and batch processing.
First, let’s install the mkvtoolnix toolset on our Debian-based system:
$ sudo apt-get install mkvtoolnix
Once mkvtoolnix is installed, we can verify it through the apt show command:
$ apt show mkvtoolnix
Package: mkvtoolnix
Version: 65.0.0-1
Priority: optional
Section: universe/graphics
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Christian Marillat <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 32.4 MB
Next, we go to the directory where we have our MP4 file. Now, to split the MP4 file, we utilize the mkvmerge command present in the mkvtoolnix toolset.
In this case, we split the MP4 file by specifying the size for each segment:
$ mkvmerge -o output.mp4 --split size:1.5M sample1.mp4
mkvmerge v65.0.0 ('Too Much') 64-bit
'sample1.mp4': Using the demultiplexer for the format 'QuickTime/MP4'.
'sample1.mp4' track 0: Using the output module for the format 'AVC/H.264'.
'sample1.mp4' track 1: Using the output module for the format 'AAC'.
The file 'output-001.mkv' has been opened for writing.
'sample1.mp4' track 0: Extracted the aspect ratio information from the MPEG-4 layer 10 (AVC) video data and set the display dimensions to 640/360.
Progress: 1%
The cue entries (the index) are being written...
The file 'output-002.mkv' has been opened for writing.
The cue entries (the index) are being written...
The file 'output-003.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Multiplexing took 0 seconds.
Although it’s fairly self-explanatory, let’s review both options in the command:
- -o indicates the output
- –split size refers to the maximum size of each segment
After executing the command, we get three files, each with a maximum size of 1.5 MB.
4. Conclusion
In this article, we discussed two utilities to split an MP4 file into multiple parts in Linux.
The MP4Box tool is specifically designed for handling MP4 files. Additionally, we can split an MP4 file based on duration, file size, frame number, and track ID number. Furthermore, the MP4Box tool often exhibits superior performance compared to the mkvtoolnix tool in terms of time taken for splitting.
On the other hand, the mkvtoolnix tool is designed for handling Matroska (MKV) files. Although we can use it to split MP4 files into different segments, the available splitting options are fewer than the MP4Box tool.