Recently I needed to write a small video editor with a web interface.
Before that, I had occasionally used commands like
ffmpeg -i file.avi file.mp3
Mainly for converting from one format to another. Everything was always more or less smooth and it was difficult to imagine how many nuances there really are for working with video and audio.
But let’s start from the beginning. For some time now, my ubuntu began to issue:
*** THIS PROGRAM IS DEPRECATED *** This program is only provided <strong>for</strong> compatibility and will be removed <strong>in</strong> a future release. Please use avconv instead.
In general, while it was used in small ways, it was not very important, but it was somehow “not it” to put an already obsolete feature into the project. I had to google what’s what and it turned out that the ffmpeg
project had split some time ago and some of the developers started creating the libav library, which is currently included in ubuntu by default. Of course, the interoperability of the advanced features was sacrificed first. At the same time, with the renaming of the project, the ffmpeg executable file was renamed to avconv, which was the above warning.
avconv [input file options] -i input file [conversion options] output file
there can be several input files, respectively, and -i options are written before each of them
But in reality, everything turned out to be a little more complicated.
Further, a set of useful recipes and along the description, small comments about the options.
Basic options commonly found in commands
-y
– overwrite file without question-threads 8
– how many threads to perform the operation (in this case 8, but not all codecs are able to parallelize)-s hd720
– video size (in this case, according to the hd720p-1280 * 720 standard)-q 1
– sets the encoding quality (affects different codecs differently). Do not forget this option, because by default only the name is from the quality.
For options setting stream properties, it is possible to specify this stream directly in the option itself (without this, the option applies to all streams). This is done as follows:-q: a 1
– sets the audio quality,-q: v 1
– respectively – video.
The number will mean the stream numbered-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 need to first look at the information that avconv gives about the contents of the input files.
Cutting a large file into pieces
avconv -i in.mp4 -ss 00:00:30 -t 00:10:00 -q 1 -s hd720 -threads 8 -r 25 -y out.mp4
We say from what second -ss 00:00:30
and what length of time -t 00:10:00
to take the video.
Separation of audio from video at the desired 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
– excludes the video stream from the output-ss 00:00:22
– the beginning of the cut fragment-t 00:00:30
– length of the cut fragment-vol 512
– the volume, 256 is the normal volume, everything higher increases that lower, respectively-ar 44100
– audio sample rate (audio stream sampling frequency) 44100 corresponds to cd quality and is a 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 another format, but raw is useful for us if we want to concatenate a lot of audio fragments, which will be easy to do just by connecting the files one by one.