Useful ffmpeg commands

FFMPEG conversion commands

First of all we need to see how to install ffmpeg on a debian based system.
$ sudo add-apt-repository ppa:jon-severinsson/ffmpeg
$ sudo apt-get update
$ sudo apt-get install ffmpeg

This article describes some basic audio format conversions using ffmpeg utility. All ffmpeg format conversion commands use default ffmpeg setting. User should alter below commands to increase or decrease audio quality with additional ffmpeg options.

How to play files from command line?

To play wav files try to see if sox is installed on the system using command man sox to see a detailed explanation for the sox command. It will display information if the sox is installed
type in play <audio-filename.wav> to play the audio file of wav format.

To play mp3 from command line using this command.
sudo apt-get install mpg123
sudo apt-get install libsox-fmt-mp3

Then type in play <audio-filename.mp3>

1.1. WAV - Waveform Audio File Format

1.1.1. wav to mp3

Convert wav to mp3 with ffmpeg:
ffmpeg -i audio.wav -acodec libmp3lame audio.mp3
 
to control the quality see this page Encodemp3
ffmpeg -i audio.wav -codec:a libmp3lame -qscale:a 0 audio.mp3
 
qscale is the one that controls the quality of the audio file 

1.1.2. wav to ogg

Convert wav to ogg with ffmpeg:
ffmpeg -i audio.wav  -acodec libvorbis audio.ogg

1.1.3. wav to aac

Convert wav to acc with ffmpeg:
ffmpeg -i audio.wav  -acodec libfaac audio.aac

1.1.4. wav to ac3

Convert wav to ac3 with ffmpeg:
ffmpeg -i audio.wav -acodec ac3 audio.mp3

1.2. OGG - Free, open standard container

1.2.1. ogg to mp3

Convert ogg to mp3 with ffmpeg:
ffmpeg -i audio.ogg -acodec libmp3lame audio.mp3

1.2.2. ogg to wav

Convert ogg to wav with ffmpeg:
ffmpeg -i audio.ogg audio.wav

1.2.3. ogg to aac

Convert ogg to aac with ffmpeg:
ffmpeg -i audio.ogg  -acodec libfaac audio.aac

1.2.4. ogg to ac3

Convert ogg to ac3 with ffmpeg:
ffmpeg -i audio.ogg -acodec ac3 audio.ac3

1.3. AC3 - Acoustic Coder 3

1.3.1. ac3 to mp3

Convert ac3 to mp3 with ffmpeg:
ffmpeg -i audio.ac3 -acodec libmp3lame audio.mp3

1.3.2. ac3 to wav

Convert ac3 to wav with ffmpeg:
ffmpeg -i audio.ac3 audio.wav

1.3.3. ac3 to aac

Convert ac3 to aac with ffmpeg:
ffmpeg -i audio.ac3  -acodec libfaac audio.aac

1.3.4. ac3 to ogg

Convert ac3 to ogg with ffmpeg:
ffmpeg -i audio.ac3 -acodec libvorbis audio.ogg

1.4. AAC - Advanced Audio Coding

1.4.1. aac to mp3

Convert aac to mp3 with ffmpeg:
ffmpeg -i audio.aac -acodec libmp3lame audio.mp3

1.4.2. aac to wav

Convert aac to wav with ffmpeg:
ffmpeg -i audio.aac audio.wav

1.4.3. aac to ac3

Convert aac to ac3 with ffmpeg:
ffmpeg -i audio.aac  -acodec ac3 audio.ac3

1.4.4. aac to ogg

Convert aac to ogg with ffmpeg:
ffmpeg -i audio.aac -libvorbis audio.ogg
 
FOR VIDEO Conversion commands 
 
1. Cut video file into a smaller clip You can use the time offset parameter (-ss) to specify the start time stamp in HH:MM:SS.ms format while the -t parameter is for specifying the actual duration of the clip in seconds.

ffmpeg -i input.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4

2. Split a video into multiple parts

