A Practical Guide Of FFmpeg – Part 1

FFmpeg is a great multimedia framework that helps you deal with your audio and video files. What do I mean? You can easily convert from one format to another, extract audio from a video, compress a video and even extract pictures from a video. There are many cool things you can do with this tool. It is written mostly in C programming language and the good thing is that FFmpeg is open source. You can go on GitHub, search for the project, download the code and study it yourself. A little hacking never harmed anyone.

Download And Install FFmpeg

Installing FFmpeg on your Linux platform is not that hard. The important thing is to know a couple of commands and run them. But even if you have no idea about software installation from the command line do not worry. Unixmen has the right recipes to help  you install this multimedia framework on your system.

First let me teach you guys to compile and install FFmpeg from source. What does it mean to install software from source? It means to download the code of the tool you want to install on your system, compile it and finally do an install. There are certain steps you need to follow when installing software from source.

I love keeping things practical. So, open a new console on your Linux distribution and use the following command to download the source of FFmpeg framework from Github.

wget https://github.com/FFmpeg/FFmpeg/archive/master.zip

Then unzip the zip archive with the following command.

unzip master.zip

Once the archive is extracted run the following command.

./configure
make

Then install it on your system with the following command.

su -c 'make install'

Once the installation is finished you can do a simple test by running the following command on your console. If it does not print any error it means the installation has been completed successfully.

ffmpeg

There are many other ways to install FFmpeg on your system based on the type of distribution you are running. But this article does not cover the installation for each distribution as it focuses more on the practical  usage  of FFmpeg rather than the installation of it on Linux distributions.

Compiling and installing from source should work for anyone so it is ok.

Get Information About A Video

When you do stuff with videos such as converting them, editing or hacking deep on them you for sure need to do a probe on the video file in order to analyze it at a lower level. To do this, the FFprobe tool is used. According to the official documentation, this tool  gathers information from multimedia streams and prints it in human- and machine-readable fashion. In case you want to know about streams and formats on a video or audio file this is the right tool for the job.  You just do the following.

ffprobe -show_format -show_streams losemyself.mp4

When running the above command the following output is printed on my console.

[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
codec_type=video
codec_time_base=1/60
codec_tag_string=avc1
codec_tag=0x31637661
width=640
height=360
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=yuv420p
level=30
timecode=N/A
is_avc=1
nal_length_size=4
id=N/A
r_frame_rate=30/1
avg_frame_rate=30/1
time_base=1/30
start_time=0.000000
duration=276.500000
nb_frames=8295
TAG:language=und
TAG:handler_name=VideoHandler
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=Advanced Audio Coding
codec_type=audio
codec_time_base=1/44100
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=s16
sample_rate=44100
channels=2
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/44100
start_time=0.000000
duration=276.503220
nb_frames=11908
TAG:creation_time=2015-03-06 09:05:03
TAG:language=und
TAG:handler_name=
[/STREAM]
[FORMAT]
filename=losemyself.mp4
nb_streams=2
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime/MPEG-4/Motion JPEG 2000 format
start_time=0.000000
duration=276.501667
size=20421136
bit_rate=590843
TAG:major_brand=mp42
TAG:minor_version=0
TAG:compatible_brands=isommp42
TAG:creation_time=2015-03-06 09:05:01
[/FORMAT]

Extract Audio From MP4 Video

When downloading a music video usually you want to import it on your Ipod so you can listen to it  but the thing  it is a very big file. So what do you do? You make use of the FFmpeg framework. It has some nice commands for extracting the audio from a video. I have the following music video on my Desktop.

losemyself.mp4

The size of the  file listed above is 20.4 MB. It is a video containing some great music. I just want to extract the audio and leave the video alone. The following command will do that for me.

ffmpeg -i losemyself.mp4 -vn -acodec copy losemyselfaudio.mp4

I know you are wondering what does each option in the above command mean. Let me explain it to you in very human readable words. the option -i stands for input file, the option  -vn  means do not include the video in the output, -acodec means audio codec and copy stands for copying the audio.

Very simple and easy to understand even for someone who has no idea about audio and video. You can also do the following if you want to save the audio file as mp3.

ffmpeg -i losemyself.mp4 -vn -acodec copy losemyselfaudio.mp3

The above command will save the audio file as mp3. As you can see from the two commands the extension of the output file is very important when it comes to the results. Different extension, in this case, means different format for the audio. Both cases work for us. It is up to you to save the file either as mp4 or mp3.

Convert MP4 Video To AVI

Basic conversion of a video from one format to another is an easy thing to do with FFmpeg framework. The only thing you have to know is the extension of the video output you want. For example, I can convert my mp4 video to avi using the following command.

ffmpeg -i losemyself.m4 losemyself.avi

So easy! You can also convert the video back to mp4 using the following command.

ffmpeg -i losemyself.avi losemyself.mp4

The only thing you should care in this case is the extension of the file. The option -i should be used to specify the input file in the same way we did on extracting audio from mp4 video.

Extract A Specific Image From A Video

Sometimes you like an image on a video you are watching so much that you want to have it as a  single file your computer. And the thing is you don’t know how to get it. It is not an easy task to do unless you have the right command for it. The following command will extract a single image from a specific video and save it on your local machine.

ffmpeg -ss 13 -i losemyself.mp4 -t 1 -s 640x360 -f image2 losemyself.jpg

I know that  I have to explain every option for you guys except the option -i. The option -ss stands or starting second. This option helps to tell FFmpeg where to start the extract. The option -t is used for the number of frames to be extracted from the video. In  my case, it is limited to one frame. Then there is the  option -s which is used to tell the size of the output image file. I have used 640×360 for my case. And the final option is -f which forces the  format.

Remove Audio From Video

There are many reasons why you want to remove audio from a video. Sometimes you may need a silent video for demonstration purposes or a demo. In this case, FFmepg can help you to remove the audio part from your video file. The following command does the job for you.

ffmpeg -i losemyslf.mp4 -an -vcodec copy losemyselfsilent.mp4

The input file in the above case is losemyself.mp4.  The video is going to get copied without audio on the losemyselfsilent.mp4 file. Not that the above command is not going to overwrite the original file.

Conclusion

In this firs part of Practical FFmpeg On Linux series, we took a look at some practical and very useful FFmpeg tools that help Linux users in their daily life while working with video and audio on their machines. In the second  part, we will take a look at some other commands that are also very practical and easy to understand by anyone.

Make sure to practise the commands if you want to improve your FFmpeg skills.

  • N_mag

    Very cool thanks, however… where’s part 2?