1. Introduction
Each user at least once used the computer to watch movies or listen to music. Most of you are aware of the existence of various formats, both audio and video. Each format is designed for its purpose.
So MP4 is convenient for playback on mobile devices due to the high degree of compression. MKV format files are huge, but at the same time high-quality video and sound. The AVI format is not even a standalone format, but a wrapper for packaging media data of other formats. Strictly speaking, MKV is also a container based on the binary analogue of the XML language
[1] . This allows you to expand the format without losing compatibility with old programs.
The list can be continued for a long time. Often there is a need to translate, for example, a film from one format to another. For example, this may be needed to watch videos on a mobile phone or DVD player that do not support playback of the original format.
In this case, you can use a simple, but at the same time extremely powerful tool - FFmpeg converter.
1.1 General characteristics
The description in this section is based on Wikipedia
[2] and the MAN help pages for the Ubuntu 10.04 LTS operating system.
So, FFmpeg is a set of open source libraries. The project was created by Fabrice Bellar. Since the source code is open, the program can be compiled (created) for any operating system. The FFmpeg utility is the interface for these libraries. It works from the command line. This may not seem convenient to many modern users, however, the command line allows you not to take system resources away to display graphic elements, which speeds up the work of the program.
FFmpeg is a whole system. It includes codecs, a streaming server, a utility for converting video files, and even a simple player. We will now be interested mainly in the file conversion capabilities of this system.
FFmpeg supports most common formats. Since the development of the system has not been stopped until now, it will be difficult to provide a complete list of them. Of course, FLV, AVI, MP4 are among them. For all the time I could not find a single format that would not be supported in FFmpeg.
In addition to the ability to convert files to different formats, FFmpeg can convert video format to an audio track, “capture” streaming video, or arrange a broadcast broadcast. The possibilities are enormous. In addition, the command line interface was designed to be intuitive. For example, most formats are recognized by the specified extensions of the input and output files.
FFmpeg has a wide range of audio and video data processing capabilities. For example, you can change the sampling rate for the audio format, the sound track of the film, trim or scale frames.
FFmpeg can be used for streaming video or audio converter. You can continue to list the advantages of the system, but let's move on to its study and get acquainted with them along the way.
')
1.2 First steps
Any road begins with the first step. So let's get started. Immediately make a reservation: I assume that the FFmpeg utility is already installed on your system. If this is not the case, you will have to take care of this yourself. The fact is that the installation for different operating systems is very different. In order for the article to be universal and not to contain an extra volume, only the description of the process of use is contained here.
So, suppose we have a video file film.mkv. This format provides a very high image quality, but is picky about system resources. In addition, mobile devices often do not support it. There is a MP4 format. It compresses video data very well and is supported by most devices with low system requirements. To transfer our file to this format, go to the directory with the film.mkv file and give the command:
ffmpeg -i film.mkv film.mp4
The film.mp4 file will appear in the same directory. Just right? Most of the settings are set to "default." File formats are automatically detected. But I do not advise doing this, and here's why: modern video data is of very good quality. It depends on many parameters, but, in my opinion, three are decisive: bit rate, frame rate and codec.
Bitrate is the amount of information in bits that must be processed per second to the program that will show your movie or play your sound. By default, the bitrate is set to 200 kb / s. Even if the sound track has 64 kb / s, and the frame rate is 15 fps (15 frames per second), it means that about 9 kb or about kilobyte falls on one frame. Even with modern video compression technology, there is no question of what quality.
As you understand, the size of the final file directly depends on the bitrate. In each case, the bitrate is selected individually and general recommendations cannot be given here. To set the maximum bitrate, the command needs to be added as follows:
ffmpeg -i film.mkv -b 1200k film.mp4
Now the maximum bitrate is 6 times higher than the set “default” and the image quality will increase very noticeably.
The frame rate is the number of frames that passes on the screen per second. The standard television frequency is 24 fps, which means that 24 frames are played in one second. Each frame is essentially a picture. The standard frequency for digital video is 15-16 fps. Thus, it is possible to increase the bit rate, but to win in the file size by reducing the frame rate. For the human eye, this will be almost not noticeable, and the size of the video will be reduced by about one and a half times.
To set the frame rate, the previous command must be added as follows:
ffmpeg -i film.mkv -b 1200k -r 16 film.mp4
After executing the command, the file will appear in the directory film.mp4, the maximum bitrate of which is 1200 kb / s, and the frame rate is 16 fps.
Conversion to a lower frequency occurs by removing the "extra" frames. Transformation to a higher frequency, presumably, by duplicating adjacent frames. As you understand, such a conversion does not affect the quality, but the size is very even.
And one more basic operation: resizing a frame. Such a change is possible by scaling or cropping each frame. Here I will only consider scaling.
Situation: our film has a frame size of 640 Ă— 480. Your mobile phone plays only MP4 format and has a screen size of 320 Ă— 240. In order to change the size of each frame of the film, the previous command needs to be changed as follows:
ffmpeg -i film.mkv -b 1200k -r 16 -s 320x200 film.mp4
A film.mp4 file will appear in the directory with a maximum bit rate of 1200 kb / s, a frame rate of 16 fps and a frame size of 320 Ă— 240 pixels. Increasing the size of frames is not necessary for the same reasons as their frequency.
Thus, you can set any frame size, not even a multiple of the original (this will lead to image deformation, but possible). Most films and commercials have standard sizes. For example, 320 Ă— 240, 640 Ă— 480, 1024 Ă— 768, 1600 Ă— 1200 and so on. For most of them (and possibly for all) there are letter symbols. So the previous command can be written in the form:
ffmpeg -i film.mkv -b 1200k -r 16 -s qvga film.mp4
The result is completely the same. Remembering the formats (at least, the basic ones) is quite simple. For example, 640 Ă— 480 is, as you know, VGA; 320 Ă— 240 is QVGA (i.e., a quarter of a VGA). Why a quarter? Mentally draw two lines on the VGA field dividing them vertically and horizontally in half. It turns out four rectangles, dimensions 320 Ă— 240. That's why a quarter of the VGA. The letter designations are given below. The list of formats is not full, but the other frame sizes are just crazy and are rarely found. You can learn about them from the FFmpeg reference page, although you are unlikely to ever need them.
sqcif - 128 Ă— 96
qqvga - 60 Ă— 120
uxvga - 1600 Ă— 1200
qcif - 176 Ă— 144
qvga - 320 Ă— 240
qxga - 2048 Ă— 1536
cif - 352 Ă— 288
vga - 640 Ă— 480
sxga - 1280 Ă— 1024
4cif - 704 Ă— 576
svga - 800 Ă— 600
qsxga - 2560 Ă— 2048
16cif - 1408 Ă— 1152
xvga - 1024 Ă— 768
hsxga - 5120 Ă— 4096
What do you think will happen if you give the system a command:
ffmpeg -i film.mkv film.mp3
Exactly: the audio track will be copied to the film.mp3 file. So just you can separate the video from the sound.
Often there is a need to convert a video file to a format suitable for recording on standard media (for example, a DVD). Such carriers have a special structure and some limitations. Just writing a file to disk is not enough. To take all this into account, we would have to tell FFmpeg a lot of parameters, many of which neither you nor I even know. Fortunately, the authors of the program took care of this. It is enough to specify one parameter, and the rest are set automatically. So, in order to prepare to burn a film.mkv file to a DVD, you need to give the command:
ffmpeg -i film.mkv -target dvd film.mpg
As always, the final file appears in the directory, ready to be written to disk. A list of common formats (their name should be substituted into the command instead of dvd) can be found on the program's manual page. The most common of them are
vcd, svcd, dvd, dv, dv50, pal- vcd, ntsc-svcd
. These symbols can be directly substituted into the
-target
field of the last command. If your operating system is not sensitive to the extension (you have Linux, for example), the file extension does not matter.
You can set the format of the output file regardless of the extension. To do this, give the command:
ffmpeg -f mp4 -i film.mkv -b 1200k -r 16 -s qvga film
A film file without an extension will appear in the directory, but in MP4 format. The names of the formats coincide with the extensions for the files of these formats.
The article mentioned codecs and file formats. You can find out which ones are available for you. For this you need to give the command:
ffmpeg -codecs
ffmpeg -formats
The first command will display a list of available codecs, the second - a list of available formats (including audio).
That's all. You have successfully completed the basic course of work with the FFmpeg program. Now you can perform most of the tasks of converting files and those encountered by the average user. If you have any questions, ask them at mailto: mna-norn@mail.ru with the subject “FFmpeg”. Letters from unfamiliar addresses without this topic will be deleted without reading. This article is over. Thanks for attention.