Categories: Ubuntu

How to Convert Videos using FFMPEG in Ubuntu

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
Sponsored
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:

sudo apt-get install ffmpeg

The apt-get install command will work for all versions of Ubuntu except 14.04.

Audio Conversion

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.

ffmpeg -i originial_file.mp3 converted_file.mp4

You can add your own file names and types to this line of code. This is only the basic syntax for usage.

Video Conversion

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.

ffmpeg -i original_file.mp4 converted_file.webm

Photo Conversion

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:

ffmpeg -i original_file.jpg converted_file.png

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:

ffmpeg -i video.mp4 output_file.jpg

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:

ffmpeg -i video.mp4 output_file%d.jpg

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.

ffmpeg -i video.mp4 -vf fps=1 output_file%d.jpg

Likewise, by defining the fps value to 1/6000, the following command will generate 1 image per 6,000 seconds of video:

ffmpeg -i video.mp4 -vf fps=1/6000 output_file%6d.jpg

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:

ffmpeg -ss 00:00:01 -t 00:00:04 -i video.mp4 output_file%3d.jpg

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!

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p output_file.mp4

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.

Sponsored

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.

ffmpeg -i video.mp4 -c:a libmp3lame -q:a 0 -map a output_file.mp3

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:

ffmpeg -i video.mp4 -ss 00:00:08 -t 00:00:10 -q:a 0 -c:a libmp3lame -q:a 0 -map a output_file.mp3

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.

ffmpeg -i video.mp4 -filter:v “crop=w=width:h=height:x:y” output_file.mp4

For example:

ffmpeg -i video.mp4 -filter:v “crop=w=550:h=200:x=100:y=200” output_file.mp4

As an example, I took my own picture and tried to crop it with the following code:

ffmpeg -i minions.jpg -filter:v “crop=w=500:h=200” out.jpg

Trim Videos

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.

ffmpeg -i video.mp4 -ss 00:00:20 -t 00:00:10 -c:v h264 -c:a aac output_file.mp4

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.

Resize Video

Resizing videos is just as easy as trimming and cropping videos.

ffmpeg -i video.mp4 -vf scale=320:240 output_file.mp4

Alternatively, you can also do the following:

ffmpeg -i video.mp4 -vf scale=“iw/1:ih/2” output_file.mp4

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?

ffmpeg -i video.mp3 -filter:a “volume=2” output_file.mp3

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!

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 output_file.mp4 -f dshow -i audio=“Stereo Mix (Realtek Audio)” output_file.mp4

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:

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -qp 0 output.mp4

Conclusion

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.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

13 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

2 days ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

3 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

4 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

4 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago