Managing media isn’t always easy. File formats complicate things further. Many Linux systems employ a powerful tool to both play and manage media: FFMPEG.
FFMPEG is a powerful command-line tool that you can easily use to manage and convert your media files. It can also easily be scripted or incorporated into full programs. Here are some useful FFMPEG commands that will help you get the most from it.
1. View File Information
FFMPEG has access to all of the information about the files that you’re working with. You can display that information in the terminal without modifying the file or running a conversion.
ffmpeg -i video.mkv -hide_banner
Also read: How to Decompile a Video File Into Images with FFMPEG on Linux
2. Simple Conversions Between Formats
Despite its power, FFMPEG is really simple to use. You can easily convert a file between formats with a single command.
ffmpeg -i input.mkv output.mp4
The same thing works with audio files.
ffmpeg -i input.mp3 output.ogg
3. Video Targets
For videos, FFMPEG also includes a feature that attempts to match the specifications of a medium for output. It’s mostly used for formatting a video to be burned to a DVD.
ffmpeg -i input.mkv -target dvd output.avi
You can specify regions, too.
ffmpeg -i input.mkv -target ntsc-dvd output.avi
4. Converting Video to Audio
If you have a video with a song that you’d like or possibly only need the audio portion of a video file, you can use FFMPEG to strip video from a file, leaving only the audio.
ffmpeg -i input.mkv -vn output.ogg
5. Convert Video to GIF
If you’re a fan of creating memes out of short animated .gifs, you’re in luck. FFMPEG can convert a video file to a GIF. Combine this with the next section on creating clips to isolate the exact piece of the video that you want.
ffmpeg -i video.mkv animation.gif
6. Creating Clips
You can create video and audio clips with FFMPEG, too. You only need to specify a start point and how long you want FFMPEG to run for. The -ss
flag will give you a start point. Give it the time in hh:mm:ss
. The same is true for the -f
flag that will specify the run time of the clip. At the end, the -c
flag tells FFMPEG to create a copy from the original instead of cutting down the same file.
ffmpeg -i orig.mkv -ss 00:23:45 -t 00:03:87 -c clip.mkv
7. Scaling Video
FFMPEG can also scale down video to different resolutions to reduce size and/or fit different screens. Specify the dimensions that you want, and it’ll automatically scale when you convert.
ffmepg -i source.mkv -vf scale=1280:720 scaled.mkv
8. Add Subtitles to Video
If you have a video file and the accompanying subtitles separate, you can add in the subtitles with FFMPEG.
ffmpeg -i source.mkv -i subs.srt -map 0 -map 1 -c copy -crf 23 video-with-subtitles.mkv
Also read: How to Screencast From the Command Line in Linux
9. Rotate a Video
You can actually change the rotation of a video. This is useful for when you want to format a video for mobile devices. There are several options you can use to change the direction of the rotation. 0 is for a counter-clockwise rotation and a vertical flip. 1 is for a 90-degree rotation clockwise. 2 rotates the video 90 degrees counter-clockwise. 3 will rotate the video 90 degrees clockwise and flip it vertically.
ffmpeg -i video.mkv -vf transpose=1 for-mobile.mkv
10. Set Audio Bitrate
When you’re converting audio files, it’s often important to set the bitrate of the resulting file. The bitrate will help improve the quality of the audio. To specify the bitrate, use the -ab
flag.
ffmpeg -i source.flac -ab 320k output.mp3
11. Add a Banner to Audio
If you’re creating an audio file to upload to a video site, you can add one in to turn the audio into a video file with a static image.
ffmpeg -loop 1 -i banner.jpg -i audio.mp3 -strict experimental -shortest audio-upload.mp4
12. Change the Default Volume of Audio Files
Sometimes you have an audio file that’s just too quiet to hear, or worse, one that’s obnoxiously loud. You can use FFMPEG to change the default volume of an audio file. It considers 1 to be the default volume, so 2 would be double the original, and 0.5 would be half.
ffmepg -i quiet-song.mp3 -af "volume=2.0" louder-song.mp3
13. Record Audio
You can also use FFMPEG to record audio directly from an input source, like a microphone. It works very similarly to anything else, but you need to specify which audio backend (alsa or pulseaudio) you’re using. You also need to specify a device (the microphone) as the source instead of a file.
ffmpeg -f alsa -i /dev/dsp -ab 320k recording.flac
Closing Ideas
There is a lot more you can do with FFMPEG, actually, and once you get into scripting, you’ll find many more possibilities. Start combining these options, too, and you’ll find that you can gain quite a bit of control over your media files.
This article was first published on September 2016 and was updated in February 2018.
Our latest tutorials delivered straight to your inbox