10 Tricks You Can Do With FFmpeg on Linux


Want to do more with the Linux terminal? You might not instinctively put videos and the command line together, but with FFmpeg you can actually do a lot with a video file just by typing a simple command in your terminal.

In case you haven’t heard of it, FFmpeg is a command line tool that can handle anything related to media. It’s available in most Linux distros’ repositories, so you just need to find and install the package to start using it.

Whether you want to quickly play a video, retrieve some information, or perform cool video-editing tricks, FFmpeg has got you covered. Here are some useful things you can do with FFmpeg on your Linux machine.

10

Playing a Video

You might already have a favorite video player, and that’s great. But sometimes, you just need a quick and simple way to watch something without opening a full graphical application. You can do that using FFmpeg’s built-in player, ffplay.

For example, to play a video, open your terminal and run:

ffplay your_video_file.mp4
Playing video in the terminal using ffplay.

Your video will pop up in a separate window, ready to play. You can control it with commands like “q” to quit, “p” to pause, and the left or right arrow keys to fast-forward or rewind.

If you desire to play your video on a loop, run:

ffplay -loop 0 your_video_file.mp4

The -loop 0 option makes it loop indefinitely. You can replace 0 with any number to set how many times it repeats.

Related


How to Play Your Music Collection From the Linux Command Line

Here’s how to play your local music collection in a Linux terminal window.

Normally, getting video file information involves digging through menus in a media player, or maybe even using a separate app. But with FFmpeg, getting all this info is just a command away.

To get media information, simply run:

ffmpeg -i your_video_file.mp4

In just seconds, you’ll see a comprehensive readout, including codecs, bitrates, frame rates, and more.

Displaying video information using FFmpeg in the terminal.

For even more detailed information about video, audio, and subtitle streams, use ffprobe (a tool of FFmpeg):

ffprobe -show_streams –i your_video_file.mp4

And for a cleaner output in JSON format, run:

ffprobe -v quiet -print_format json -show_format -show_streams your_video_file.mp4
Displaying video information in proper JSON format in the terminal window.

Plus, let me tell you that all of these commands help you efficiently analyze video files without actually playing them.

8

Record Your Screen

FFmpeg can also record your screen. Whether you want to show someone how to do something on Linux or create a quick demo, there’s no need for extra screen recording programs.

Let’s say you want to record your whole screen for 10 seconds. You can do this with this:

ffmpeg -f x11grab -video_size 1920x1080 -r 30 -i :0.0+0,0 -t 10 output.mp4

This command captures your desktop at 1920×1080 resolution with a frame rate of 30 frames per second. Further, the “-i :0.0+0,0” option tells FFmpeg what screen to record. For example, in our case, the “:0.0” refers to the main screen, and “+0,0” means start recording from the top-left corner.

If your system uses Wayland instead of Xorg, you may sometimes encounter a black screen issue, as FFmpeg’s x11grab works best with Xorg. Switching to a Xorg session should fix the problem.

If you don’t know your screen size or position, you can find out with this:

xdpyinfo | grep dimensions

You should be aware the FFmpeg command earlier records your desktop screen without any audio. So, if you also want to record audio along with the video, then you need to specify an audio input device alongside the video input.

For example, you can use the “-f alsa -i pulse” option to capture both the screen video and audio:

ffmpeg -f x11grab -video_size 1920x1080 -r 30 -i :0.0+0,0 -f alsa -i default -t 10 output.mp4

For recording a specific window, the FFmpeg command is slightly more complex, but for quick full-screen recordings, FFmpeg is an excellent choice. Plus, if you love working from the terminal, it gives you full control over every aspect of the recording process.

Related


How to Capture Your Screen in Linux with OBS Studio

Get started with capturing or live streaming using this free, open-source program.

Have you ever wanted to extract a single frame from a video—perhaps for a thumbnail or to capture a cool shot? FFmpeg makes this tasks very simple.

For example, suppose you want to extract one picture every second from a video and save them as image files. To do this, run:

ffmpeg -i input.mp4 -r 1 image-%04d.jpg
Extracted images displaying in a specific folder.

Here, option “-r 1” sets the capture rate to one picture per second. It extracts one frame from each second of the video. You can adjust this number to capture images more or less frequently. You can also change JPG to PNG or another image format if needed.

6

Convert Images Into a Video

FFmpeg can not only extract images but also assemble a series of images into a video. Whether you want to create a slideshow, an animation, or a time-lapse, FFmpeg simplifies the process.

Before conversion, ensure that your images are named sequentially (e.g., image-0001.jpg, image-0002.jpg). Now, convert these sequential images into a video with the following command:

ffmpeg -framerate 1 -i image-%04d.jpg -c:v libx264 -r30 output.mp4

Here, we set the frame rate option to 1 FPS, meaning that if we’ve got 5 pictures and prefer a 5-second video, the frame rate will be 1. You can adjust the frame rate value to speed up or slow down the video.

