x32x01
  • by x32x01 ||
FFmpeg is a very flexible, powerful, cross-platform, and versatile command-line tool for converting media file formats.
It allows you to record, convert and stream audio and video files.
It supports all popular audio and video formats. So you don’t have to look for alternative tools for any media file conversion.
Most Linux GUI video editors use ffmpeg in the background for conversion.
Most conversions can be made with one or two commands. Also, you can use ffmpeg to make a more complex conversion.
It uses many libraries to perform all kinds of functions. The libraries are libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale and libswresample.
It offers some tools like ffmpeg, ffplay, and ffprobe that can be used by end-users for transcoding and playing.

In this guide, we’ll show you how to easily convert audio and video files using FFmpeg in Linux
Installing FFmpeg in Linux
The FFmpeg package can be installed on most Linux distributions from default repositories.

For Ubuntu/Debian :
Code:
sudo apt install ffmpeg

For RHEL/CentOS 6/7 : To install ffmpeg you need to enable EPEL Repository and RPMFusion Repository on RPM based distributions (RHEL, CentOS and Fedora).
Code:
sudo yum install ffmpeg ffmpeg-devel

For Fedora :
Code:
sudo dnf install ffmpeg ffmpeg-devel

For ArchLinux/Manjaro :
Code:
sudo pacman -S ffmpeg

For openSUSE :
Code:
sudo zypper install ffmpeg

When done, run the following command to check the installed version of FFmpeg :
Code:
$ ffmpeg -version

ffmpeg version 4.2.4-1ubuntu0.1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-10ubuntu2)
configuration: --prefix=/usr --extra-version=1ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
libavutil      56. 31.100 / 56. 31.100
libavcodec     58. 54.100 / 58. 54.100
libavformat    58. 29.100 / 58. 29.100
libavdevice    58.  8.100 / 58.  8.100
libavfilter     7. 57.100 /  7. 57.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  5.100 /  5.  5.100
libswresample   3.  5.100 /  3.  5.100
libpostproc    55.  5.100 / 55.  5.100
As described at the beginning of the article, FFmpeg supports all popular audio and video formats. Run the command below to get a list of formats supported by FFmpeg:
Code:
$ ffmpeg -formats -hide_banner

File formats:
D. = Demuxing supported
.E = Muxing supported
--
D  3dostr          3DO STR
  E 3g2             3GP2 (3GPP2 file format)
  E 3gp             3GP (3GPP file format)
D  4xm             4X Technologies
  E a64             a64 - video for Commodore 64
D  aa              Audible AA format files
D  aac             raw ADTS AAC (Advanced Audio Coding)
DE ac3             raw AC-3
D  acm             Interplay ACM
D  act             ACT Voice file format
D  adf             Artworx Data Format
D  adp             ADP
D  ads             Sony PS2 ADS
  E adts            ADTS AAC (Advanced Audio Coding)
DE adx             CRI ADX
D  aea             MD STUDIO audio
.
.
The general syntax of FFmpeg is as follows:
Code:
ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

1) How to Get Media File Information
Run the command below to display information about the media file. By default, it displays copyright information about FFmpeg. To avoid that, add the “-hide_banner” flag.
Code:
$ ffmpeg -i media_file_name -hide_banner
It works for audio and video files too as shown below:
Code:
$ ffmpeg -i audio_file.mp3 -hide_banner
$ ffmpeg -i video_file.mp4 -hide_banner

$ ffmpeg -i audio.mp3 -hide_banner
Input #0, mp3, from 'audio.mp3':
  Metadata:
    album           : Dharmadurai (2016)
    comment         : Friendstamilmp3.com
    artist          : Srimathumitha
    title           : Poi Vaadaa En Polikaatu Raasa - Srimathumitha
    composer        : Yuvan Shankar Raja
    date            : 2016
  Duration: 00:02:03.53, start: 0.023021, bitrate: 330 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 320 kb/s
    Stream #0:1: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 400x400, 90k tbr, 90k tbn, 90k tbc (attached pic)
    Metadata:
      comment         : Cover (front)

2) How to Convert Audio From One Format to Another
FFmpeg allows you to convert audio files between different formats. To do so, you must specify the input and output file, the rest will be taken care of by FFmpeg. It will identify the required format from the file extensions.

