In the modern world, with YouTube and other social media apps as the mainstream way to view digital media, uploading videos has become a common practice. It has become so common, in fact, that creating and editing audio and video files have become the new normal in the span of only a few decades. There are obviously expensive tools out there that one can buy to fulfill one’s needs, but what if we could do this for free? What if a few lines of code could do almost any video-related task that you require?
Ffmpeg is a free and open-source video conversion, extraction, and editing tool that provides an answer to this question! What can ffmpeg not do? Almost nothing. The tasks that can be performed with the aid of ffmpeg include audio file conversion, video file conversion, photo conversion, image file extraction from video files, generating videos from image files, extracting audio from video, cropping videos and photos, trimming videos, re-sizing videos, boosting volume, and creating screen recordings.
Installing FFmpeg in Ubuntu
Ffmpeg does not come pre-installed in Ubuntu by default, so you will first have to install this program to use it. To complete the installation, you must be the root user. Next, type the following to install FFmpeg:
The apt-get install command will work for all versions of Ubuntu except 14.04.
Converting an audio file’s filetype can be performed in a single line of code using the ffmpeg utility. In this case, you only need to pass two parameters: the name of the original file and the name of the new file. This command allows you to convert any file format to any other file format by automatically detecting the file types by name. As such, there is no need for additional code to specify the file types.
You can add your own file names and types to this line of code. This is only the basic syntax for usage.
You can also convert video file types using ffmpeg. When inputting the command, all you need is the name of the original file and the name of the output file. The -i tag denotes the input file.
What is true for video and audio files applies to image files, as well. You can convert one photo format to another format quickly and easily with ffmpeg simply by using the syntax shown below:
Extract Image from Video File
Ffmpeg can also be used to extract images from video files. In other words, this program can take screenshots at precise times during a video and save the screenshots as a collection of image files. For example:
In this case, the -i tag depicts the video file from which the image file(s) will be extracted. However, we still have not given the program any specifications as to the image quality or screenshot timing. So, it is first necessary to specify these details before inputting the code.
If we write the following:
The %d value following the name of the output file represents a variable that you can customize according to the number of frames per second you would like to generate from the video. When the pictures are generated at one frame per second, each photo will be labeled output_file1.jpg; at two frames per second, output_file2.jpg; at three frames per second, output_file3.jpg; etc. If the movie has 35 frames per second, and it is one second long, then giving the %d variable a value of 1 will generate 35 photos.
Next, what if we want to capture the images at a certain frames-per-second rate? The fps value defines this in the ffmpeg syntax. To capture 1 frame per second, you would set the fps value equal to 1.
Likewise, by defining the fps value to 1/6000, the following command will generate 1 image per 6,000 seconds of video:
In the above command, the term %6d will generate a variable with six digits. The output files will be labeled as follows: output_file000001.jpg, output_file000002.jpg, etc.
Now, suppose that you do not want to capture all the frames. Instead, you want to be more selective and wish to capture a set number of images between two timeframes. Then, the syntax would look something like this:
This code will capture images, starting at 00:00:01, for 4 seconds. In other words, the image capturing will start at 00:00:01 and end at 00:00:05 in the video. As usual, the number in the name of the output file will have three digits (i.e., output_file001.jpg, output_file002.jpg, etc.).
Generate Video from Image Files
What if you want to do just the opposite and put images together to form a video? Ffmpeg comes to the rescue once again!
After reading the previous sections, you now know what the term img%03.png stands for – the source image files that to compile into the video. In this syntax, the -c:v tag represents the codec for the video, fps represents the frames per second value, and r represents the framerate of the output video.
Extract Audio from Video
What about extracting audio files from a video file? There are tools on the web that you can use to perform this task, but with ffmpeg, one line of code can do the job just as well.
In the above syntax, video.mp4 is the input video file and output_file.mp3 is the output audio file. The -map a tag takes the default audio stream and excludes any subtitles, c:a specifies the codec for the audio to be used, and q:a defines a variable bitrate for the audio.
If you would like to extract only a portion of the audio file from a video file, you can use the following syntax to do so. In the following code, only the section of audio from between 00:00:08 and 00:00:10 is extracted:
Obviously, there are many more details you can add, should you wish, but the above example is a great starting point.
Crop Videos and Photos
What if you want to crop videos? You can either take the time and money to find cropping software elsewhere, or you can use one line of code with ffmpeg to crop the video to size.
As an example, I took my own picture and tried to crop it with the following code:
Trimming video files requires cutting them from one specific time to another specific time. For example, cutting a video file into two or three segments would be trimming it. Once again, trimming video or audio files can be easily done using ffmpeg.
In this case, the -ss tag stands for start seeking, or the time at which to begin the trimming process. In the above command, we will start cutting or trimming at 00:00:20. The t tag stands for the duration of the clip. Here, the duration of the cut clip will be 10 seconds. Finally, the c:v tag is for the codec of the video used, while c:a is for the audio codec used.
Resizing videos is just as easy as trimming and cropping videos.
Alternatively, you can also do the following:
Here, the value iw defines input width, while ih defines input height. The latter will also scale it.
Boost Volume of Video
In this article, you have seen how to crop, trim, and convert video and image files, but what about boosting the volume of audio and video files?
In the latter case, we are humbly asking ffmpeg to double the volume of the file.
Create Screen Recording
Recording your screen is a task that is frequently necessary to perform, whether it be for presentations, video sharing, or online meetings. Whatever the reason for recording your desktop, typically, you would need to run some kind of software to do so. Unlike costly alternatives, ffmpeg can do this for you for free!
In the above command, the -f tag represents the format of the video recording of your screen. Following the audio value, you will put in your audio source.
In addition, the following code will only grab the video of the screen recording, without any sound added:
Overall, ffmpeg is an extremely useful program that allows you to accomplish quite a lot of things in a single line of code. You can buy expensive tools online to fulfill your editing needs, or you can try these functions out for free by installing the ffmpeg utility.