The previous command converts images into a video without adding music. But what if you want to include music in your video? Run this, replacing “music.mp3” with the audio file you want:

ffmpeg -framerate 1 -i image_%04d.jpg -i music.mp3 -c:v libx264 -r30 -shortest slideshow.mp4

Here, the -shortest option makes the video as long as the shorter input. So, if the audio is longer than the slideshow, the video will match the slideshow’s length.

Related


How to Quickly Resize, Convert & Modify Images from the Linux Terminal

ImageMagick is a suite of command-line utilities for modifying and working with images.

5

Convert a Video to MP3 or GIF

One of FFmpeg’s strongest features is converting videos into different formats, such as transforming video into MP3 or creating a GIF animation from video.

To extract the audio from a video, use the -vn option, which forces FFmpeg to discard the video stream and convert only the audio to MP3:

ffmpeg -i input.mp4 -vn -acodec libmp3lame output.mp3

You can also change output.mp3 to output.wav or another audio format if needed.

To convert video to GIF, use:

ffmpeg -i sample_video.mp4 output.gif

You can also extract specific parts of the video and convert them into GIFs with this:

ffmpeg -ss 30.0 -t 2.1 -i sample_video.mp4 output.gif

This command trims 2.1 seconds from the front of the 00:30 duration of the video and converts it to a GIF.

4

Add Subtitles to a Movie

Adding subtitles to a movie can be very useful, especially when watching content in a different language. FFmpeg simplifies the process of adding subtitles to your videos.

First, obtain a subtitle file, typically with an SRT extension. For example, if you have a subtitle file (such as subtitles.srt) and a video (input.mp4), you can hardcode the subtitles into the video using:

ffmpeg -i input.mp4 -vf "subtitles=subtitles.srt" output.mp4

This command permanently embeds the subtitles, ensuring they remain visible and cannot be turned off.

If you prefer optional subtitles that viewers can turn on or off, use this:

ffmpeg -i input.mp4 -i subtitles.srt -c copy -c:s mov_text output.mp4

This command keeps the subtitles as a separate track, preserving the original video quality.

3

Rebuild a Video’s Index Without Transcoding

Sometimes a video may appear glitchy—it might skip, freeze, or prevent fast-forwarding or rewinding. Often, this issue arises from a corrupted video index. To fix this problem, you may need to rebuild the index without re-encoding the video.

Fortunately, FFmpeg can often repair the index without altering the video itself. This process, known as remuxing, is fast because it preserves the original video and audio quality while correcting the file’s structure.

To rebuild a video index, run:

ffmpeg -i input.mp4 -c copy -copyts output.mp4

Here, the -c copy option instructs FFmpeg to copy the video and audio streams exactly as they are, preserving their quality and speeding up the process. The -copyts option ensures that the timing information is copied correctly, which is crucial for smooth playback.

This approach is useful for quickly checking and repairing your video file. However, if the issue persists, the video may be severely corrupted.

2

Resize Videos

Resizing videos is one of FFmpeg’s most useful features. You can easily adjust video dimensions for social media, mobile screens, or storage optimization. Smaller videos take up less space, upload faster, and perform better on slower connections.

To scale a video to specific dimensions (e.g., 1280×720), use:

ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4

If you want FFmpeg to maintain the aspect ratio automatically, specify only one dimension or use expressions:

ffmpeg -i input.mp4 -vf scale=640:-1 output.mp4

This sets the width to 640 pixels, and FFmpeg calculates the appropriate height to preserve the aspect ratio. However, note that downscaling may reduce quality, so choose resolutions carefully.

1

Trim and Crop Videos

Trimming a video allows you to extract only the necessary sections without affecting quality. This is perfect for eliminating unwanted intros, outros, or any mistakes.

For instance, to extract a 20-second segment starting from 10 seconds into the video, run:

ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:30 -c copy output_trimmed.mp4

Here, “-ss 00:00:10” tells FFmpeg to start at the 10-second mark, while “-to 00:00:30” stops the video at 30 seconds. Also, the “-c copy” option ensures that the video and audio are copied without re-encoding, making the process much faster while preserving the original quality.

Cropping removes unnecessary edges or zooms in on the most important part of a video. To crop a video to 640×480 pixels, starting from the top-left corner, execute this:

ffmpeg -i input.mp4 -vf "crop=640:480:0:0" output_cropped.mp4

The crop filter takes four values: width, height, and the x and y coordinates for where the crop should start. In this case, the width and height are set to 640×480, and 0:0 ensures the cropping starts from the top-left corner of the original video.

Related


How to Trim Videos in VLC Media Player

Cut the unwanted parts out of your videos!


By practicing and learning these FFmpeg tricks, you can easily boost your productivity and streamline your workload. And remember—this is only the beginning. There are many things in FFmpeg waiting to be explored, so dive in and keep experimenting!



Source link

Previous articleBitcoin price recovery sets base for TON, AVAX, NEAR, OKB to rally
Next articleBitcoin Futures Data Shows Bullish Long/Short Ratio – Details — TradingView News