For instance, run the following command to convert an mp3 audio file to a wav audio file:
Code:
$ ffmpeg -i input_audio.mp3 output_audio.wav -hide_banner

Input #0, mp3, from 'audio.mp3':
  Metadata:
    album           : Dharmadurai (2016)
    comment         : Friendstamilmp3.com
    artist          : Srimathumitha
    title           : Poi Vaadaa En Polikaatu Raasa - Srimathumitha
    composer        : Yuvan Shankar Raja
    date            : 2016
  Duration: 00:02:03.53, start: 0.023021, bitrate: 330 kb/s
    Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 320 kb/s
    Stream #0:1: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 400x400, 90k tbr, 90k tbn, 90k tbc (attached pic)
    Metadata:
      comment         : Cover (front)
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'audio.wav':
  Metadata:
    IPRD            : Dharmadurai (2016)
    ICMT            : Friendstamilmp3.com
    IART            : Srimathumitha
    INAM            : Poi Vaadaa En Polikaatu Raasa - Srimathumitha
    composer        : Yuvan Shankar Raja
    ICRD            : 2016
    ISFT            : Lavf58.29.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      encoder         : Lavc58.54.100 pcm_s16le
size=   23157kB time=00:02:03.50 bitrate=1536.0kbits/s speed= 550x 
video:0kB audio:23157kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000945%

If you want to get multiple output file formats, run the command below:
Code:
$ ffmpeg -i audio.mp3 audio_1.wav audio_2.ogg

3) How to Convert Video from one Format to Another
FFmpeg allows you to convert video files between different formats. This is similar to the examples above. To do so, the input and output file must be specified with an extension.

For instance, run the following command to convert the mp4 video file to avi file:
Code:
$ ffmpeg -i input_video.mkv output_video.avi -hide_banner

Input #0, matroska,webm, from 'video.mkv':
  Metadata:
    ENCODER         : Lavf58.29.100
  Duration: 00:00:49.51, start: 0.000000, bitrate: 255 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x2108 [SAR 1:1 DAR 480:527], 30.30 fps, 30 tbr, 1k tbn, 60 tbc (default)
    Metadata:
      DURATION        : 00:00:49.468000000
    Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:00:49.514000000
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (native))
  Stream #0:1 -> #0:1 (vorbis (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, avi, to 'video.avi':
  Metadata:
    ISFT            : Lavf58.29.100
    Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 1920x2108 [SAR 1:1 DAR 480:527], q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc (default)
    Metadata:
      DURATION        : 00:00:49.468000000
      encoder         : Lavc58.54.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:00:49.514000000
      encoder         : Lavc58.54.100 libmp3lame
frame= 1485 fps=134 q=31.0 Lsize=   10003kB time=00:00:49.53 bitrate=1654.2kbits/s speed=4.48x 
video:9134kB audio:774kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.950320%
If you want to get multiple output file formats, run the command below:
Code:
$ ffmpeg -i video.mp4 video_1.avi video_2.mkv

Add the “-qscale 0” parameter before the output file to preserve the quality of the source video file:
Code:
$ ffmpeg -i video.mp4 -qscale 0 video.avi

Add the “-vcodec” parameter to specify the encoding format to use for the output video:
Code:
$ ffmpeg -i video.mp4 -c:v libx264 video.avi

4) How to Extract the Audio from Video
Add the "-vn" flag to extract audio from the video file:
Code:
$ ffmpeg -i video.avi -vn audio.mp3
By default, the above conversion uses the bit rate of the original file. But, if you want to specify the bit rate, use the format below:
Code:
$ ffmpeg -i video.avi -vn -ab 256k audio.mp3

5) How to Mute Audio on the Video
Add the “-an” flag to disable the audio portion from the video file.
Code:
$ ffmpeg -i video.avi -an video-mute.avi

6) How to Concatenate (Join) Media Files
If there are multiple audio or video files with the same extension. You can join them into a single file using FFmpeg. To do so, create an input file with a list of all the source files you want to concatenate and run the command below:
Code:
# cat vides-list.text

/home/magi/Videos/video_1.mp4
/home/magi/Videos/video_2.mp4
/home/magi/Videos/video_3.mp4
/home/magi/Videos/video_4.mp4