If you want to split a large video into multiple smaller clips without re-encoding, ffmpeg can help. This command will split the source video into 2 parts – one ending at 50s from the start and the other beginning at 50s and ending at the end of the input video.
ffmpeg -i video.mp4 -t 00:00:50 -c copy small-1.mp4 -ss 00:00:50 -codec copy small-2.mp4

3. Convert video from one format to another

You can use the -vcodec parameter to specify the encoding format to be used for the output video. Encoding a video takes time but you can speed up the process by forcing a preset though it would degrade the quality of the output video.

ffmpeg -i youtube.flv -c:v libx264 filename.mp4

ffmpeg -i video.wmv -c:v libx264 -preset ultrafast video.mp4

4. Join (concatenate) video files

If you have multiple audio or video files encoded with the same codecs, you can join them into a single file using FFmpeg. Create a input file with a list of all source files that you wish to concatenate and then run this command.

ffmpeg -f concat -i file-list.txt -c copy output.mp4

5. Mute a video (Remove the audio component)

Use the -an parameter to disable the audio portion of a video stream.

ffmpeg -i video.mp4 -an mute-video.mp4

6. Extract the audio from video

The -vn switch extracts the audio portion from a video and we are using the -ab switch to save the audio as a 256kbps MP3 audio file.

ffmpeg -i video.mp4 -vn -ab 256 audio.mp3

7. Convert a video into animated GIF

FFmpeg is an excellent tool for converting videos into animated GIFs and the quality isn’t bad either. Use the scale filter to specify the width of the GIF, the -t parameter specific the duration while -r specifies the frame rate (fps).

ffmpeg -i video.mp4 -vf scale=500:-1 -t 10 -r 10 image.gif

8. Extract image frames from a video

This command will extract the video frame at the 15s mark and saves it as a 800px wide JPEG image. You can also use the -s switch (like -s 400×300) to specify the exact dimensions of the image file though it will probably create a stretched image if the image size doesn’t follow the aspect ratio of the original video file.

ffmpeg -ss 00:00:15 -i video.mp4 -vf scale=800:-1 -vframes 1 image.jpg
OR
ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%3d.jpeg
This will extract one video frame per second from the video and will output them in files named foo-001.jpeg, foo-002.jpeg. Images will be rescaled to fit the new WxH values

9. Convert Video into Images

You can use FFmpeg to automatically extract image frames from a video every ‘n’ seconds and the images are saved in a sequence. This command saves image frame after every 4 seconds.

ffmpeg -i movie.mp4 -r 0.25 frames_%04d.png

10. Merge an audio and video file

You can also specify the -shortest switch to finish the encoding when the shortest clip ends.

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental -shortest output.mp4

11. Resize a video

Use the size (-s) switch with ffmpeg to resize a video while maintaining the aspect ratio.

ffmpeg -i input.mp4 -s 480x320 -c:a copy output.mp4

12. Create video slideshow from images

This command creates a video slideshow using a series of images that are named as img001.png, img002.png, etc. Each image will have a duration of 5 seconds (-r 1/5).

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p slideshow.mp4

13. Add a poster image to audio

You can add a cover image to an audio file and the length of the output video will be the same as that of the input audio stream. This may come handy for uploading MP3s to YouTube.

ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4

14. Convert a single image into a video

Use the -t parameter to specify the duration of the video.

ffmpeg -loop 1 -i image.png -c:v libx264 -t 30 -pix_fmt yuv420p video.mp4

15. Add subtitles to a movie

This will take the subtitles from the .srt file. FFmpeg can decode most common subtitle formats.

ffmpeg -i movie.mp4 -i subtitles.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mkv

16. Crop an audio file

This will create a 30 second audio file starting at 90 seconds from the original audio file without transcoding.

ffmpeg -ss 00:01:30 -t 30 -acodec copy -i inputfile.mp3 outputfile.mp3

17. Change the audio volume

You can use the volume filter to alter the volume of a media file using FFmpeg. This command will half the volume of the audio file.

ffmpeg -i input.wav -af 'volume=0.5' output.wav

18. Rotate a video

This command will rotate a video clip 90° clockwise. You can set transpose to 2 to rotate the video 90° anti-clockwise.

