Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
Video splitting is an important technique for controlling the size and flow of videos. Depending on the use case, we can split a video file based on time, number of frames, or size of chunks.
In a Linux environment, we can split a video into multiple parts using tools like FFmpeg. FFmpeg provides efficient and versatile ways to process multimedia files and is available on a wide range of operating systems like Linux, macOS, and Windows.
In this tutorial, we’ll learn to split a video into multiple parts with FFmpeg.
Before moving forward, let’s make sure that we have all the prerequisites ready, including FFmpeg and a sample video.
To begin with, we verify that we have FFmpeg installed on our system:
$ ffmpeg -version
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
configuration: --prefix=/usr --extra-version=0ubuntu0.22.04.1
...
In case it’s not already available, we can install FFmpeg via the local package manager:
$ sudo apt install ffmpeg
In addition to FFmpeg, we’ll also need a sample video file to split it into multiple parts.
Let’s get a sample video file using the wget command:
$ wget https://sample-videos.com/video123/mp4/480/big_buck_bunny_480p_5mb.mp4
We’ll use the downloaded video file to learn about splitting using FFmpeg.
In general, we split a video into multiple parts to extract smaller chunks of that video.
To begin with, we’ll split a video into multiple parts, each lasting the exact same amount of time:
$ ffmpeg -i big_buck_bunny_480p_5mb.mp4 -acodec copy -f segment -segment_time 10 -vcodec copy -reset_timestamps 1 -map 0 output_time_%d.mp4
This command takes an input video file, big_buck_bunny_480p_5mb.mp4, and splits it into 10-second parts.
Let’s understand each option:
As a result of this command, we get three (3) segments of the input video, big_buck_bunny_480p_5mb.mp4, with an approximate duration of 10 seconds each.
However, it’s common that not all of the resulting segments are exactly 10 seconds. The reason behind this is the complex nature of a video file. Multiple factors are at play here, including keyframe alignment, audio-video synchronization, and variable frame rate.
Therefore, re-encoding the video before the segmentation may serve as a better approach enabling more accurate control over the segment duration.
Re-encoding a video may result in an increase or decrease in size, depending on the quality of the input video and the codec libraries used for re-encoding.
To begin with, we’ll re-encode the video via FFmpeg followed by splitting it into multiple parts:
$ ffmpeg -i big_buck_bunny_480p_5mb.mp4 -c:v libx264 -c:a aac -strict experimental -b:a 192k -force_key_frames "expr:gte(t,n_forced*10)" -f segment -segment_time 10 -reset_timestamps 1 -map 0 output_part_%d.mp4
Firstly, this command re-encodes the given input video with audio and video codecs. Secondly, it forces keyframes at 10-second intervals. Lastly, it segments the video into parts, each lasting for 10 seconds.
Let’s understand the new parameters used in this command:
With this command, the last split part may have a shorter duration than the specified 10 seconds.
Similar to splitting at a regular time interval, splitting at regular keyframe intervals can be tricky. Thus, we’ll split the video file by re-encoding it:
$ ffmpeg -i big_buck_bunny_480p_5mb.mp4 -c:v libx264 -c:a aac -strict experimental -b:a 192k -force_key_frames "expr:gte(n,200*trunc(t/200))" -f segment -segment_frames $(seq 200 200 600 | paste -sd "," -) -reset_timestamps 1 -map 0 output_partf_%d.mp4
This command effectively splits the input video file, big_buck_bunny_480p_5mb.mp4, at every 200 keyframes.
The parameter -force_key_frames “expr:gte(n,200*trunc(t/200))” ensures that a keyframe is forced at every 200 frames. Let’s take a close look at the expression:
With the -segment_frames option, we can specify the frame numbers at which to split the video. In the above command, we used seq to generate a sequence of numbers from 200 to 600 in increments of 200 and the paste command to join the numbers with a comma separator.
We can also manually specify the frame numbers (i.e., 200, 400, 600) instead of generating them using seq in the above command, achieving the same result.
Moreover, we can force keyframes by any frame index that is a factor of the target keyframe interval, in this case, 200.
Since video duration in chronological time can be inconsistent, we can use both seconds and frames as our video measurements.
First, let’s find the duration of one of the first part we split using FFprobe:
$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 output_part_1.mp4
10.000000
Let’s break down this command to understand each parameter:
Similarly, we can check the duration of each of the split parts with ffprobe.
Now, let’s find the total number of frames in one of the keyframe-split videos using ffprobe:
$ ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 output_partf_0.mp4
200
Thus, the first split part has 200 frames. In this command, -select_streams v:0 specifies and selects the video stream with index 0. The -show_entries stream=nb_frames option specifies the information to be displayed, i.e., the total number of frames available in an input video file.
Similarly, we can use ffprobe to find the total number of frames in each of the split videos by changing the input file name.
In this article, we learned how to split a video into multiple parts with FFmpeg.
Firstly, we learned how to install FFmpeg and download a sample video file using the wget command. Secondly, we saw how to split a video at regular time and keyframe intervals. Lastly, for accurate splitting, we practiced splitting a video by re-encoding the input video.
Moreover, we used FFprobe to extract video information like the duration and the total number of frames in one of the split videos.