The Grip of FFmpeg

By Arseny Vasilyev

FFmpeg is a set of libraries for video and audio files processing. FFmpeg converts mov into mp4, places a watermark, builds up several video files into one file, changes the resolution, recodes the streaming video, creates the stream by itself. FFmpeg is useful for those who work on the video professionally, develop video applications or just process home videos.

FmFpeg has no graphical interface and is controlled by console operators. Many users work with FFmpeg by coping necessary commands from the net, which is this or that flag. Let’s learn how to work with FFmpeg deliberately through several useful commands.

Today we’ll make our way from getting the information about a file to practicing several filters simultaneously.

Getting the information about a file

ffmpeg -i file.mp4

This is a simple matter — we enter the path, get the information about it: codec, the quantity of paths, resolution, a bitrate and a framerate. We direct the paths to the files with a flag -i (input)

-i file1.mp4 -i file2.mp4

Converting into mp4

Let’s say we have a site, where the video was played with Flash player. We want to change it for HTML5Video. To do it we convert the video files on the server from flv format into mp4 format.

ffmpeg -i flashvideo.flv -vcodec libx264 -acodec libvo_aacenc output.mp4

In comparison with the previous command we have three fields added:

· output.mp4 — is a path to a converted file.

· -vcodec libx264 — is an indicator what video codec we’d like to get, so, in this case it’s H264, which is provided by most of the browsers

· -acodec libvo_aacenc — is an audio codec AAC

You can get the list of codecs supported by ffmpeg with ffmpeg -codecs.

You can get the list of codecs supported by ffmpeg with ffmpeg -codecs.

Instead of –vcodec you can see the record -codec:v and -c:v. If you don’t point out the codecs for conversion ffmpeg -i flashvideo.flv output.mp4, the ffmpeg will define the defaulted codecs of the file format at the output.

During conversion not only codecs should be plotted. Let’s increase the number of settings:

ffmpeg -i flashvideo.flv -b:v 700k -r 25 -pix_fmt yuv420p -vcodec libx264 \

-b:a 128k -ar 44100 -acodec libvo_aacenc -y output.mp4

We defined the bitrate for the video -b:v and audio -b:a as 700 and 128 Kbps. You can define the bitrate with other flags as well: -vb and -b for video and -ab for audio.

The other three characteristics are the framerate -r 25 in frames per second, the color model YUV , which equals a common variable Yuv420p, and sound sampling frequency 44100 Hz. We need flag -y for the rerecording of the file if it already exists.

Cutting a video fragment

ffmpeg -ss 10 -t 20 -i source_video.mp4 -vcodec copy -acodec copy result.mp4

We point out the moment when we cut the original video with the flag –ss, where -t is the duration. At the output we get a 20 seconds video, which will start from the tenth second of the original video. The record -ss 00:00:10.250 -t 00:00:20.120 will be at use while defining a more precise patch of time. Before Version 2.1 FFmpeg cut the video according to the nearest key frame in both cases. In updated versions the final video is close to the defined interval as high as possible.

We added -vcodec copy -acodec copy to keep the codecs of the original video. Otherwise, ffmpeg would recode them into the codecs automatically. Currently, the video isn’t recoded, and the command is run quickly. Then we’ll simplify this record with a flag -c copy.

If we place the flags -ss and-t after the incoming video, the final video will be the same, but the command will be run longer. Flags will be related to the final video and FFmpeg will decode the video fragment up to -ss . In the first case, this part will be ignored. Flag -t can be replaced with –to, having indicated the final point instead of the durance. This flag can’t be applied to the incoming video.

ffmpeg -ss 10 -i source_video.mp4 -to 30 -c copy result.mp4

The command resulted as follows: 30 seconds video from the tenth second of the original video. It occurred because -to used the time scale result.mp4, the thirtieth second of which equals the fortieth second source_video.mp4. To use the original time scale let us add the flag –copyts. Thus, we will get the same video as from the first command.

ffmpeg -ss 10 -i source_video.mp4 -to 30 -c copy result.mp4

Filters. Changing the resolution

One of the most powerful ffmpeg tools is filters.

They can add effects, overlap various videos, bind several files into the one. We can see the list of determined filters with the help of the command ffmpeg -filters Let’s change the video resolution with their help.