ffmpeg -i input.mp4 -filter:v 'transpose=1' rotated-video.mp4
This will rotate the video 180° counter-clockwise.

ffmpeg -i input.mp4 -filter:v 'transpose=2,transpose=2' rotated-video.mp4

19. Speed up or Slow down the video

You can change the speed of your video using the setpts (set presentation time stamp) filter of FFmpeg. This command will make the video 8x (1/8) faster or use setpts=4*PTS to make the video 4x slower.

ffmpeg -i input.mp4 -filter:v "setpts=0.125*PTS" output.mp4

20. Speed up or Slow down the audio

For changing the speed of audio, use the atempo audio filter. This command will double the speed of audio. You can use any value between 0.5 and 2.0 for audio.

ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv
  
21.  Check the properties of a file using ffmpeg 
 
ffprobe <filename> 
 
 
Source -  https://trac.ffmpeg.org/wiki/AudioChannelManipulation

Manipulating audio channels with ffmpeg

stereo → mono stream

stereo to mono diagram
Mix a single stereo stream down to a mono stream. Both channels of the stereo stream will be downmixed into the stream:
ffmpeg -i stereo.flac -ac 1 mono.flac
Note: Any out of phase stereo will cancel out.

stereo → 2 × mono files

stereo to 2 mono outputs diagram
Output each channel in stereo input to individual mono files:
ffmpeg -i stereo.wav -map_channel 0.0.0 left.wav -map_channel 0.0.1 right.wav
or with the pan audio filer:
ffmpeg -i stereo.wav -filter_complex "[0:0]pan=1c|c0=c0[left];[0:0]pan=1c|c0=c1[right]" -map "[left]" left.wav -map "[right]" right.wav

stereo → 2 × mono streams

stereo to 2 mono streams diagram
Output each channel in stereo input to individual mono streams in one output file with the channelsplit audio filter:
ffmpeg -i in.mp3 -filter_complex channelsplit=channel_layout=stereo out.mka
Note: Your player will likely play the first stream by default unless your player allows you to select the desired stream.

mono → stereo

mono to stereo diagram
Create a stereo output from one mono input:
ffmpeg -i input.mp3 -ac 2 output.m4a
or with the amerge audio filter:
ffmpeg -i input.mp3 -filter_complex "[0:a][0:a]amerge=inputs=2[aout]" -map "[aout]" output.m4a
Note: These examples will not magically create a "true" stereo output from the mono input, but simply place the same audio into both the left and right channels of the output (both channels will be identical).

2 × mono → stereo

2 mono files to stereo diagram
Create a stereo output from two mono inputs with the amerge audio filter:
ffmpeg -i left.mp3 -i right.mp3 -filter_complex "[0:a][1:a]amerge=inputs=2[aout]" -map "[aout]" output.mka

6 × mono → 5.1

6 mono inputs to 5.1 output
Combine 6 mono inputs into one 5.1 (6 channel) output with the amerge audio filter:
ffmpeg -i front_left.wav -i front_right.wav -i front_center.wav -i lfe.wav -i back_left.wav -i back_right.wav \
-filter_complex "[0:a][1:a][2:a][3:a][4:a][5:a]amerge=inputs=6[aout]" -map "[aout]" output.wav
All inputs must have the same sample rate and format. If inputs do not have the same duration the output will stop with the shortest.

5.1 → 6 × mono

5.1 to individual channels
Split a 5.1 channel input into individual per-channel files:
ffmpeg -i in.wav \
-filter_complex "channelsplit=channel_layout=5.1[FL][FR][FC][LFE][BL][BR]" \
-map "[FL]" front_left.wav \
-map "[FR]" front_right.wav \
-map "[FC]" front_center.wav \
-map "[LFE]" lfe.wav \
-map "[BL]" back_left.wav \
-map "[BR]" back_right.wav

5.1 → stereo

