FFmpeg is a cross-platform solution for recording, converting, and streaming audio and video. It has the ability to decode, encode, transcode, mux, demux, stream, filter, and play any multimedia content. It works across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc under an expansive variety of build environments, configurations and machine architectures.

FFmpeg tools for transcoding and playing are:

  • ffmpeg is a command line tool that is used to convert multimedia files between formats.
  • ffplay is a simple portable media player using the FFmpeg libraries and the SDL library
  • ffprobe is a simple multimedia analyzer that gathers information from multimedia streams and prints it in human- and machine-readable fashion

FFmpeg includes libraries for developers that can be used by applications to provide the best technically possible solutions. They include;

  • libavutil library which aids in portable multimedia programming including random number generators.
  • libavdevice library contains the input and output devices that are rendered.
  • libavcodec library that provides a generic encoding/decoding framework for audio, video, and subtitle streams.
  • livabfilter library that contains multimedia filters.

This guide takes you through the process of Installing and using FFmpeg on RHEL 9 and CentOS Stream 9.

Install FFmpeg on RHEL 9|CentOS Stream 9

Enable the EPEL repository in your system.

Install the RPM Fusion free with the following command

sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-$(rpm -E %rhel).noarch.rpm

Update your system packages

sudo dnf update -y

Install FFmpeg with the following command

sudo dnf install ffmpeg ffmpeg-devel

Check for the version to confirm successful installation.

$ ffmpeg -version
ffmpeg version 5.1.1 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11 (GCC)

To know the available encoders and decoders, use the following command

ffmpeg -encoders
ffmpeg -decoders

Use FFmpeg on RHEL 9|CentOS 9

There are basic operations that you can do with FFmpeg. Something to note is that you should be in the folder where the video or audio you are working on is located.

Get Video Information

To get video information using FFmpeg use the -i flag that specifies the input.

ffmpeg -i input.mp4

You can use the -hide banner option to hide unnecessary information on the output.

Convert Audio/Video to other formats

You can convert video or audio files to another format depending on the one you want.

To convert MP4 video to WebM, enter the video input with the -i flag then give the output a name with the format you want to convert to. Use the following command.

ffmpeg -i video.mp4 output.webm

To convert an audio file from MP3 to an Ogg file:

ffmpeg -i audio.mp3 output.ogg

Extract an audio file from a video file.

To extract an audio file, use the following command. The -vn flag is used to disable video recording during conversion.

$ ffmpeg -i video.mp4 -vn audio.mp3

Convert a video to an animated GIF.

You can convert a video to an animated GIF by indicating the .gif file format as shown below.

ffmpeg -i video.mp4 animated.gif.mp4

Covert a video to a CD or VCD

You can do this by adding the -target type option on the command line. The options vary from vcd, svcd, dvd, or dv.

$ ffmpeg -i video.mpg -target vcd vcd_video.mpg

Increase/Reduce Video Playback Speed

To manipulate the speed of the video, we use the -vf flag for video filters to adjust the speed.

ffmpeg -i video.mpg -vf "setpts=0.5*PTS" highspeed.mpg

Creating Multiple Outputs

FFmpeg supports creating multiple outputs from the same input. For example, to encode your video in HD, VGA, and QVGA, the command line would be something like this.

ffmpeg -i input.mp4 \
	-s 1280x720 -acodec … -vcodec … output1 \
	-s 640x480  -acodec … -vcodec … output2 \
	-s 320x240  -acodec … -vcodec … output3

Burn Subtitles

You can burn text subtitles with one of two filters: subtitles or ass.

Let’s say the subtitle is in a separate file, the command would be similar to the one below.

##subtitles filter ##
ffmpeg -i video.avi -vf subtitles=subtitle.srt out.avi
## ass filter ##
ffmpeg -i video.avi -vf "ass=subtitle.ass" out.avi

If the subtitle is embedded in a video, the command is as shown below

ffmpeg -i video.mkv -vf subtitles=video.mkv out.avi

Extract Close Captions from a video

Captions are embedded in a video and can be extracted with FFmpeg to an SRT file. This can help in situations where translation may be required.

ffmpeg -f lavfi -i "movie=INPUT.mp4[out0+subcc]" -map s "OUTPUT.srt"

Concatenate Media files

You can concatenate media files with the same codec and codec parameters. We can use Concat demuxer that reads a list of directives from a text file

First, create a text file with a list of the files and their paths you want to concatenate.

$ sudo vi video.txt

file '/path/to/file1.wav'
file '/path/to/file2.wav'
file '/path/to/file3.wav'

Save and exit the file. Then re-encode your files with the following command.

fmpeg -f concat -safe 0 -i video.txt -c copy output.wav

Encoding Videos for Video sharing sites

The best recommendation is to upload an original video to provide the highest quality to video-sharing sites like YouTube, Vimeo, Twitch, and Facebook. But this is not always the case due to format or file size. You can re-encode a video with a similar quality as the original but with a more manageable size with FFmpeg.

ffmpeg -i input.avi -crf 18 output.avi

To specify more codecs, the command should look something similar to the one below.

ffmpeg -i input.avi -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p output.mkv

FFmpeg Filters

FFmpeg can be used to change the frame rate of an existing video to an output frame rate that is lower or higher than the input frame rate. The ​fps filter can be used to do so. the v next to the filter represents the video.

ffmpeg -i input.mp4 -filter:v fps=30 output.mp4

To Crop a Video, we can use the filter to do so. The w is for width, h is for height, and x and y are for the top left corner. The x and y options are optional and if not indicated, the video is centered in the frame.

ffmpeg -i input.mp4 -filter:v "crop=w=640:h=480:x=100:y=200" output.mp4

To scale a video:

ffmpeg -i input.mp4 -filter:v "scale=w=640:h=480" output.mp4

To rotate a video we use the constant PI/180 to multiply with the degree you want the video rotated

ffmpeg -i input.mp4 -filter:v "rotate-45*PI/180" output.mp4

To Tweak the Volume of audio, we can also use the filter to do so. The a represents the target audio channel and 2 for Volume will double it.

ffmpeg -i input.mp3 -filter:a "volume=2" output.mp3

Conclusion

This guide has taken us through the process of installing and using FFmpeg in RHEL 9|Cent OS Stream 9. FFmpeg is a cross-platform multimedia framework that has the ability to encode and decode multimedia content from audio, video, and image files. You can use FFmpeg to extract subtitles from a video, convert audio/video to another format or concatenate different files into one. Check below for some of our recently published articles;

LEAVE A REPLY

Please enter your comment!
Please enter your name here