📜 ⬆️ ⬇️

Taming Multimedia with ffmpeg

Suddenly, your disk is full of photos and videos, and there are new trips ahead. What to do, buy a new one, rent a disk space on the cloud, or can it better compress video files via ffmpeg ?





But why limit yourself to saving 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 regular package management program; it won't take long.


Convert one format of audio and video files to another


ffmpeg -i file.<old_extension> [options] file.<new_extension> 

Reduce the video recorded on the camera:


 ffmpeg -i MVI_4703.MOV MVI_4703.avi 

The same, but with quality control.


 ffmpeg -i MVI_4703.MOV -q:v 4 MVI_4703.avi 

The video size has decreased by more than 5 times without noticeable loss of quality. The -qscale:vn option, abbreviated to -q:vn allows you to set the quality level of the generated video stream, where n takes values ​​in the range from 1 to 31. Value 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.MOV 

Specify codec


To select the codec we need, use the -c:a <codec> -c:v <codec> keys.


 ffmpeg -i video.mp4 -c:v vp9 -c:a libvorbis video.mkv 

You can see all supported codecs using the ffmpeg -codecs .


Change file container


Now take this user case happening. Your TV's built-in player supports the 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.mkv 

If you only need to change the sound, and leave the video as it is, run this command. For some reason, Phillips TVs understand only the AAC / AC3 ​​audio formats.


 ffmpeg -i video.m4v -c:v copy -c:a aac video.mkv 

Add audio track


Simply enumerate the input files and set the output.


 ffmpeg -i video.mp4 -i audio.ogg video_sound.mp4 

Extract audio track


If you just need to extract the sound, then you can.


 ffmpeg -i video.MOV -vn audio.ogg 

Specify the format of the extracted audio track.


 ffmpeg -i video.MOV -vn -c:a flac audio.flac 

Indicates the acceptable bitrate, by default 128k will be recorded.


 ffmpeg -i video.MOV -vn -c:a flac -b:a 192k audio.flac 

Making a slide show of pictures


The same case, when it was smooth on paper. In practice, you have to walk around the rake, pushing through the inconsistency of formats, codecs, sizes and orientation of photos.


 ffmpeg -r .3 -pix_fmt rgba -s 1280x720 -pattern_type glob -i "*.JPG video.mkv 

Some explanation is required.



Change video stream


Suppose you do not need the entire video file, but only a part of it. This command cuts 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.m4v 

How to improve the quality of audio or video streams? To do this, use the key bit- -b .


 ffmpeg - video.webm -c:a copy -c:v vp9 -b:v 2M final.mkv 

Screen capture


To capture the screen, an x11grab device is x11grab , and ffmpeg should be built with the --enable-x11grab .


 ffmpeg -f x11grab -framerate 25 -video_size 4cif -i :0.0 out.mpg 


Bonus track


For automatic photo processing is convenient to work with the program ImageMagick . Change the size of all photos in the folder.


 mogrify -resize 60% *.png 

Accurate image sharpening, like Smart Sharpen using a Perl script that uses convert and composite from the ImageMagick toolkit .


Related Links


  1. Habrapost about ffmpeg , many useful commands, but the syntax for the majority has already changed.
  2. A quick guide to using FFmpeg to convert media files
  3. Setting the encoding quality in FFmpeg: variable and constant bitrate for Mpeg4

')

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


All Articles