Audio / video file converter FFmpeg
1 Introduction
Each user at least once used a computer to watch movies or listen to music. Most of you are aware of the existence of various formats for both audio and video information. 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 an independent format, but a wrapper for packaging media of other formats. Strictly speaking, MKV is also a container based on a binary analogue of the XML language [1] . This allows you to expand the format without losing compatibility with older programs.
The list goes on. Often there is a need to transfer, for example, a film from one format to another. For example, you may need this to watch video on a mobile phone or DVD player that does not support playback of the original format.
In this case, you will find it useful to use a simple but unusually powerful tool - the FFmpeg converter.
1.1 General
Description The description in this section is based on materials from Wikipedia [2] and the help pages of the MAN operating system Ubuntu 10.04 LTS.
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 for many modern users, but the command line allows you not to take system resources to display graphic elements, which speeds up the program.
FFmpeg is the whole system. It includes codecs, a server for streaming broadcasts, a utility for converting video files, and even a simple player. We will now be interested mainly in the capabilities of this system for converting files.
FFmpeg supports most common formats. Since the development of the system has not been stopped so far, it will be difficult to give 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 a video format to an audio track, “capture” streaming video or arrange broadcast broadcasting. The possibilities are huge. In addition, the command line interface has been developed intuitively. So, for example, most formats are recognized by the specified input and output file extensions.
FFmpeg has extensive audio and video processing capabilities. So, for example, you can change the sampling rate for the audio format, movie soundtrack, crop 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 better move on to its study and get to know 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 it yourself. The fact is that the installation for different operating systems is very different. In order for the article to be universal and not contain excessive volume, it contains only a description of the use process.
So, suppose we have a video file film.mkv. This format provides very high image quality, but is demanding on system resources. In addition, mobile devices most often do not support it. There is an 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, you need to go to the directory with the film.mkv file and give the command:
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 don’t advise doing this and that'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 needs to be processed per second for the program that will show your movie or play your sound. By default, the bitrate is set to 200 kb / s. Even if the audio track has 64 kb / s and the frame rate is 15 fps (15 frames per second), it means that about 9 kb or about kilobytes per frame. Even with modern video compression technologies, there is no question of any 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 must be supplemented as follows:
Now the maximum bitrate is 6 times higher than the set "default" and the image quality will increase significantly.
Frame rate is the number of frames that passes on the screen per second. The standard television frequency is 24 fps, i.e., 24 frames are played in one second. Each frame is essentially a picture. The standard frequency for digital video is 15-16 fps. Thus, you can increase the bitrate, but gain in file size by reducing the frame rate. For the human eye, this will be almost invisible, and the video size will decrease by about one and a half times.
To set the frame rate, the previous command needs to be supplemented as follows:
After the command is executed, the film.mp4 file will appear in the directory, the maximum bitrate of which is 1200 kb / s, and the frame rate is 16 fps.
Converting to a lower frequency occurs by removing the "extra" frames. Converting to a higher frequency, presumably, by duplicating neighboring frames. As you understand, such a conversion does not affect the quality, but very much even the size.
And another basic operation: resizing the frame. Such a change is possible by scaling or cropping each frame. Here I will consider only scaling.
Situation: our film has a frame size of 640 × 480. Your mobile phone only plays MP4 format and has a screen size of 320 × 240. In order to change the size of each frame of the film, you need to change the previous command as follows:
The film.mp4 file appears in the directory with a maximum bitrate of 1200 kb / s, a frame rate of 16 fps and a frame size of 320 × 240 pixels. It is not worth increasing the size of frames 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 it is possible). Most films and clips have standard sizes. For example, 320 × 240, 640 × 480, 1024 × 768, 1600 × 1200, and so on. For most of them (possibly for all) there are letter designations. So the previous command can be written as:
The result is completely similar. Remembering the formats (in any case, the basic ones) is quite simple. For example, 640x480 is, as you know, VGA; 320 × 240 is QVGA (i.e., a quarter of VGA). Why a quarter? Mentally draw two lines on the VGA field, dividing them vertically and horizontally in half. You will get four rectangles with dimensions of 320 × 240. That's why a quarter of VGA. Letter designations are given below. The list of formats is not complete, but the remaining frame sizes are just crazy and rarely found. You can find out about them from the FFmpeg man page, although you are unlikely to ever need them. What do you think will happen if you give the system a command: Quite right: the soundtrack will be copied to the film.mp3 file. It's just that 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 media 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, to prepare for writing the film.mkv file to a DVD-ROM, you need to give the command:
As always, the final file appears in the directory, ready for burning to disk. A list of common formats (their name must be substituted into the command instead of dvd) can be found on the program’s help page. The most common ones are
You can set the output file format regardless of the extension. To do this, give the command:
The film file appears in the directory without an extension, but having the 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 to you. For this you need to give the command: The first command displays 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 working with the FFmpeg program. Now you can perform most of the tasks for converting files and those that the average user encounters. If you have any questions, ask them at mailto: mna-norn@mail.ru with the subject “FFmpeg”. Letters from unfamiliar addresses without this subject will be deleted without reading. This article is over. Thanks for attention.
Each user at least once used a computer to watch movies or listen to music. Most of you are aware of the existence of various formats for both audio and video information. 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 an independent format, but a wrapper for packaging media of other formats. Strictly speaking, MKV is also a container based on a binary analogue of the XML language [1] . This allows you to expand the format without losing compatibility with older programs.
The list goes on. Often there is a need to transfer, for example, a film from one format to another. For example, you may need this to watch video on a mobile phone or DVD player that does not support playback of the original format.
In this case, you will find it useful to use a simple but unusually powerful tool - the FFmpeg converter.
1.1 General
Description The description in this section is based on materials from Wikipedia [2] and the help pages of the MAN operating system Ubuntu 10.04 LTS.
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 for many modern users, but the command line allows you not to take system resources to display graphic elements, which speeds up the program.
FFmpeg is the whole system. It includes codecs, a server for streaming broadcasts, a utility for converting video files, and even a simple player. We will now be interested mainly in the capabilities of this system for converting files.
FFmpeg supports most common formats. Since the development of the system has not been stopped so far, it will be difficult to give 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 a video format to an audio track, “capture” streaming video or arrange broadcast broadcasting. The possibilities are huge. In addition, the command line interface has been developed intuitively. So, for example, most formats are recognized by the specified input and output file extensions.
FFmpeg has extensive audio and video processing capabilities. So, for example, you can change the sampling rate for the audio format, movie soundtrack, crop 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 better move on to its study and get to know 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 it yourself. The fact is that the installation for different operating systems is very different. In order for the article to be universal and not contain excessive volume, it contains only a description of the use process.
So, suppose we have a video file film.mkv. This format provides very high image quality, but is demanding on system resources. In addition, mobile devices most often do not support it. There is an 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, you need to go to the directory with the film.mkv file and give the command:
ffmpeg -i film.mkv film.mp4The 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 don’t advise doing this and that'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 needs to be processed per second for the program that will show your movie or play your sound. By default, the bitrate is set to 200 kb / s. Even if the audio track has 64 kb / s and the frame rate is 15 fps (15 frames per second), it means that about 9 kb or about kilobytes per frame. Even with modern video compression technologies, there is no question of any 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 must be supplemented as follows:
ffmpeg -i film.mkv -b 1200k film.mp4Now the maximum bitrate is 6 times higher than the set "default" and the image quality will increase significantly.
Frame rate is the number of frames that passes on the screen per second. The standard television frequency is 24 fps, i.e., 24 frames are played in one second. Each frame is essentially a picture. The standard frequency for digital video is 15-16 fps. Thus, you can increase the bitrate, but gain in file size by reducing the frame rate. For the human eye, this will be almost invisible, and the video size will decrease by about one and a half times.
To set the frame rate, the previous command needs to be supplemented as follows:
ffmpeg -i film.mkv -b 1200k -r 16 film.mp4After the command is executed, the film.mp4 file will appear in the directory, the maximum bitrate of which is 1200 kb / s, and the frame rate is 16 fps.
Converting to a lower frequency occurs by removing the "extra" frames. Converting to a higher frequency, presumably, by duplicating neighboring frames. As you understand, such a conversion does not affect the quality, but very much even the size.
And another basic operation: resizing the frame. Such a change is possible by scaling or cropping each frame. Here I will consider only scaling.
Situation: our film has a frame size of 640 × 480. Your mobile phone only plays MP4 format and has a screen size of 320 × 240. In order to change the size of each frame of the film, you need to change the previous command as follows:
ffmpeg -i film.mkv -b 1200k -r 16 -s 320x200 film.mp4The film.mp4 file appears in the directory with a maximum bitrate of 1200 kb / s, a frame rate of 16 fps and a frame size of 320 × 240 pixels. It is not worth increasing the size of frames 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 it is possible). Most films and clips have standard sizes. For example, 320 × 240, 640 × 480, 1024 × 768, 1600 × 1200, and so on. For most of them (possibly for all) there are letter designations. So the previous command can be written as:
ffmpeg -i film.mkv -b 1200k -r 16 -s qvga film.mp4The result is completely similar. Remembering the formats (in any case, the basic ones) is quite simple. For example, 640x480 is, as you know, VGA; 320 × 240 is QVGA (i.e., a quarter of VGA). Why a quarter? Mentally draw two lines on the VGA field, dividing them vertically and horizontally in half. You will get four rectangles with dimensions of 320 × 240. That's why a quarter of VGA. Letter designations are given below. The list of formats is not complete, but the remaining frame sizes are just crazy and rarely found. You can find out about them from the FFmpeg man page, although you are unlikely to ever need them. What do you think will happen if you give the system a command: Quite right: the soundtrack will be copied to the film.mp3 file. It's just that you can separate the video from the sound.
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 × 4096ffmpeg -i film.mkv film.mp3Often there is a need to convert a video file to a format suitable for recording on standard media (for example, a DVD). Such media 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, to prepare for writing the film.mkv file to a DVD-ROM, you need to give the command:
ffmpeg -i film.mkv -target dvd film.mpgAs always, the final file appears in the directory, ready for burning to disk. A list of common formats (their name must be substituted into the command instead of dvd) can be found on the program’s help page. The most common ones are
vcd, svcd, dvd, dv, dv50, pal- vcd, ntsc-svcd. These designations can be directly substituted in the field of the -targetlast 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 output file format regardless of the extension. To do this, give the command:
ffmpeg -f mp4 -i film.mkv -b 1200k -r 16 -s qvga filmThe film file appears in the directory without an extension, but having the 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 to you. For this you need to give the command: The first command displays a list of available codecs, the second - a list of available formats (including audio).
ffmpeg -codecs
ffmpeg -formats
That's all. You have successfully completed the basic course of working with the FFmpeg program. Now you can perform most of the tasks for converting files and those that the average user encounters. If you have any questions, ask them at mailto: mna-norn@mail.ru with the subject “FFmpeg”. Letters from unfamiliar addresses without this subject will be deleted without reading. This article is over. Thanks for attention.