5.1 to stereo diagram
To downmix you can simply use -ac 2:
ffmpeg -i 6channels.wav -ac 2 stereo.wav
Notes:
  • By default when using -ac 2 the LFE channel is omitted. See "Digital Audio Compression Standard (Document A/52:2012)", sections 6.1.12 and 7.8 for more downmixing info.
  • ffmpeg integrates a default down-mix (and up-mix) system that should be preferred (the -ac option) over the pan filter unless you have very specific needs.
If you want to map specific channels and drop the rest you can use the pan audio filter. This will map the FL (Front Left) of the input to the FL of the output, and the FR (Front Right) of the input to the FR of the output:
ffmpeg -i 6channels.wav -af "pan=stereo|c0=FL|c1=FR" stereo.wav
You can also map specific channels by number. This example will map the first and third channels of the input to the first and second channels of the output.
ffmpeg -i 6channels.wav -af "pan=stereo|c0=c0|c1=c2" output.wav
If the = in a channel specification is replaced by <, then the gains for that specification will be renormalized so that the total is 1, thus avoiding clipping noise. See the pan audio filter documentation for additional information and examples.

2 × stereo → stereo

2 stereo inputs to 1 stereo output diagram
Combine two stereo inputs into one stereo output with the amerge and pan audio filters:
ffmpeg -i input1.wav -i input2.wav -filter_complex "[0:a][1:a]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3[aout]" -map "[aout]" output.mp3
Or use -ac 2 instead of the pan audio filter:
ffmpeg -i input1.wav -i input2.wav -filter_complex "[0:a][1:a]amerge=inputs=2[aout]" -map "[aout]" -ac 2 output.mp3
Note: The output produced with the pan audio filter may not be identical to the output produced with -ac 2, so you'll have to listen to your outputs or view audio statistics to determine which output suits you.
2 stereo inputs to 1 stereo output diagram, alt
A similar situation as above, but instead use the left and right channels from the first input to make the left channel out the output, and use the left and right channels of the second input to make the right channel of the output.
Just change the channel specifications in the pan filter:
ffmpeg -i input1.wav -i input2.wav -filter_complex "[0:a][1:a]amerge=inputs=2,pan=stereo|c0<c0+c1|c1<c2+c3[aout]" -map "[aout]" output.mp3
The pan audio filter has to be used in this situation instead of -ac 2 unlike the previous example.

Mix both stereo channels to stereo

stereo to stereo mix diagram
The left and right channels of the output will each contain both the left and right channels of the input:
ffmpeg -i input.mp3 -af "pan=stereo|c0<c0+c1|c1<c0+c1" output.ogg

Switch stereo channels

switch stereo channels diagram
Switch left channel to right and right channel to left:
ffmpeg -i stereo.ogg -map_channel 0.0.1 -map_channel 0.0.0 output.wav
or with the pan audio filer:
ffmpeg -i stereo.ogg -af pan=stereo|c0=c1|c1=c0 output.wav

Virtual Binaural Acoustics

FFmpeg can produce virtual binaural acoustics files using sofalizer filter, most known channel layouts are supported for input, output is always stereo.
ffmpeg -i input.wav -af sofalizer=/path/to/sofa/file output.flac
SOFA files can be found on http://sofacoustics.org/data/database/ari/

Mute a channel

mute a stereo channel diagram
This example will mute the first channel (the left channel) but keep the second channel as is:
ffmpeg -i stereo.wav -map_channel -1 -map_channel 0.0.1 output.wav

Statistics

