How to use FFmpeg with examples

FFmpeg logo

According to Wikipedia FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams.

I will put here some commands that I personaly use.

Configuration

  • OS : ArchLinux
  • ffmpeg : 4.4

Commands

Videos

Compression

  • Compress with hardware acceleration :
user@host:~$ ffmpeg -hwaccel auto -i INPUT.mp4 -vcodec libx264 -crf 20 OUTPUT.mp4
  • Reduce video size :
user@host:~$ ffmpeg -i INPUT.mp4 -vcodec libx264 -crf 20 OUTPUT.mp4

Cut

  • Remove 5 first seconds of a video :
user@host:~$ ffmpeg -ss 5.00 -i INPUT.mp4 -c copy OUTPUT.mp4
  • Keeps first 120 seconds only of a video :
user@host:~$ ffmpeg -t 120 -i INPUT.mp4 -c copy OUTPUT.mp4

Video to Images

  • Convert a video to png files from 1h22m00s and for 4s :
user@host:~$ ffmpeg -ss 1:22:00 -t 4 -i /movies/1993.hot.shots.part.two.multi.1080p.bluray.x264.mkv -vf fps=10 $filename%05d.png
  • Convert a video to jpg files from 1h22m00s, for 4s and with a resolution of 320 pixels :
user@host:~$ ffmpeg -ss 1:22:00 -t 4 -i /movies/1993.hot.shots.part.two.multi.1080p.bluray.x264.mkv -vf fps=10,scale=320:-1 $filename%05d.jpg

Video to Audio

  • Extract audio from video :
user@host:~$ ffmpeg -i VIDEO.mp4 -b:a 192K -vn AUDIO.mp3
  • Extract 5 seconds from 10m of a video :
user@host:~$ ffmpeg -ss 10:00 -t 5 -i VIDEO.mkv -vn AUDIO.wav
  • Extract from mutliaudio file :
    • Get information :
user@host:~$ ffmpeg -i /movies/1998.The.Big.Lebowski.MULTI.1080p.Bluray.x264.mkv
  Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1920x1040 [SAR 1:1 DAR 24:13], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Metadata:
      LANGUAGE        : eng
      ENCODER         : Lavc55.63.100 libx264
  Stream #0:1(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s (default)
    Metadata:
      title           : VO
      LANGUAGE        : eng
      ENCODER         : Lavc55.63.100 ac3_fixed
  Stream #0:2(fra): Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
    Metadata:
      title           : VFF
      LANGUAGE        : fra
      ENCODER         : Lavc55.63.100 ac3_fixed
  Stream #0:3(fre): Subtitle: subrip (default)
    Metadata:
      title           : Fran├žais [Complets]
  Stream #0:4(fre): Subtitle: subrip
    Metadata:
      title           : Anglais [Complets]
    • Extract audio :
user@host:~$ ffmpeg -ss 36:00 -t 8 -i /movies/1998.The.Big.Lebowski.MULTI.1080p.Bluray.x264.mkv -map 0:1 -vn /tmp/OUTPUT.wav

Rotate

  • Rotate a video :
    • 0 = 90CounterCLockwise and Vertical Flip (default)
    • 1 = 90Clockwise
    • 2 = 90CounterClockwise
    • 3 = 90Clockwise and Vertical Flip
user@host:~$ ffmpeg -i INPUT.mp4 -vf "transpose=2" OUTPUT.mp4

Audio

Compress

Reducing wav file :
  • Audio rate : 16000 :
user@host:~$ ffmpeg -y -i INPUT.wav -ar 16000 OUTPUT.wav
  • Audio rate : 8000; codec pcm_s16le :
user@host:~$ ffmpeg -y -i INPUT.wav -ar 8000 -acodec pcm_s16le OUTPUT.wav
  • Audio rate : 8000; codec pcm_u8 :
user@host:~$ ffmpeg -y -i INPUT.wav -ar 8000 -acodec pcm_u8 OUTPUT.wav
  • Audio rate : 8000; audio chanel : 1 (mono) :
user@host:~$ ffmpeg -y -i INPUT.wav -ar 8000 -acodec pcm_u8 -ac 1 OUTPUT.wav

Convert

  • Convert stereo to mono with a wav file :
user@host:~$ ffmpeg -y -i INPUT.wav -ac 1 OUTPUT.wav
  • Convert mp3 to opus :
user@host:~$ ffmpeg -i INPUT.mp3 -c:a libopus -b:a 128k ${i%mp3}opus
  • Convert ogg to mp3 :
user@host:~$ ffmpeg -i INPUT.ogg -b:a 128K -vn "${i%ogg}"mp3

Misc

  • Media info :
user@host:~$ ffprobe -show_format -show_streams INPUT.mp4
  • Repair corrupted VOB file :
user@host:~$ ffmpeg -i VTS_06_5.VOB -target ntsc-dvd /tmp/OUTPUT.mp2
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :