CodeTechSphere

Using ffmpeg to handle multimedia

Cover Image for Using ffmpeg to handle multimedia
Muhammad Khawaja
Muhammad Khawaja

Multimedia processing and manipulation is a task that many people come across frequently, but few have the means or knowledge of doing it themselves. Regardless if you are a developer or a non-technical enthusiast, learning how to use Ffmpeg allows for a ton of possibilities for audio and video manipulation. From basic tasks like trimming a video, adjusting the resolution, or converting formats, to more complex tasks like video compression and filters, Ffmpeg is an amazing tool that everyone can benefit from.

In this tutorial, we’ll look at the different functionalities Ffmpeg has to offer for virtually every multimedia need. By the end, you’ll have enough knowledge of Ffmpeg to execute command-line commands with confidence for tasks like resizing and transcoding videos.

Transcoding

Perhaps one of the most common manipulation methods people use is transcoding. That is, converting multimedia from one encoding to another. This can be useful for converting multimedia to a more efficient encoding, more compatible, or both.

To convert from mov to mp4 is as simple as this:

ffmpeg -i input.mov output.mp4

There are a number of encoding options that can be passed (after the input file is specified):

-c:v: Specify the video codec, e.g. libx265. More efficient codecs don’t guarantee compatibility necessarily. -c:a is for audio codecs. Use “copy” to avoid re-encoding of a specific stream.
-b:v: Set the output video bit rate. Bit rate controls the overall quality of the video. This cannot be used with a constant rate factor.
-crf: Constant rate factor (0-51). This lets you specify how much compression the video should undergo, with higher values resulting in a smaller file size, but worse quality. 23-28 is a good place to start.
-preset: This just determines encoding efficiency and affects the encoding speed. The more time you allow for encoding, the better the results usually. Options are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, and placebo.
-r: Limit the fps of the output.

Compression

Video compression is done to reduce the file size significantly at the tradeoff of video quality, usually an amount that is acceptable. As specified above, the options all contribute to reducing file size when used. To have the best video compression (most file size reduction without sacrificing quality), look to have a constant rate factor just below 28, and a preset that allows for more encoding time. Another thing that can be adjusted is the resolution which the next section covers.

Resizing

Decreasing resolution of videos is one of the most effective ways of decreasing file size. No matter how compressed a video is or advanced a codec is, resolution can be a stalling point for one looking to reduce file size.

The command to adjust the resolution of a video to a specific width and height:

ffmpeg -i input.mp4 -vf scale=width:height output.mp4

If you are looking to maintain a certain aspect ratio, set either width or height to -2.

It’s also possible to resize the video based on it’s own dimensions, like reducing them by half:

ffmpeg -i input.mp4 -vf scale="iw/2:ih/2" output.mp4

Trimming

Trimming a video from a certain timestamp to another is as simple as a one-liner:

ffmpeg -i input.mp4 -ss 00:01:30 -to 00:02:45 -c:v copy -c:a copy output1.mp4

In this command, the video will be trimmed from 1m30s to 2m45s. The video and audio codec is also copied over (with the “-c:v copy -c:a copy” options) as there is no need for those to be re-encoded.

-to can be replaced with -t to act as a duration option, i.e. 2 minutes and 45 seconds will be trimmed after the 1m30s mark.

Extraction

One might want to extract images from a video for something like an album:

ffmpeg -i input.mp4 -r 1 image-%2d.png

The -r option lets us specify the number of frames to extract from one second of video. The image-%2d.png format tells Ffmpeg to output the images with a padding of at least 1, i.e. image-01, image-02, and so on.

Conclusion

Ffmpeg is an extremely powerful and versatile tool that every media enthusiast should master. It allows for all sorts of multimedia handling with just a single command, and is free to use with no limits.