Getting Started With Ffmpeg On Linux Comprehensive Guide

📚 On this page
Getting Started With Ffmpeg On Linux Comprehensive Guide

FFmpeg is a powerful command-line tool for handling multimedia files. In this blog post, I’ll walk you through the basics of using FFMPEG on a Linux system, covering installation, common commands, and practical examples.

#What is FFMPEG?

FFMPEG is a free and open-source command-line tool for processing multimedia files. It supports various formats, including MP4, AVI, MKV, MP3, and more. With FFMPEG, you can convert, record, edit, and stream audio and video files easily.

#Installation

First, let’s install FFmpeg. On most Linux distributions, you can use the package manager: For Ubuntu/Debian:

sudo apt update
sudo apt install ffmpeg
Copied!
sudo dnf install ffmpeg
Copied!
sudo pacman -S ffmpeg
Copied!

Once installed, you can verify the installation by running:

ffmpeg -version
Copied!

#Basic FFMPEG Commands

Let’s explore some basic commands that you can use to work with audio and video files.

FFmpeg’s basic syntax is:

ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...
Copied!

#1. Converting Video Formats

One of the most common tasks with FFMPEG is converting video files from one format to another. For example, to convert a video from MP4 to AVI, you can use the following command:

ffmpeg -i input.mp4 output.avi
Copied!

In this command, -i specifies the input file, and the output file format is determined by the file extension.

You can even specify more output files:

ffmpeg -i input.wav output_1.mp3 output_2.ogg
Copied!

This will convert the input files to all specified formats.

To see a list of all supported formats, use:

ffmpeg -formats
Copied!

#2. Extracting Audio from Video

You can also extract audio from a video file using FFMPEG. For example, to extract the audio from an MP4 video and save it as an MP3 file:

ffmpeg -i input.mp4 -vn output.mp3
Copied!

This command extracts the audio without changing the original quality.

Alternatively, you can use:

ffmpeg -i input.mp4 -q:a 0 -map a output.mp3
Copied!

In this command:

  • -q:a 0 sets the audio quality to the highest possible level.
  • -map a ensures that only the audio stream is extracted.

Between these two, the second option (-q:a 0) offers better control over the audio quality, making it a better choice when you need to maintain the best sound.

#3. Compressing Video

FFMPEG allows you to compress video files to reduce their size while maintaining decent quality. To compress a video, you can use the following command:

ffmpeg -i input.mp4 -vcodec libx264 -crf 28 output.mp4
Copied!

In this command, -vcodec libx264 specifies the video codec, and -crf 28 controls the quality of the video. Lower CRF values result in higher quality, while higher values reduce quality but decrease file size. A CRF of 28 is a good balance between quality and compression.

#4. Merging Audio and Video Files

If you have separate audio and video files and want to combine them into a single file, you can do this easily with FFMPEG:

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4
Copied!

This command merges the video and audio files without re-encoding the video (-c:v copy), which preserves the original video quality. The audio is encoded with AAC (-c:a aac).

#5. Trimming Video

You can trim or cut a portion of a video using FFMPEG. For example, to trim the first 10 seconds of a video:

ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4
Copied!

Here, -ss 00:00:10 sets the start time at 10 seconds, and -t 00:00:30 sets the duration to 30 seconds. The -c copy option copies the streams without re-encoding, making the process faster and preserving quality.

#6. Adding Subtitles

You can add subtitles to your video using FFMPEG. Assuming you have a subtitle file (e.g., subtitles.srt), you can add it like this:

ffmpeg -i input.mp4 -vf subtitles=subtitles.srt output.mp4
Copied!

This command burns the subtitles directly into the video, making them a permanent part of the file.

#7. Adding Watermarks to Videos

To add a watermark to a video, you can use the following FFMPEG command:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4
Copied!

In this command, watermark.png is the image file used as the watermark, and overlay=10:10 positions the watermark 10 pixels from the top and left edges of the video. You can adjust the overlay values to position the watermark wherever you prefer.

#Advanced Usage

#1. Concatenating Videos

You can join multiple videos into one using FFMPEG. If your videos are in the same format, you can use the following command:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
Copied!

Here, filelist.txt is a text file listing the paths of the videos you want to concatenate, one per line, like this:

file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
Copied!

The -c copy option tells FFMPEG to concatenate without re-encoding, preserving the original quality.

#2 Creating a GIF from Video

You can create a GIF from a video using FFMPEG, which is great for making short, looping animations:

ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" output.gif
Copied!

In this command, fps=10 sets the frame rate to 10 frames per second, and scale=320:-1 resizes the video to a width of 320 pixels while maintaining the aspect ratio. The lanczos filter provides high-quality scaling.

#Bonus Tips

#1. Record Your Screen

FFMPEG can record your screen, which is useful for creating tutorials or demonstrations. To capture your screen:

ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0 output.mp4
Copied!

Press q or CTRL+C at any time to stop the screencast.

In this command, -video_size 1920x1080 sets the resolution, -framerate 25 sets the frame rate, and -f x11grab -i :0.0 specifies the screen input on Linux.

Tip: If you want FFMPEG to automatically detect your screen’s resolution, you can use this alternative command:

ffmpeg -f x11grab -s $(xdpyinfo | grep dimensions | awk '{print $2;}') -i :0.0 output.mp4
Copied!

This command will adjust to your screen’s resolution without needing to specify it manually, making the recording process even easier.

#2. Record Your Webcam

You can also use FFMPEG to record video directly from your webcam:

ffmpeg -f v4l2 -i /dev/video0 output.mp4
Copied!

Here, -f v4l2 specifies the video input format, and /dev/video0 is the default device for your webcam. Adjust the device path as needed.

#3. Record Your Audio

To record audio from your microphone, use the following command:

ffmpeg -f alsa -i default output.wav
Copied!

In this command, -f alsa -i default selects your system’s default microphone input. The output is saved as a WAV file, but you can choose other formats like MP3 or FLAC if needed.

#Conclusion

This tutorial covers the basics of using FFmpeg on Linux, including common tasks like compressing videos, extracting audio, and more. While this guide focuses on essential commands and options, FFmpeg offers a wide range of advanced features and options.

For more detailed information and to explore the full capabilities of FFmpeg, consult the official FFmpeg documentation or run man ffmpeg in your terminal.

Feel free to experiment with different options and settings to find what works best for your needs. If you have any questions or need further clarification on any part of this tutorial, let me know!

Happy exploring! 🚀