In this guide, I will use examples to illustrate how to use the FFmpeg media framework to do various audio and video transcoding and conversion operations. I have compiled more than 20 most commonly used FFmpeg commands for beginners, and I will add more examples from time to time to keep this guide updated. Please bookmark this guide and check back for updates later. Let's get started, if you haven't installed FFmpeg on your Linux system, refer to the guide below.
- Install FFmpeg in Linux
More than 20 FFmpeg commands for beginners The typical syntax of
FFmpeg command is:
- ffmpeg [global options] {[input file options] -i input_url_address} ...
- {[output file options ] Output_url_address} ...
Now we will look at some important and useful FFmpeg commands.
1. Get audio/video file information
To display the details of your media files, run:
- $ ffmpeg -i video.mp4
Sample output:
- ffmpeg version n4.1.3 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-versi on3
- 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. 3.100
- libpostproc 55. 3.100 / 55. 3.100zlibpostproc Input 0, mov9 mp4,m4a,3gp,3g2,mj2, from'video.mp4':
- Metadata:
- major_brand: isom
- minor_version: 512
- compatible_brands: bitomiso2avc1mp41
- enzcoder: startzzz 00:00: 00:00: 454zrate 00:00: 00:00: 00:00 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 mu st be specified
As you can see in the above output, FFmpeg displays the media file information, as well as FFmpeg details, such as version, configuration details, copyright mark, build parameters, library options, etc.
If you don't want to see the FFmpeg banner and other details, but just want to see the media file information, use the -hide_banner flag, as shown below.
- $ ffmpeg -i video.mp4 -hide_banner
Sample output:
Use FFMpeg to view audio and video file information.
see it? Now, it only displays the 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 mp4 files to avi files, run:
- $ ffmpeg -i video.mp4 video.avi
Similarly, you can convert media files to any format you choose.
, for example, to convert YouTube flv format video is in mpeg format, run:
- $ ffmpeg -i video.flv video.mpeg
If you want to maintain the quality of your source video file, use -qscale 0 parameter:
- $ ffmpeg -i input.webm -qscale 0 output.mp4
is to check the list of supported formats of FFmpeg, run:
- $ ffmpeg -formats
3, convert video file to audio file
I convert a video file to audio file, just specify the output format, like .mp3, or .ogg , Or any other audio format. The above command
will convert the input.mp4 video file to the output.mp3 audio file.
- $ ffmpeg -i input.mp4 -vn output.mp3
In addition, you can also use various 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
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-output file format. In our example, it is in mp3 format.
4, change the resolution of the video file
If you want to set a video file to a specified resolution, you can use the following command:
- $ ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
or,
- $ ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4
The above command will set the resolution of the given video file to 1280×720.
Similarly, to convert the above file to 640×480 size, run:
- $ ffmpeg -i input.mp4 -filter:v scale=640:480 -c:a copy output.mp4
or,
- $ ffmpeg -i input .mp4 -s 640x480 -c:a copy output.mp4
This trick will help you scale your video files to smaller display devices, such as tablets and mobile phones.
5. Compressing video files
It is always a good idea to reduce the size of media files to a smaller size to save hardware space. The command below
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
Please note that if you try to reduce the size of the video file, you will lose the video quality. If 24 is too aggressive, you can lower the -crf value to or lower.
You can also use the following options to convert the encoded audio to reduce the bit rate, make it have a sense of stereo, thereby reducing the size.
- -ac2 -c:a aac -strict -2 -b:a 128k
6. Compressed audio files
are just like compressed video files. To save some disk space, you can also use the -ab flag to compress audio files.
For example, you have an audio file with a bitrate of 320 kbps. You want to compress it by changing the bitrate to any lower value, like below.
- $ ffmpeg -i input.mp3 -ab 128 output.mp3
The list of various available audio bitrates is:
- 96kbps
- 112kbps
- 128kbps
- 160kbps
- 192kbps
- 256kbpsz10, if you don't want an audio stream from a video file, you don’t want to remove a video stream from a video file of 4zz21kbps zz10. an sign.
- $ ffmpeg -i input.mp4 -an output.mp4
- $ ffmpeg -i input.mp4 -vn output.mp3
- $ ffmpeg -i input.mp4 -vn -ab 320 output.mp3
- $ ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png
- -r-set the frame rate. That is, the number of frames to images is extracted every second. The default value is 25.
- -f-indicates the output format, that is, in our case it is an image.
- image-%2d.png – shows how we want to name the extracted image. In this example, the naming should start like image-01.png, image-02.png, image-03.png, etc. If you use %3d, then the image naming starts like image-001.png, image-002.png, etc.
- ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
- input.mp4-source video file.
- -filter:v-indicates the video filter.
- crop-represents a crop filter.
- w – The width of the rectangle we want to crop from the source video.
- h-the height of the rectangle.
- x-we want to self-source the videoThe x coordinate of the cropped rectangle.
- y – The y coordinate of the rectangle.
- $ ffmpeg -i input.mp4 -filter:v "crop=640:480: 200:150" output.mp4
- $ ffmpeg -i input.mp4 -t 10 output.avi
- $ ffmpeg -i input.mp4 -aspect 16:9 output.mp4
- 16:9
- 4:3
- 16:10
- 5:4
- 2:21:1
- 9 2:10zzz13, add the poster image to the image of zzz11:11:13 Audio file
- $ ffmpeg -loop 1 -i inputimage.jpg -i inputaudio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
- $ ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4
- –s – represents the start time of the video clip. In our example, the start time is the 50th second.
- -t-represents the total duration.
- $ ffmpeg -i audio.mp3 -ss 00:01:54 -to 00:06:53 -c copy output.mp3
- $ ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4
- -t 00:00:30 means from the beginning of the video Create a part of the video to the 30th second of the video.
- -ss 00:00:30 shows the start timestamp for the next part of the video. It means that Part 2 will start at the 30th second and will continue to the end of the original video file.
- file /home/sk/myvideos/part1.mp4
- file /home/sk/myvideos/part2.mp4
- file /home/sk/myvideos/part3.mp4
- file /home/sk/myvideos/part4.mp4
- $ ffmpeg -f concat -i join.txt -c copy output.mp4
- [concat @ 0x555fed174cc0] Unsafe file name'/path/to/mp4'
- join.txt: Operation not permitted
- $ ffmpeg -f concat -safe 0 -i join.txt -c copy output.mp4
- $ fmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4
- $ ffplay video.mp4
- $ ffplay audio.mp3
- $ ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
- $ ffmpeg -i input.mp4 -vf "setpts=4.0*PTS" output.mp4
- in Linux
- How to create a video from a PDF file in Linux
- $ man ffmpeg
Here, -an means no audio recording. The command above
will cancel all audio-related flags, because we don't want audio from input.mp4.
8. Remove a video stream from a media file
Similarly, if you don't want a video stream, you can simply remove it from the media file using the -vn flag. -vn means there is no video recording. In other words, this command converts the given media file into an audio file. The command below
will remove the video from the given media file.
You can also use the -ab flag to indicate the bit rate of the output file, as shown in the following example.
9. Extracting 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 images from a video file, use the following command:
Here,
10, cropped video
FFMpeg allows to crop a given media file in any range we choose. The syntax of
cropping a video file is given as follows:
Here,
For example, if you want a video with a width of 640 pixels and a height of 480 pixels from the position of the video (200,150), the command should be:
Please note that cutting the video will affect the quality. Do not cut unless necessary.
11. Convert a specific part of a video
Sometimes, you may want to convert only a specific part of a video file to a different format. To illustrate with an example, the following command will convert the first 10 seconds of the given video input.mp4 file to the video .avi format.
Here, we specify the time in seconds. In addition, it is also possible to specify the time in the hh.mm.ss format.
12. Set the aspect ratio of the video screen
You can use the -aspect flag to set the aspect ratio of a video file, as shown below.
The commonly used aspect ratio is:
You can add a poster image to your file so that the image will be displayed when the audio file is played. This is useful for audio files hosted on video hosting or sharing websites.
14, use the start and stop time to cut a piece of media file
can use the start and stop time to cut a video into a short clip, we can use the following command.
Here,
It is very useful when you want to cut a part of an audio or video file using the start and end time.
Similarly, we can cut the audio like below.
15, split the video file into multiple parts
Some websites will only allow you to upload videos of specific sizes . In this case, you can split a large video file into multiple smaller parts, like below.
Here,
16. Join or merge multiple video parts into one
FFmpeg. You can also join multiple video parts and create a single video file.
creates join.txt containing the exact path of the file you want to join. All files should be in the same format (same encoding format). The path of all files should be listed one by one, like below.
Now, join all files, use the command
If you get some errors like the following;
Add -safe 0 :
The above command will join part1.mp4, part2.mp4, part3.mp4 and part4.mp4 files into one called output.mp4 in a single file.
17. Add subtitles to a video file
We can use FFmpeg to add subtitles to the video file. Download the correct subtitles for your video and add it to your video as shown below.
18, preview or test video or audio files
you may want to preview To verify or test whether the output file has been properly transcoded. To complete the preview, you can play it from your terminal with the command:
Similarly, you can test the audio file as shown below.
19, increase/decrease the video playback speed
FFmpeg allows you to adjust the video playback speed.
To increase the video playback speed, run:
This command will double the video speed.
To reduce the speed of your video, you need to use a multiple greater than 1. To reduce the playback speed, run:
20. Create animated GIFs
For various purposes, we use GIFs on almost all social and professional networks image. Using FFmpeg, we can create animated video files simply and quickly. The following guide explains how to use FFmp on Unix-like systemseg and ImageMagick create an animated GIF file. How to create animated GIF
21, create video from PDF files
I have collected many PDF files over the years, most of which are Linux tutorials, which are saved on my tablet. Sometimes I am too lazy to read them from the tablet. Therefore, 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.
22, get help
In this guide, I have covered most of the frequently used FFmpeg commands. It has many different options to do various advanced functions. To learn more usage, please refer to the man page.
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 are coming. Stay tuned!
Thank you!
via: https://www.ostechnix.com/20-ffmpeg-commands-beginners/
Author: sk topics: lujun9972 Translator: robsean proofread: wxy
LCTT original article by the compiler, Linux China is proud
transferred from https: / /linux.cn/article-10932-1.html