📜 ⬆️ ⬇️

Creating a video editor - useful avconv recipes (ffmpeg)



Recently it took to write a small video editor with a web interface.
Prior to that, occasionally it has been possible to use commands like
ffmpeg -i file.avi file.mp3 

mainly for converting from one format to another. Everything has always been more or less smooth and difficult to imagine how many different nuances actually exist for working with video and audio.
But let's start from the beginning. For some time, my ubuntu began to issue:
 *** THIS PROGRAM IS DEPRECATED *** This program is only provided for compatibility and will be removed in a future release. Please use avconv instead. 

In general, while it was used in small things, it was not particularly important, but to put the already obsolete feature into the project is somehow “not it”. I had to google what was happening and it turned out that the ffmpeg project had split up some time ago and some developers started creating the libav library, which is currently included in ubuntu by default. Of course, the compatibility of advanced features was sacrificed first. At the same time, and with the renaming of the project, the ffmpeg executable file was renamed to avconv, which was the above warning.

Under the cut a small squeeze of the main features that are useful.

')
 avconv [  ] -i   [ ]   

There may be several incoming files, respectively, and -i options are written before each of them

But in fact, everything turned out to be a little more complicated.
Further, a set of useful recipes and in the course of the description a small comment about the options

The main options that are often found in teams

