Taming multimedia with ffmpeg
Suddenly, your drive to the eyeballs is packed with photos and videos, and new trips are ahead. What to do, buy a new one, rent disk space on the cloud, or can it be better to compress video files through ffmpeg ?

But why limit yourself to save disk space? I propose to learn the amazing possibilities of processing photos, audio and video data, command line utilities.
Ffmpeg library and video processing
The open source library ffmpeg is most likely already installed on your operating system. If not, install it with a standard package management program, it will not take much time.
Convert one audio and video file format to another
ffmpeg -i file. [options] file. Reduce the video recorded on the camera:
ffmpeg -i MVI_4703.MOV MVI_4703.aviThe same, but with quality control.
ffmpeg -i MVI_4703.MOV -q:v 4 MVI_4703.aviThe video size has decreased by more than 5 times without a noticeable loss in quality. The option -qscale:v n, in short, -q:v nallows you to set the quality level of the generated video stream, where it ntakes values in the range from 1 to 31. A value of 1 corresponds to the best quality, and 31 to the worst.
-rw-r--r-- 1 mig users 124M июл 18 23:29 foto/MVI_4703.avi
-rw-r--r-- 1 mig users 686M июн 27 21:38 foto/MVI_4703.MOVSpecify codec
In order to select the codec we need, we use the keys .-c:a
ffmpeg -i video.mp4 -c:v vp9 -c:a libvorbis video.mkvYou can see all supported codecs using the command ffmpeg -codecs.
Change file container
Now take this user casehappening. The built-in player of your TV supports mkv format but does not support m4v. In order to change the container, use the following command.
ffmpeg -i video.m4v -c:av copy video.mkvIf you need to change only the sound and leave the video as it is, run this command. For some reason, Philips TVs only understand AAC / AC3 audio formats.
ffmpeg -i video.m4v -c:v copy -c:a aac video.mkvAdd soundtrack
Just list the input files and set the output.
ffmpeg -i video.mp4 -i audio.ogg video_sound.mp4Extract audio track
If you just need to extract the sound, then you can.
ffmpeg -i video.MOV -vn audio.oggSet the format of the extracted audio track.
ffmpeg -i video.MOV -vn -c:a flac audio.flacIndicates an acceptable bitrate, by default 128k will be recorded.
ffmpeg -i video.MOV -vn -c:a flac -b:a 192k audio.flacMaking a slide show of pictures
This is the case when it was smooth on paper. In practice, you have to walk a rake, wading through a variety of formats, codecs, sizes and orientations of photos.
ffmpeg -r .3 -pix_fmt rgba -s 1280x720 -pattern_type glob -i "*.JPGЭ video.mkvSome clarification required.
- -r number - frame rate per second.
- -pix_fmt - Pixel format, list from the command
ffmpeg -pix_fmts. Not with all formats it turns out to set the desired frame size. - -pattern_type glob - To use pattern matching as in a command shell. An alternative is to use the C format
printf, for exampleimage%03d.pngfor everyoneimage0001.png,image0002.pngetc.
Change Video Stream
Suppose you do not need the entire video file, but only part of it. This command will cut 10 seconds of video, starting from the first minute.
ffmpeg -i video_full.m4v -c:av copy -ss 00:01:00 -t 10 video_short.m4vHow to improve the quality of audio or video streams? For this, a bitrate key is used -b.
ffmpeg - video.webm -c:a copy -c:v vp9 -b:v 2M final.mkvScreen capture
A device is used to capture the screen x11grab, and ffmpeg must be compiled with the option --enable-x11grab.
ffmpeg -f x11grab -framerate 25 -video_size 4cif -i :0.0 out.mpg- -video_size word - Capture size, cif = 352x288, 4cif = 704x576. More in
info ffmpeg-utils.
Bonus track
For automatic photo processing, it is convenient to work with ImageMagick . Resize all photos in a folder.
mogrify -resize 60% *.pngAccurate image sharpening, like Smart Sharpen using a Perl script that uses convertand compositea set of tools of ImageMagick .
Related Links
- Habrapost about ffmpeg , a lot of useful commands, but the syntax for most has already changed.
- A quick guide to using ffmpeg to convert media files
- Configure encoding quality in FFmpeg: variable and constant bitrate for Mpeg4