In this tutorial, I’ll use examples to illustrate how to do a variety of audio and video transcoding and conversion operations using the FFmpeg media framework. I’ve compiled over 20 of the most commonly used FFmpeg commands for beginners, and I’ll keep this guide up to date by adding more examples from time to time. Please bookmark this guide and check back later for updates. Let’s get started. If you don’t have FFmpeg installed on your Linux system, refer to the guide below.
- Install FFmpeg in Linux
More than 20 FFmpeg commands for beginners
The typical syntax for FFmpeg commands is:
Ffmpeg [global options] {[Input file options] -i Input _url_ address}... {[Output file option] Output _url_ address}...Copy the code
Now we’ll look at some important and useful FFmpeg commands.
1. Obtain audio/video file information
To display the details of your media file, run:
$ ffmpeg -i video.mp4
Copy the code
Sample output:
Copyright (C) 2000-2019 The FFMPEG developers Built with GCC 8.2.1 (GCC) 20181127 Configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3 libavutil 56.22.100/56 22.100 libavCodec 58.35.100/58.35.100 libavformat 58.20.100/58.20.100 libavDevice 58.5.100/58.5.100 Libavfilter 7.40.101/7.40.101 libswScale 5.3.100/5.3.100 libswresample 3.3.100/3.100 libpostProc 55.3.100 / 55. 3.100 Input#0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':Metadata: major_brand: isom minor_version: 512 compatible_brands: isomiso2AVc1mp41 encoder: Lavf58.20.100 Duration: 00:00:28.79, start: 0.000000, bitrate: 454 KB /s Stream#0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/smpte170m), 1920x1080 [SAR 1:1 DAR 16:9], 318 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 04/08/2019.
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 04/08/2019.
At least one output file must be specified
Copy the code
As you can see in the output above, FFmpeg displays the media file information, along with FFmpeg details such as version, configuration details, copyright tags, build parameters, library options, and so on.
If you don’t want to see the FFmpeg banner and other details, and just want to see the media file information, use the -hide_banner flag, like below.
$ ffmpeg -i video.mp4 -hide_banner
Copy the code
Sample output:
Use FFMpeg to view audio and video file information.
See? Right now, it just shows media file details.
2. Convert video files to different formats
FFmpeg is a powerful audio and video converter, so it can convert media files between different formats. For example, to convert an MP4 file to an AVI file, run:
$ ffmpeg -i video.mp4 video.avi
Copy the code
Similarly, you can convert media files to any format you choose.
For example, to convert a YouTube FLV video to MPEG, run:
$ ffmpeg -i video.flv video.mpeg
Copy the code
If you want to maintain the quality of your source video files, use the -qscale 0 parameter:
$ ffmpeg -i input.webm -qscale 0 output.mp4
Copy the code
To check the list of supported formats for FFmpeg, run:
$ ffmpeg -formats
Copy the code
3. Convert video files to audio files
I convert a video file to an audio file by specifying the output format, like.mp3, or.ogg, or any other audio format.
The command above will convert the input.mp4 video file to output.mp3 audio file.
$ ffmpeg -i input.mp4 -vn output.mp3
Copy the code
In addition, you can also use a variety of audio conversion encoding options for the output file, as shown below.
$ ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 320 -f mp3 output.mp3
Copy the code
Here,
-vn
– Indicates that we have disabled video recording in the output file.-ar
– Set the audio frequency of the output file. Commonly used values are 22050 Hz, 44100 Hz, 48000 Hz.-ac
– Set the number of audio channels.-ab
– Indicates the audio bit rate.-f
– Format of the output file. In our case, it’s in MP3 format.
4. Change the resolution of the video file
If you want to set a video file to the specified resolution, you can use the following command:
$ ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
Copy the code
Or,
$ ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4
Copy the code
The command above sets the resolution of the given video file to 1280×720.
Similarly, to convert the above file to 640×480, run:
$ ffmpeg -i input.mp4 -filter:v scale=640:480 -c:a copy output.mp4
Copy the code
Or,
$ ffmpeg -i input.mp4 -s 640x480 -c:a copy output.mp4
Copy the code
This trick will help you zoom your video files onto smaller display devices, such as tablets and phones.
5. Compress video files
It’s always a good idea to reduce the media file size to a smaller size to save space on your hardware.
The following command will compress and reduce the size of the output file.
$ ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4
Copy the code
Please note that if you try to reduce the size of your video file, you will lose video quality. If 24 is too aggressive, you can lower the -crF value to or lower.
You can also reduce the size by converting the encoded audio to lower the bit rate and make it stereo.
-ac 2 -c:a aac -strict -2 -b:a 128k
Copy the code
6. Compress audio files
Just as you can compress video files, to save some disk space, you can also compress audio files with the -ab flag.
For example, you have an audio file with a bit rate of 320 KBPS. You want to compress it by changing the bit rate to an arbitrarily low value, as shown below.
$ ffmpeg -i input.mp3 -ab 128 output.mp3
Copy the code
A list of the various audio bitrates available is:
- 96kbps
- 112kbps
- 128kbps
- 160kbps
- 192kbps
- 256kbps
- 320kbps
Remove the audio stream from a video file
If you do not want audio in a video file, use the -an flag.
$ ffmpeg -i input.mp4 -an output.mp4
Copy the code
In this case, -an means no audio recording.
The command above undoes all audio related flags because we do not want audio from input.mp4.
Remove the video stream from a media file
Similarly, if you don’t want the video stream, you can simply remove it from the media file using the -vn flag. -vn indicates that no video is recorded. In other words, this command converts the given media file to an audio file.
The following command will remove the video from the given media file.
$ ffmpeg -i input.mp4 -vn output.mp3
Copy the code
You can also use the -ab flag to indicate the bit rate of the output file, as shown in the following example.
$ ffmpeg -i input.mp4 -vn -ab 320 output.mp3
Copy the code
9. Extract images from video
Another useful feature of FFmpeg is that we can easily extract images from a video file. This can be very useful if you want to create an album from a video file.
To extract an image from a video file, use the following command:
$ ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png
Copy the code
Here,
-r
– Set the frame speed. That is, the number of frames extracted to the image per second. The default value is 25.-f
– Represents the output format, that is, in our case, the image.image-%2d.png
— Indicates how we want to name the extracted image. In this example, the naming should start like this: image-01.png, image-02.png, image-03.png, etc. If you use%3d
PNG, image-001.png, image-002.png, etc.
Crop the video
FFMpeg allows you to crop a given media file in any range we choose.
The syntax for clipping a video file is given as follows:
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
Copy the code
Here,
input.mp4
– Source video files.-filter:v
– Indicates a video filter.crop
– Clipping a filter.w
— The width of the rectangle we want to crop from the source video.h
– Height of the rectangle.x
— The x coordinate of the rectangle we want to crop from the source video.y
– The y coordinate of the rectangle.
For example, if you want a video from a video location (200,150) with a width of 640 pixels and a height of 480 pixels, the command should be:
$ ffmpeg -i input.mp4 -filter:v "crop=640:480:200:150" output.mp4
Copy the code
Please note that cutting video will affect quality. Do not cut unless necessary.
Convert a specific part of a video
Sometimes, you may want to convert just a specific part of the video file to a different format. As an example, the following command will convert the first 10 seconds of the given video input.mp4 file to the video.avi format.
$ ffmpeg -i input.mp4 -t 10 output.avi
Copy the code
Here, we specify time in seconds. It is also possible to specify the time in hH.mm. Ss format.
12. Set the screen aspect ratio of the video
You can use the -aspect flag to set the screen aspect ratio of a video file, as shown below.
$ ffmpeg -i input.mp4 -aspect 16:9 output.mp4
Copy the code
The height to width ratios commonly used are:
- 16:9
- 4:3
- “
- when
- 2:21:1
- 2:35:1
- 2:39:1
13. Add poster image to audio file
You can add the poster image to your file so that the image will be displayed when playing the audio file. This is useful for hosting audio files on video hosting hosts or sharing sites.
$ ffmpeg -loop 1 -i inputimage.jpg -i inputaudio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
Copy the code
Use start and stop times to cut out a section of the media file
To cut a video into a short clip, use the start and stop times, we can use the following command.
$ ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4
Copy the code
Here,
- s
– Indicates the start time of the video clip. In our example, the start time is 50 seconds.-t
– Indicates the total duration.
It’s very useful when you want to use start and end times to cut a portion of an audio or video file.
Similarly, we can clip the audio as follows.
$ ffmpeg -i audio.mp3 -ss 00:01:54 -to 00:06:53 -c copy output.mp3
Copy the code
15. Split the video file into multiple parts
Some sites will only allow you to upload videos of a specific size. In such cases, you can split the large video file into multiple smaller parts, like below.
$ ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4
Copy the code
Here,
-t 00:00:30
Creates a portion of the video from the beginning to the 30th second of the video.-ss 00:00:30
Displays the start timestamp for the next section of the video. This means that Part 2 will start at 30 seconds and continue until the end of the original video file.
Join or merge multiple video parts into one
FFmpeg can also join multiple video parts and create a single video file.
Create a join.txt containing the exact path to the file you want to join. All files should be in the same format (same encoding format). The paths for all files should be listed individually, as shown below.
file /home/sk/myvideos/part1.mp4
file /home/sk/myvideos/part2.mp4
file /home/sk/myvideos/part3.mp4
file /home/sk/myvideos/part4.mp4
Copy the code
Now, coalesce all the files using the command:
$ ffmpeg -f concat -i join.txt -c copy output.mp4
Copy the code
If you get something like the following error;
[concat @ 0x555fed174cc0] Unsafe file name '/path/to/mp4'
join.txt: Operation not permitted
Copy the code
Add -safe 0:
$ ffmpeg -f concat -safe 0 -i join.txt -c copy output.mp4
Copy the code
The command above will coalate the part1.mp4, part2.mp4, part3.mp4, and part4.mp4 files into a single file called output.mp4.
17. Add subtitles to a video file
We can use FFmpeg to add subtitles to the video file. Download the correct caption for your video and add it to your video as shown below.
$ fmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4
Copy the code
Preview or test a video or audio file
You may want to preview to verify or test that the output file has been transcoded properly. To complete the preview, you can play it from your terminal, using the command:
$ ffplay video.mp4
Copy the code
Similarly, you can test audio files, as shown below.
$ ffplay audio.mp3
Copy the code
19. Increase/decrease video playback speed
FFmpeg allows you to adjust the video playback speed.
To increase video playback speed, run:
$ ffmpeg -i input.mp4 -vf "Setpts = 0.5 * PTS" output.mp4
Copy the code
The command will double the speed of the video.
To slow down your video, you need to use a multiple greater than one. To reduce playback speed, run:
$ ffmpeg -i input.mp4 -vf "Setpts = 4.0 * PTS" output.mp4
Copy the code
20. Create animated GIFs
We use GIF images on almost all social and professional networks for a variety of purposes. Using FFmpeg, we can create animated video files easily and quickly. The following guide explains how to create an animated GIF file using FFmpeg and ImageMagick on unix-like systems.
- How do I create animated GIFs in Linux
Create a video from a PDF file
I’ve been collecting PDFS for years, mostly Linux tutorials, on my tablet. Sometimes I’m too lazy to read them from my tablet. So I decided to create a video from a PDF file and watch them on a large-screen device, like a TV or a computer. If you want to know how to make a movie from a batch of PDF files, the following guide will help you.
- How do I create a video from a PDF file in Linux
22. Get help
In this tutorial, I’ve covered most of the commonly used FFmpeg commands. It has a lot of different options to do a variety of advanced functions. To learn more about usage, refer to the man page.
$ man ffmpeg
Copy the code
That’s all. I hope this guide will help you get started with FFmpeg. If you find this guide useful, please share it on your social and professional networks. More good things to come. Stay tuned!
Thank you very much!
Via: www.ostechnix.com/20-ffmpeg-c…
By robsean (sk) lujun9972
This article is originally compiled by LCTT and released in Linux China