Finally, run the below command to concatenate them:
Code:
$ ffmpeg -f concat -i file-list.txt output.mp4

7) How to Change Video Resolution (Resize a Video)
If you want to resize a video to a specific resolution, run the command below.

For instance, to convert a video file to 640×480 size:
Code:
$ ffmpeg -i input_video.mp4 -filter:v scale=640:480 -c:a copy output_video.mp4
Or
Code:
$ ffmpeg -i input_video.mp4 -s 640x480 -c:a copy output_video.mp4

8) How to Compress Audio files
If you have high bitrate audio files like 320 kbps and if you want to convert them to low bitrate, use the format below. This can be achieved by using the “-ab” flag.

FFmpeg supports the following bitrates:
  • 96kbps
  • 112kbps
  • 128kbps
  • 160kbps
  • 192kbps
  • 256kbps
  • 320kbps
Code:
$ ffmpeg -i input_audio.mp3 -ab 128 output_audio.mp3

9) How to Increase/Decrease Audio Volume
You can easily manage the audio file volume using the “volume filter” option. FFmpeg uses “1” as the original volume of the file.

To decrease the volume by half, use the following format:
Code:
$ ffmpeg -i input_audio.mp3 -af 'volume=0.5' output_audio.mp3

To increase the volume by 50%, use the following format:
Code:
$ ffmpeg -i input_audio.mp3 -af 'volume=1.5' output_audio.mp3

10) How to Extract Images from a Video
This example shows how to create a photo album from a video file. To do so, use the format below:
Code:
$ ffmpeg -i video1.mkv -r 1 image-%2d.png -hide_banner

Input #0, matroska,webm, from 'video1.mkv':
  Metadata:
    ENCODER         : Lavf58.29.100
  Duration: 00:00:16.23, start: 0.000000, bitrate: 559 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1916x2110 [SAR 1:1 DAR 958:1055], 30.30 fps, 30 tbr, 1k tbn, 60 tbc (default)
    Metadata:
      DURATION        : 00:00:16.168000000
    Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:00:16.227000000
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (native))
Press [q] to stop, [?] for help
Output #0, image2, to 'image-%2d.png':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: png, rgb24, 1916x2110 [SAR 1:1 DAR 958:1055], q=2-31, 200 kb/s, 1 fps, 1 tbn, 1 tbc (default)
    Metadata:
      DURATION        : 00:00:16.168000000
      encoder         : Lavc58.54.100 png
frame=   18 fps=5.4 q=-0.0 Lsize=N/A time=00:00:18.00 bitrate=N/A dup=0 drop=468 speed=5.37x 
video:12690kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

  • -r: Specifies the frame rate. Number of frames to be extracted in image per second, default value is 25
  • image-%2d.png: This option allows you to name the extracted images. For example: %2d (2 digit format). This adds a two-digit value at the end of the image, such as image-01, image-02,etc,.
The above command has extracted 18 images from the given video file as shown below:
Code:
$ ls -lh
total 14M
-rw-rw-r-- 1 magi magi  235K Dec 29 14:04 image-01.png
-rw-rw-r-- 1 magi magi  235K Dec 29 14:04 image-02.png
-rw-rw-r-- 1 magi magi  243K Dec 29 14:04 image-03.png
-rw-rw-r-- 1 magi magi  249K Dec 29 14:04 image-04.png
-rw-rw-r-- 1 magi magi  248K Dec 29 14:04 image-05.png
-rw-rw-r-- 1 magi magi  248K Dec 29 14:04 image-06.png
-rw-rw-r-- 1 magi magi  248K Dec 29 14:04 image-07.png
-rw-rw-r-- 1 magi magi  913K Dec 29 14:04 image-08.png
-rw-rw-r-- 1 magi magi  918K Dec 29 14:04 image-09.png
-rw-rw-r-- 1 magi magi 1007K Dec 29 14:04 image-10.png
-rw-rw-r-- 1 magi magi  920K Dec 29 14:04 image-11.png
-rw-rw-r-- 1 magi magi  966K Dec 29 14:04 image-12.png
-rw-rw-r-- 1 magi magi  970K Dec 29 14:04 image-13.png
-rw-rw-r-- 1 magi magi  976K Dec 29 14:04 image-14.png
-rw-rw-r-- 1 magi magi  1.1M Dec 29 14:04 image-15.png
-rw-rw-r-- 1 magi magi  1.1M Dec 29 14:04 image-16.png
-rw-rw-r-- 1 magi magi  1.1M Dec 29 14:04 image-17.png
-rw-rw-r-- 1 magi magi  1.1M Dec 29 14:04 image-18.png
-rw-rw-r-- 1 magi magi  1.1M Dec 29 13:51 video1.mkv

