Split Videos into Smaller Files using File Size with FFmpeg
In this article, we will discuss how to split large video files into smaller ones using the FFmpeg tool based on file size. This can be useful when you have a large video file, for example, 1.92GB, and you want to split it into multiple smaller files, each 200MB in size. We will cover two methods to achieve this: losslessly and with some quality loss.
Lossless Split
To split a video file into smaller files while preserving the original quality, you can use the -f segment option in FFmpeg. Here's an example command:
ffmpeg -i input.mp4 -c copy -f segment -segment_time 00:00:30 -segment_format mp4 output%03d.mp4In this command:
-i input.mp4: specifies the input file-c copy: copies the input codecs to the output without re-encoding-f segment: specifies the format of the output file-segment_time 00:00:30: sets the segment time to 30 secondsoutput%03d.mp4: sets the output file name pattern
However, this method does not allow you to split the video based on file size. To achieve this, you can use a script to monitor the file size of the output segments and stop the encoding process when the desired file size is reached. Here's an example script in bash:
#!/bin/bash
FILE\_SIZE=200000000 # 200 MB in bytes
SIZE\_REACHED=0
COUNT=1
while [ $SIZE\_REACHED -eq 0 ]
do
ffmpeg -i input.mp4 -c copy -f segment -segment\_time 00:00:30 -segment\_format mp4 output$COUNT.mp4
SIZE=$(du -b output$COUNT.mp4 | cut -f1)
if [ $SIZE -gt $FILE\_SIZE ]
then
SIZE\_REACHED=1
else
COUNT=$((COUNT+1))
fi
done
Split with Quality Loss
If you are willing to accept some quality loss in the output files, you can use the -fs option in FFmpeg to split the video based on file size. Here's an example command:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k -fs 200000000 -f segment output%03d.mp4In this command:
-c:v libx264: specifies the video codec-crf 23: sets the Constant Rate Factor for the video codec (a lower value means better quality but larger file size)-c:a aac: specifies the audio codec-b:a 128k: sets the audio bitrate-fs 200000000: sets the maximum file size for the output segments
- To split a video file into smaller files while preserving the original quality, you can use the
-f segmentoption in FFmpeg and monitor the file size using a script. - To split a video file into smaller files with some quality loss, you can use the
-fsoption in FFmpeg.
References
- FFmpeg documentation: https://ffmpeg.org/documentation.html
- Bash script to split video based on file size: https://superuser.com/questions/1135045/split-video-into-equal-sized-chunks-using-ffmpeg