FFmpeg Rocks
Let's say you need to convert an MP3 file to the OGG format, or you have separate audio and video files that you'd like to merge. Your options are to either find an online tool where you upload the files and download them after merging, or download (and install) an ad-enabled software that does the job for you.
If either of those two options sounds frustrating, fear not, because you are not out of options. FFmpeg is the angel you seek. With a download size of a little over 10 to 20 MB, FFmpeg can virtually answer most of your basic multimedia needs. It's open source, doesn't show you any ads, and it's been around for long enough that it's practically bug free. Also, if you are on a Linux distro, chances are that it's already installed. All you need to do is learn the basic commands needed to do the job.
Some of the things that FFmpeg can do for you, are:
-
Convert from one multimedia format to another
-
Extract audio, video or even subtitles from a multimedia file
-
Crop or join files together
-
Merge audio and video
-
Take screenshots from videos
-
Synchronize audio and video
-
View video from your webcam
-
Take screenshots from your computer screen
What's amazing is that you only need a single command (i.e., ffmpeg
) to do all of that. You just provide the names of the input and output files and a few sensible arguments. Even if you don't like running commands in CMD or shell, the fact that you can do so much with a tiny program far outweighs the urge to cling to your mouse. If that sounds like a bold statement, read on and see how wrong you can be.
File Format Conversion
Converting from one multimedia format to another can be as easy as:
ffmpeg -i input.webm output.mp4
But bear in mind that conversion is one of those operations that need the full power of your system and is time-consuming. To speed things up, you might want to add a few extra arguments:
# use a preset to speed up conversion
ffmpeg -i input.webm -preset ultrafast "output.mp4"
# or scale down the video if you don't mind having a smaller resolution
ffmpeg -i input.webm -preset ultrafast -vf scale=-1:1080 "output.mp4"
Description
-
-i
let's you specify the input file -
-preset
use one of the default presets for conversion (affects conversion speed and quality) -
-vf
video filer to use.scale
is the name of the filter fed to-vf
.scale
scales your video to the dimensions specified.-1:1080
will set the height to 1080 and choose the width accordingly, so the video's aspect ratio is conserved.
Extracting Audio from Video
This is probably very useful if you want to cut some audio track out of a video for later use. You only need to provide the -vn
argument to ffmpeg
to indicate that you don't want the output file to contain a video. In the following example, I've specified that I want the audio track to just be copied, and not converted to a different format, the advantage being faster audio extraction.
ffmpeg -i video.mp4 -vn -c:a copy output-audio.aac
We could also extract the video from the multimedia file by specifying -an
instead of -vn
.
Question: How do I find out what audio and video formats are used in a file?
A multimedia file is like a container that holds together multiple audio, video and even subtitles inside it. You can find out these "streams" by using the following command
ffprobe filename.mp4
ffprobe
is a tool that gets installed with FFmpeg. The output of the above command will look like this:
ffprobe version ...
...
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'filename.mp4':
Metadata:
...
Duration: 00:00:40.02, start: 0.000000, bitrate: 282 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 ...
...
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
...
You can find out from the output that the file contains an audio stream in the aac
format.
Cropping
To cut out a portion of your audio or video file, use the following command. -ss
is the starting second to crop from, -t
is the number of seconds and -to
is the specific second desired. The following commands result in the same output.
ffmpeg -ss 10 -t 6 -i input.mp3 output.mp3
ffmpeg -ss 10 -to 16 -i input.mp3 output.mp3
Merging audio and video
Put the names of the files you want to merge in a text file as shown below. Enclose the file names within double quotes if your file names contain spaces or special characters.
file "file001.mp4"
file "file002.avi"
file "file003.mkv"
After you have created the text file above, use the following command to concatenate your multimedia files together:
ffpmeg -f concat -safe 0 -i files.txt -c copy output.mp4
Description
-safe 0
is used when your file names contain special characters
Converting a video into a series of images
The following command will take a screenshot of the video every second. If you need to take more screenshots per second, you can use fps=1/4
for example, which will take four screenshots every second.
ffmpeg -i video.mp4 -vf fps=1 "screenshots/images_%06d.png"
Description
-
fps
short forframes per second
will specify the number of frames that can be taken in a second -
-i
the input file -
%06d
append a six-digit number to the name of each image taken from the video and left pad the number with zeros
Syncing audio and video
If you have a multimedia file in which the audio and video in it are not in sync, use the following command
ffmpeg -i "movie.mp4" -itsoffset 3.84 -i "movie.mp4" -map 0:v -map 1:a -c copy "movie-audio-delayed.mp4"
Description
-
-itoffset
short forinput timestamp offset
specifies the second you want to offset the input. Here, the audio and video inputs are the same file. -
-map 0:v -map 1:a
take the video from the first input and audio from the second input
Stream video from the webcam
ffmpeg -f x11grab -r 30 -s 1920x1080 -i :0.0 out.mp4
Description
-
-f x11grab
the name of the format to use -
-r
frames in a second
To find the dimensions of your screen:
xdpyinfo | grep dimensions
-i
input file (or device). 0:0 is the current display. You can find your current display name using:
echo $DISPLAY
Recording audio from the microphone
ffmpeg -f pulse -i default out.aac
Description
-
-f
The name of the format to use for recording audio. To get a list of formats, useffmpeg -formats
. -
-i
the input. Here, we have specified the default audio device.
Capturing video/audio from the webcam/microphone and streaming it at the same time
Run the following commands in two different shells
# play from the webcam
ffplay -f video4linux2 -i /dev/video0 -video_size 320x240 -fflags nobuffer
# record from the screen
ffmpeg -f pulse -ac 2 -i default -f x11grab -r 30 -s 1920x1080 -i :0.0 -vcodec libx264 -preset ultrafast out.mp4
Sources
https://trac.ffmpeg.org/wiki/Capture/ALSA#Recordaudiofromyourmicrophone https://stackoverflow.com/a/22082953/2139684
https://ffmpeg.org/ffmpeg.html