11) How to Cut (Trim) Media Files
This example shows how to trim a video file with diffrent aspect.
  • -t (duration): You can input a single number (number of seconds) or HH:MM:SS (hours, minutes, seconds) for this option.
  • -ss: For this option you must specify the start time with HH:MM:SS (hours, minutes, seconds).
  • -to: You need to specify an end time with HH:MM:SS (hours, minutes, seconds) for this option.
To trim a video file from the beginning to a specific duration, use the “-t” option :
Code:
$ ffmpeg -i video1.mkv -t 5 output-video1.mkv -hide_banner
or
Code:
$ ffmpeg -i video1.mkv -t 00:00:05 output-video1.mkv -hide_banner

To trim a video file from a specific start time to the end, use the “-ss” option :
Code:
$ ffmpeg -i video1.mkv -ss 00:00:08 output-video2.mkv -hide_banner

To trim a video file from a specific start time to a specific end time or duration, use the following format:
Code:
$ ffmpeg -i video1.mkv -ss 00:00:35 -t 17 output-video2.mkv -hide_banner
or
Code:
$ ffmpeg -i video.mp4 -ss 00:00:35 -to 00:00:47 output-video3.mp4 -hide_banner

12) How to Add Subtitles to a Video File
FFmpeg allows you to add subtitles to a video file. To do so, download the correct subtitle for a video and add it:
Code:
$ ffmpeg -i input_video.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output_video.mp4

13) How to Split a Video File
If you have a large video file and want to split it into two parts without re-encoding for certain purposes, then use the format we have explained below.

For example, I have a 3 minute video file. I am going to divide it into two parts using the following command. The first part consists of 1 minute 10 seconds from the beginning.

The second part will consist remaining (1 minute and 50 seconds), it starts from the end of the last part: 00:01:10.
Code:
$ ffmpeg -i input_video.mp4 -t 00:01:10 output_video_1.mp4 -ss 00:01:10 output_video_2.mp4

14) How to Join Images into a Video
You have multiple images and you want to create a slideshow using them. Make sure all images have the same extension .png or .jpg. If not, convert them and put them all in one folder and run the following command:
Code:
$ cat /home/magi/images/* | ffmpeg -framerate 1 -f image2pipe -i - -c:v copy video.mp4

15) How to Record Your Screen
You can easily record your screen with FFmpeg. Use the xdpyinfo command to find the current screen resolution of your Linux desktop before running the FFmpeg command:
Code:
$ xdpyinfo | grep 'dimensions:'
dimensions:    3840x2160 pixels (960x540 millimeters)
or
Code:
$ xdpyinfo | awk '/dimensions/ {print $2}'
3840x2160

Once you get the screen resolution, run the command below:
Code:
$ ffmpeg -f x11grab -s 3840x2160 -i :0.0 output.mp4

Press q or CTRL+C at any time to stop the screencast.

16) How to Merge an Audio and Video File
You have audio-free video and would like to add background music to the video. If so, use the following format.
Code:
$ ffmpeg -i input_video.mp4 -i input_audio.mp3 -c:v copy -c:a aac -strict experimental output_video.mp4

Closing Notes
In this guide, we’ve included 16 examples of converting Audio and Video files in Linux.
 

Similar Threads

x32x01
Replies
0
Views
93
x32x01
x32x01
x32x01
Replies
0
Views
83
x32x01
x32x01
x32x01
Replies
0
Views
103
x32x01
x32x01
x32x01
Replies
0
Views
135
x32x01
x32x01
x32x01
Replies
0
Views
136
x32x01
x32x01
TAGs: Tags
convert audio and video convert audio files convert video files

Register & Login Faster

Forgot your password?

Latest Resources

Forum Statistics

Threads
517
Messages
518
Members
45
Latest Member
Tacola
Back
Top