The astats audio filter can display information including length, DC offset, min/max levels, peak/RMS level dB:
$ ffmpeg -i input.wav -af astats -f null -
…
[Parsed_astats_0 @ 0x168a260] Channel: 1
[Parsed_astats_0 @ 0x168a260] DC offset: -0.001829
[Parsed_astats_0 @ 0x168a260] Min level: -0.605072
[Parsed_astats_0 @ 0x168a260] Max level: 0.607056
[Parsed_astats_0 @ 0x168a260] Peak level dB: -4.335430
[Parsed_astats_0 @ 0x168a260] RMS level dB: -20.298984
[Parsed_astats_0 @ 0x168a260] RMS peak dB: -12.303891
[Parsed_astats_0 @ 0x168a260] RMS trough dB: -35.352893
[Parsed_astats_0 @ 0x168a260] Crest factor: 6.283154
[Parsed_astats_0 @ 0x168a260] Flat factor: 0.000000
[Parsed_astats_0 @ 0x168a260] Peak count: 2
[Parsed_astats_0 @ 0x168a260] Channel: 2
[Parsed_astats_0 @ 0x168a260] DC offset: -0.001826
[Parsed_astats_0 @ 0x168a260] Min level: -0.585999
[Parsed_astats_0 @ 0x168a260] Max level: 0.608490
[Parsed_astats_0 @ 0x168a260] Peak level dB: -4.314931
[Parsed_astats_0 @ 0x168a260] RMS level dB: -20.519969
[Parsed_astats_0 @ 0x168a260] RMS peak dB: -12.056472
[Parsed_astats_0 @ 0x168a260] RMS trough dB: -36.784681
[Parsed_astats_0 @ 0x168a260] Crest factor: 6.460288
[Parsed_astats_0 @ 0x168a260] Flat factor: 0.000000
[Parsed_astats_0 @ 0x168a260] Peak count: 2
[Parsed_astats_0 @ 0x168a260] Overall
[Parsed_astats_0 @ 0x168a260] DC offset: -0.001829
[Parsed_astats_0 @ 0x168a260] Min level: -0.605072
[Parsed_astats_0 @ 0x168a260] Max level: 0.608490
[Parsed_astats_0 @ 0x168a260] Peak level dB: -4.314931
[Parsed_astats_0 @ 0x168a260] RMS level dB: -20.408071
[Parsed_astats_0 @ 0x168a260] RMS peak dB: -12.056472
[Parsed_astats_0 @ 0x168a260] RMS trough dB: -36.784681
[Parsed_astats_0 @ 0x168a260] Flat factor: 0.000000
[Parsed_astats_0 @ 0x168a260] Peak count: 2.000000
[Parsed_astats_0 @ 0x168a260] Number of samples: 1440706

Layouts

Output from ffmpeg -layouts:
Individual channels:
NAME        DESCRIPTION
FL          front left
FR          front right
FC          front center
LFE         low frequency
BL          back left
BR          back right
FLC         front left-of-center
FRC         front right-of-center
BC          back center
SL          side left
SR          side right
TC          top center
TFL         top front left
TFC         top front center
TFR         top front right
TBL         top back left
TBC         top back center
TBR         top back right
DL          downmix left
DR          downmix right
WL          wide left
WR          wide right
SDL         surround direct left
SDR         surround direct right
LFE2        low frequency 2

Standard channel layouts:
NAME        DECOMPOSITION
mono        FC
stereo      FL+FR
2.1         FL+FR+LFE
3.0         FL+FR+FC
3.0(back)   FL+FR+BC
4.0         FL+FR+FC+BC
quad        FL+FR+BL+BR
quad(side)  FL+FR+SL+SR
3.1         FL+FR+FC+LFE
5.0         FL+FR+FC+BL+BR
5.0(side)   FL+FR+FC+SL+SR
4.1         FL+FR+FC+LFE+BC
5.1         FL+FR+FC+LFE+BL+BR
5.1(side)   FL+FR+FC+LFE+SL+SR
6.0         FL+FR+FC+BC+SL+SR
6.0(front)  FL+FR+FLC+FRC+SL+SR
hexagonal   FL+FR+FC+BL+BR+BC
6.1         FL+FR+FC+LFE+BC+SL+SR
6.1         FL+FR+FC+LFE+BL+BR+BC
6.1(front)  FL+FR+LFE+FLC+FRC+SL+SR
7.0         FL+FR+FC+BL+BR+SL+SR
7.0(front)  FL+FR+FC+FLC+FRC+SL+SR
7.1         FL+FR+FC+LFE+BL+BR+SL+SR
7.1(wide)   FL+FR+FC+LFE+BL+BR+FLC+FRC
7.1(wide-side)FL+FR+FC+LFE+FLC+FRC+SL+SR
octagonal   FL+FR+FC+BL+BR+BC+SL+SR
downmix     DL+DR