-y - overwrite the file without question
-threads 8 - how many threads to perform the operation (in this case, 8, but not all codecs can parallelize)
-s hd720 - video size (in this case, the standard hd720p-1280 * 720)
-q 1 - sets the quality of coding (on different codecs acts differently). Do not forget this option, because by default, only the name of the quality.
For options that set the properties of a stream, it is possible to specify this stream directly in the option itself (without this, the option's effect applies to all streams). This is done as follows:
-q:a 1 - set the audio quality,
-q:v 1 - respectively - video.
With the number will be implied flow number
-q:v:0 1 - the first video stream (counting from zero),
-q:0 1 - the first stream in general, depends on the layout of the file - you must first look at the information that avconv gives relative to the contents of the input files.

Cutting a large file into fragments

 avconv -i in.mp4 -ss 00:00:30 -t 00:10:00 -q 1 -s hd720 -threads 8 -r 25 -y out.mp4 

We -ss 00:00:30 from what second -ss 00:00:30 and what's the length of time -t 00:10:00 to take the video.

Separate audio from video at the right volume and length.

 avconv -i in.mp4 -ss 00:00:22 -t 00:00:30 -vol 512 -vn -f u16le -ac 2 -ar 44100 -threads 8 -y out.raw 

-vn - exclude video stream on output
-ss 00:00:22 - the beginning of the cut fragment
-t 00:00:30 - the length of the cut fragment
-vol 512 - volume, 256 is the normal volume, all that rises higher and below, respectively
-ar 44100 - audio sample rate (audio stream sample rate) 44100 complies with cd quality and is standard
-ac 2 - sets the number of audio channels (2 - means stereo)
-f u16le - sets the format of the output stream, u16le - saves raw data in a file without headers.
You can export data in a different format, but raw is useful to us if we want to concatenate many audio fragments, which will be easy to accomplish simply by merging the files one by one.

Replicating or cloning files

If you need to connect audio tracks, you can use a simple cat
 cat in1.raw in2.raw > out.raw 

Similarly, you can connect as many files as you like and you can take portions in several passes, creating intermediate ones.
 cat in1.raw in1.raw in1.raw > out.raw 

Thus, we can replicate a fragment (for example, silence or a single-color signal) for the time we need, or loop a small audio fragment.

Convert raw format to normal wav

 avconv -q 1 -f u16le -ac 2 -ar 44100 -threads 8 -i in.raw -y out.wav 

Pictures

Split video file into images
 avconv -i in.avi -q 1 -s hd720 -threads 8 -y "out/%08d.jpg" 

At the exit, we get a set of frames that make up the video.

Collect video file from images
 avconv -i "in/%08d.jpg" -q 1 -s hd720 -threads 8 -y out_v.avi 

Clone images in the right quantity
For example, we have a picture and we need it to be in the video for half a minute. The option -loop basically serves for this -loop
 avconv -loop 1 -i in.jpg -t 00:00:05 -q 1 -s hd720 -threads 8 -y out.mp4 


Taking a screenshot
 avconv -i file.mp4 -an -ss 00:00:30 -r 1 -vframes 1 -y -f mjpeg -q 1 file.jpg 

-f mjpeg - specify the codec for this operation
Alternative way to mix audio tracks

For audio processing, there is a good, time-tested tool, sox . Surprisingly, it was possible to merge two audio tracks after a series of unstable results with avconv without any surprises. For example, avconv can merge two tracks that are identical in time, and at the exit one of them will complete earlier than the second. Accordingly, the sound can “move out” strongly when applied, especially on long rollers.

 sox -m in1.wav in2.wav out.wav 

-m - option specifying the mixing mode of input files. Without the options, sox simply scans the files, adding the second after the first.

Merging sound and video into one file

 avconv -i in.mp4 -i in.mp3 -c:v copy -q 1 out.mp4 

-c - select codec, respectively
-c:a - audio codec
-c:v - video codec
-c:v copy - Do not recode using original data

Concatenation option

 avconv -i "concat:in1.avi|in2.avi" -q 1 -b:a 128k -preset libvpx-720p -threads 8 -y {f_out} 

-b - select bitrate
-b:a 128k - we set a bitrate of 128 kbps for mp3 tracks
-preset - immediately sets the settings from the predefined
-preset libvpx-720p - installs the vpx video codec and frame size according to the hd720p standard
concat: is a special “format” of input files with which lists are set. In practice, it works in the same way as cat in1.avi in2.avi connected files. Correspondingly, like cat, it will put the files together, but the codec that parses them may not understand (this is what happens for most formats) and you need to choose a format that supports such a connection. Of these formats, it is worth mentioning mpeg, avi and raw video data, which could be devoted to a separate article and will not be dealt with here.

Build tracks from different files

 avconv -i in1.mp4 -i in2.mp4 -map 0:a -map 1:v -y -q 1 out.mp4 

-map - indicates the source track to be included in the output file (if no map is specified, avconv will simply convert the first file to the output file format).
-map 0:a - audio track from the first file (counting from zero).
-map 1:v - video track from the second.

New in version 0.9 -filter_complex

This option will determine the order of filters for complex (complex) operations.
It normally works with files when they have the same frame rate.

The filter is described by a string that looks like this:
[in1] [in2] convert [out]

At the entrance - this is, in fact, the same thing that we write in -map, for example [0: a] [1: a] - audio channels of the first and second videos.
At the output, we give the name to the newly created channel after the completed conversions, for example, mix_a. It can also be used in the map in the future or as a stream name at the input of the next transformation.

By default, all incoming streams (or the first stream, if the filter accepts only one) are included in the conversion, so they can be omitted. The resulting channel becomes the default channel for the resulting file, so in most cases, it can also be dropped.

Transformation operations can be divided into “generators” - they don’t accept anything at all, they only create a stream with specified properties, »filters’ - take one stream at the input and convert it and mixers ’- take several streams at the input and connect their.

There may also be several streams at the output, for example, if the filter breaks stereos into two mono tracks, they also need to be described with a list [out1] [out2], etc.

Generator example

 avconv -filter_complex 'color=white' -t 5 -q 1 out.mkv 

color=white - generates an image with pure white. This can be used, for example, to create a background for subtitles or as a pause between video clips.

Video Filter Examples

 avconv -i in.avi -vf "hflip" out.avi 

hflip - mirror image horizontally

 avconv -i in.avi -vf "smooth=type=blur" -q 1 out.avi 

smooth - performs image blurring using one of several algorithms: “blur”, “blur_no_scale”, “median”, “gaussian”, “bilateral”.

 avconv -i in.avi -vf "scale=w=200:h=100" -q 1 out.avi 

scale - performs image scaling. On his example, we can consider how parameters are passed to filters. “Scale = ...” - if after the filter name, as in this example, an equal sign is indicated, then a list of parameters of the form “name = value” should follow. The parameters are also indicated with an equal sign, therefore, initially it is somewhat confusing, but you can get used to it. The parameters themselves in the list are separated by colons.
w=200:h=100 - rigidly sets the height and width of the output image. Of course, with the hard way of setting the size, the image is obtained in a controlled size, but the proportions can be distorted.
scale=w=iw/2:h=ih/2 - the size of the output image can be specified depending on the input. In this case, we specify to halve. By fitting the coefficients accordingly, you can adjust the proportions. This, by the way, allows you to achieve interesting effects, for example, a reduced image can be inserted as additional information into a corner or you can combine several video images as is done on video surveillance systems.
ow=200:oh=200 - cuts the image to the specified size
You can also use more complex expressions and functions in calculations.

Mixing examples

 avconv -i in1.mp4 -i in2.mp4 -filter_complex amix=inputs=2 -q 1 -b:a 128k -preset libvpx-720p -threads 2 -y out.mp4 

amix=inputs=2 Mix audio streams. Here we indicate how many streams at the input and discard the listing itself.
Amix also has a useful duration parameter, which allows you to adjust the length of the output stream and can take one of three values:
- longest - by the duration of the largest mixed audio stream,
- shortest - respectively, the shortest,
- first - by the length of the first stream listed.

The picture is superimposed on the video.

 avconv -i video.mkv -i logo.png -filter_complex overlay=x=10:y=main_h-overlay_h-10 -q 1 out.mkv 

overlay - allows you to overlay one stream over another. You can also use a picture as a stream. In the example above, we set the logo to be indented to the left and bottom by ten pixels. To calculate the indentation below, we subtract the height of the superposed stream from the height of the main stream and additionally subtract the indent itself.

In this example, we can demonstrate the use of several filters together. To separate the list of filters, use ";".

 avconv -i in1.avi -i in2.avi -filter_complex "[1:v]scale=w=iw/3:h=ih/3[v_small];[0:v][v_small]overlay=x=main_w-overlay_w-main_w/20:y=main_h-overlay_h-main_w/20" -q 1 out.avi 

We take the second image, reduce it three times and place it in the lower right corner, making indent from the edge equal to the horizontal and vertical one-twentieth width of this image.

PS I would like to say at the end that the final version was very different from how everything was seen at the beginning. Last but not least, many bugs affected this even in the most recent version. As a stable version is offered from the branch 0.8, but there, unfortunately, there are no many interesting features. On the other hand, avconv from branch 0.9 is very unstable and contrived, in already seemingly tested places, on some input files to eat all the RAM in a few seconds and hang the server. Of course, the use of memory can be limited to the current user, but this still does not solve the problem, since avconv in any case falls out with an error. Sometimes individual videos from the video series disappeared, in some cases, the time on the audio track and video sequence unexpectedly diverged - you had to look for other tools and other ways to perform the same tasks.

UPD: Apply one image to the sound:
 ffmpeg -r 1 -loop 1 -i aa.jpg -i aa.mp4 -acodec copy -vcodec mjpeg -t 326 -q 1 -y a.mp4 


Who does not have avconv 0.9 in the system
1) clone git and configure
 git clone git://git.libav.org/libav.git; ./configure 

2) Compilation is better to specify in several streams so that it is faster. Install is better not to do so that later nothing had to be restored in the system.
 make -j 8 

If there is no codec:
  ./configure --help 

And there just to see what --enabled for example ./configure --enable-vdpau

useful links

- Help avconv (eng.)
- Computed expressions that can be used in -filter_complex
- Video Filters
- Audio Filters
Also on Habré there is a good translation of the article on video conversion 19 ffmpeg commands for any needs , where you can learn more useful recipes. Almost everything will work with avconv.

Source: https://habr.com/ru/post/192724/


All Articles