ffmpeg -i source_video.avi -vf scale=640:480 source_preview.avi

We used the flag -vf , having indicated the filter scale and resolution 640:480. This filter can keep the proportions and calculate the resolution dynamically

Combination of filters. Speeding up the replay

ffmpeg -i source_video.mp4 \

-filter_complex "[0:v]setpts=0.5*PTS;[0:a]atempo=2.0" accelerated.mp4

We combined several filters into the one with the flag filter_complex and got a twice speeded file. In oeder to get onto it, we’ll aplly filters one by one.

We speeded up the video track with the flag setpts . However, the audio track wasn’t changed, and the file will be of the same durance as it was: the video will be replayed twice faster, and the last frame will be hanging during the whole second for all the rest rest half of the file. If we remove the audio track with -an flag, the duration of the original file will be defined by the video track and will be twice shorter than the original one.

On the contrary, if we need only the speeded up audio without any video, we can use the filter atempo, and put away the video track with the flag vn.

ffmpeg -i source_video.mp4 -af atempo=2.0 -vn accelerated_audio.mp3

After having added filter_complex and implied both filters, we speeded up the whole file.

The filter graph implies the sequence of several filters to the original file. We defined the speed-up setpts for the video track [0:v] and atempo for the audio [0:a]. With the help of filter combination we can solve a lot of tasks with the video editing.

Sticking files together

ffmpeg -i input1.mp4 -i input2.mp4 \

-filter_complex "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" \

-map "[v]" -map "[a]" -y output.mp4

We see a new flag map. It shows the ffmpeg which tracks we would like to get in the final file. Ensure that there is one video track and one audio track in our original video files, and on condition that we want to have the same at the output, we don’t need the flag map. Other examples of using map you can find in the ffmpeg site.

Let us do the same in a different way.

ffmpeg -f concat -i list.txt -c copy output.mp4

FFmpeg has elements, which read or write media in certain forms. They are demuxer and multiplexor.

-f concat — is a demuxer, which gets the text file with a list of media files and stick them together in one file. You can see other elements with the command ffmpeg –formats.

The contents list.txt:

file 'input1.mp4'

file 'input2.mp4'

This approach is rational if we have many files and we need to stick them together. This approach doesn’t recode video and works faster than the first way. But the first approach is implied more often. First, it can take a video with various codecs. Secondly, it provides more flexibility. For instance, if we need only the part of the first video we can use flags -ss and -t. Pay attention that these flags are only for the first file input1.mp4.

ffmpeg -ss 5 -t 2 -i input1.mp4 -i input2.mp4 \

-filter_complex "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" \

-map "[v]" -map "[a]" -y result.mp4

Watermark

The last example we are going to view is the watermark overlay. The filter overlay accepts at the input the coordinates of the watermark position in the final video.

Let’s place the watermark align center. Knowing the size of the original video and png of the picture, you can calculate the coordinates yourselves. For example, we take the video 1280 x720 and a watermark 200x200, the state in horizontal direction x = (1280–200) / 2 = 540 and in vertical direction y = (720–200) / 2 = 260. Correspondingly the filter value “overlay=540:260”. However, it’s more comfortable to write a general command, in order the ffmpeg could count everything for us. We will get the size of the video according to the criteria main_w and main_h, and the watermark according to overlay_w and overlay_h. Then we will write the formula, with the help of which we calculated the coordinates, into the filter value "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2". Done, we got the command for watermark overlay align center.

ffmpeg -i source_video.mp4 -i watermark.png \

-filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" \

-codec:a copy video_protected.mp4

FFmpeg is a powerful tool for work with a video. Other problem solving with its help you can find in the article 19 FFmpeg commands for every needs.

--

--

--

Video and Multimedia Software Developmet company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

python in Machine Learning

Flexible

Active Record: seeding data & the Faker gem

Going Agile

Top 5 Most Demanding Interview Questions and Answers in CSS3

🚀BSC Launcher Presale is SOLD OUT.

OVERSHIB INU

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fora Soft

Fora Soft

Video and Multimedia Software Developmet company

More from Medium

Difference between activity and fragment android

Coding a Better World Together with Uncle Bob

Being a better developer: Self Improvement

Common Android Developer Job Interview Questions for 2022