What is FFmpeg?
Welcome to my new blog. This article focuses on how to use ffmpeg to alter and process media and audio files.FFmpeg can be used with various web frameworks.
Problem
The post-processing of videos uploaded sometimes requires trimming the video file based on certain time intervals and combining the trimmed files to produce a final result without any data loss. To help with this, we required a tool to support fast trim and merge operations.
Solution
Many tools help in processing media files like FFmpeg, HandBrake, Avidemux, etc. Out of this, FFmpeg is the most popular one since it is open source and very easy to use.FFmpeg has been designed to handle the widest variety of media formats most efficiently with minimum fuss from the user.
What is FFmpeg ? 🤔
FFmpeg is a free and open-source software project consisting of multiple libraries for handling video, audio, and other multimedia files and streams. The command-line ffmpeg tool itself is designed for processing video and audio files. It can decode, encode, transcode, mux, demux, stream, filter and play pretty much anything.
🔗 Download FFmpeg: Link
In this article, our objective will be to :
- Download a video using the wget command.
- Trim a video based on time intervals using ffmpeg tools.
- Combine them to deliver a new output file, all in mp4 format.
Step I ~ Get the file using wget
What is wget?
WGET is a free tool to crawl websites and download files via the command line.
🔗 Install wget: Link
Syntax: `wget "input url" -O "output location"`
Video used in the article ~ Video Link
Output Location: Desktop/ffmpegTrial/video.mp4
wget "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" -O Desktop/ffmpegTrial/video.mp4
Step II ~ Trimming Video
There are various ways to cut/trim a video based on
- Cut using a duration
- Cut using start — end time
- Cut the end of a video
- Cut with re-encoding
In this article, we will learn to trim videos based on specific time intervals without re-encoding.
Trim time intervals : (5–20,25–30,45–55), denoting (start second — end second of the original video)
So, our trimmed videos will be of 5secs,15secs, and 15 secs respectively and the final merged output video will be of 30secs.
✂️ Cut without re-encoding:
Syntax: ffmpeg -i "InputFile" -ss "startTime" -to "endTime"
-c:v libx264 "OutputFile"
If we re-encode the video while cutting, this will give us a frame-accurate cut because FFmpeg will re-encode the video and start it with an I-frame. This will be time-consuming but has its advantages like we can re-encode it at a particular bit rate or improve quality and resolution.
- libx264 is an advanced encoding library for creating H.264 video streams.
✂️ Cut without re-encoding:
Syntax: ffmpeg -i "InputFile" -ss "startTime" -to "endTime" -c copy "OutputFile"
- -ss denotes a particular time of the video from where to start
- -to denote the part where the cut should end.
- -c copy copies the video and audio to the output file without re-encoding them. This won't harm the quality of the original video content and is very fast.
If we specify a endTime
that is longer than the input video, e.g. -to 00:35:00
when the input video is 20 minutes long, the trimmed video will end where the input video ends. If we specify a endTime(-to)
that is smaller than the startTime(-ss)
then the command won't run. We will get the following error: -to value smaller than -ss; aborting.
ffmpeg -i video.mp4 -ss 00:05 -to 00:20 -c copy trims/trim1.mp4
ffmpeg -i video.mp4 -ss 00:25 -to 00:30 -c copy trims/trim2.mp4
ffmpeg -i video.mp4 -ss 00:45 -to 00:55 -c copy trims/trim3.mp4
We have successfully cut the video into three parts based on intervals. Also, to keep in mind, the original video is not overwritten.
Disadvantage: Since the seeking operation jumps between I-frames, it might not accurately stop at the time we requested. It will search for the nearest I-frame and start copying.
Step III ~ Final Step — Merging videos into one
There are two steps to using this command.
1. Firstly, we need to create a .txt file with the names and paths of all the individual files that we wish to concatenate.
2. Then, we need to supply this list to ffmpeg as a command-line parameter along with the concat tool.
Let’s create the file first. Here is an example and let us call the text file fileList.txt
. Each line starts with the keyword file
Now, we will concat the contents of the video files stored in fileList.txt.
Syntax: ffmpeg -f concat -i "fileList.txt" -c copy "mergedFileName"
Since we have trimmed the video.mp4 file based on the time intervals (5–20,25–30,45–55), the duration of mergedVideo.mp4 should be 30secs.
Conclusion
This brings us to the end of this article. We have understood how to download videos using wget and perform processing on them using FFmpeg tools. FFmpeg has endless capabilities and is a great library to have in our video processing toolkit.
Resources
Hope you liked it! Thanks for reading!😄