Skip to content

ffmpeg | Cheatsheet

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. At its core is the command-line ffmpeg tool itself, designed for processing of video and audio files


Install media-video/ffmpeg

```bash echo "X alsa bzip2 dav1d encode gnutls gpl iconv mp3 network opengl postproc pulseaudio sdl svg threads truetype vorbis x264 xvid zlib cpudetection doc fontconfig jpeg2k libcaca libdrm librtmp libv4l lzma openh264 openssl samba ssh v4l webp x265"

/etc/portage/package/use/dnsmasq emerge --ask media-video/ffmpeg

Cut mp3 file from 30 seconds to 70

This command cuts an MP3 file from the 30-second mark to the 70-second mark and saves it as a new MP3 file without re-encoding the audio stream.

ffmpeg -ss 30 -t 70 -i inputfile.mp3 -acodec copy outputfile.mp3

Extract sound from movie file (-vn = no video)

This command extracts the audio stream from a movie file and saves it as an MP3 file.

ffmpeg -i input-video.mkv -vn -acodec copy output-audio.mp3

Cut mp3 file from 30 seconds to 70 only audio

This command cuts an MP3 file from the 30-second mark to the 70-second mark, encoding only the audio stream.

ffmpeg -i Molle_Musikvideo_Jobba,_Jobba,_Jobba-26tzRuurdgo.mkv -ss 56 -t 61 -q:a 0 -map a molle_-_jobba.jobba.mp3

Convert mp3 to ogg

This command converts an MP3 file to the OGG format.

ffmpeg -i input.mp3 -c:a libvorbis -q:a 4 output.ogg

Convert all mp3 files in current dir to ogg

This command converts all MP3 files in the current directory to the OGG format.

for f in ./*.mp3; 
  do ffmpeg -i "$f" -c:a libvorbis -q:a 4 "${f/%mp3/ogg}"; 
done

Convert all mp3 files in current dir to ogg recursive

This command converts all MP3 files in the current directory and its subdirectories to the OGG format recursively.

find . -type f -name '*.mp3' -exec bash -c 'ffmpeg -i "$0"  \
  -c:a libvorbis -q:a 4 "${0/%mp3/ogg}"' '{}' \;

Stream video and audio using RTP

Stream video and audio from a source to a destination using RTP protocol.

ffmpeg -i $src -an -vcodec [...details in description...] rtp rtp://$dstIP:$dstAudioPort4 -newaudio

Convert audio to FLAC

Convert an audio file to the FLAC format using SoX.

ffmpeg -loglevel 0 -y -i audio.mp3 -f sox - | sox -p -V -S -b24 -t audio.flac gain -3 rate -va 7056000 rate -v 48k

Capture video from webcam and audio from microphone

Capture video from a webcam and audio from the default microphone, and save it as an MP4 file.

ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 -f alsa -ac 1 -i default -f mp4 output.mp4

Convert video to AVI with specific codecs

Convert a video file to AVI format with MPEG-4 video codec and MP3 audio codec.

ffmpeg -i input_demo.ogv -vcodec mpeg4 -qscale 0 -acodec libmp3lame output.avi

Crop video

Crop a video file to a specific region.

ffmpeg -i input.avi -filter:v "crop=820:750:200:0" -qscale 0 output_cropped.avi

Extract frames from video

Extract frames from a video file and save them as individual images.

ffmpeg -i input.mp4 -r 15/1 frame%03d.png

Create video from frames

Create a video file from a sequence of images.

ffmpeg -f image2 -r 15 -i frame%04d.png -r 15 -b:v 8M output.avi

Add watermark to video

Add a watermark image to a video file.

ffmpeg -i input.mp4 -vhook '/usr/lib/vhook/watermark.so -f watermark.png -m 1 -t 222222' -an output.flv

Add text overlay to video

Add a text overlay to a video file.

ffmpeg -i input.flv -vhook '/usr/lib/vhook/imlib2.so -c white -x 250 -y H+(-1.8*N+80) -t Hallo! -A max(0,255-exp(N/16))' -sameq -acodec copy output.flv

Extract audio from video

Extract the audio stream from a video file and save it as an MP3 file.

ffmpeg -i input.flv -vn output.mp3

Convert images to video

Convert a sequence of images to a video file.

ffmpeg -r 12 -i img%03d.jpg -sameq -s hd720 -vcodec libx264 -crf 25 output.mp4

Convert video file format

Convert a video file from one format to another.

ffmpeg -i inputfile.mp4 outputfile.flv

Record desktop screen and audio

Record the desktop screen and audio and save it

as a MKV file.

ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1024x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 output.mkv

Trim audio file

Trim a section of an audio file.

ffmpeg -ss 00:00:30 -t 00:02:58 -i input.mp3 -acodec copy output.mp3

Create video from image and audio

Create a video file from an image and an audio file.

ffmpeg -loop 1 -i image.png -i audio.mp3 -shortest output.mp4

Convert video to 3GP format

Convert a video file to 3GP format.

ffmpeg -i input.flv -r 15 -b 128k -s qcif -acodec amr_nb -ar 8000 -ac 1 -ab 13 -f 3gp -y output.3gp

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -r 25 -i :0.0 -sameq /tmp/output.mpg

Record audio and video from devices

Record audio from ALSA and video from video4linux2 devices and save as AVI file.

ffmpeg -f alsa -r 16000 -i hw:2,0 -f video4linux2 -s 800x600 -i /dev/video0 -r 30 -f avi -vcodec mpeg4 -vtag xvid -sameq -acodec libmp3lame -ab 96k output.avi

Merge video and audio files

Merge a video file and an audio file into a single video file.

ffmpeg -i video.mp4 -i audio.mp3 -vcodec copy -acodec copy -map 0.0:0 -map 1.0:1 mix.mp4

Extract audio from video and convert to MP3

Extract the audio stream from a video file and save it as an MP3 file.

ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3

Convert video to animated GIF

Convert a video file to an animated GIF.

ffmpeg -i input.flv -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -loop 0 - output.gif

Record desktop screen with audio

Record the desktop screen with audio and save it as a MKV file.

ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y output.mkv

Convert video with experimental settings

Convert a video file with experimental settings to a specific format.

ffmpeg -i input_file.avi -strict experimental output_file.webm

Convert video to

MP4 for iPod

Convert a video file to MP4 format with specific settings for iPod.

ffmpeg -i [source].avi -f mp4 -vcodec mpeg4 -b 250000 -s 480?320 -acodec aac -ar 24000 -ab 64 -ac 2 [destination].mp4

Convert video to MPEG format

Convert a video file to MPEG format for VCD.

ffmpeg -i movie.avi -y -f vcd -vcodec mpeg1video -map 0.0:0.0 -b 1150 -s 352x240 -r 29.97 -g 12 -qmin 3 -qmax 13 -acodec mp2 -ab 224 -ar 44100 -ac 2 -map 0.1:0.1 movie.mpg

Trim video file

Trim a section of a video file.

ffmpeg -i input_file.mp4 -strict experimental -ss 00:01:30 -t 0:0:20 output_file.mp4

Convert audio with specific settings

Convert an audio file with specific audio codec and bitrate.

ffmpeg -f alsa -ac 2 -i hw:1,0 -acodec libmp3lame -ab 96k output.mp3

Fix broken video file

Fix a broken video file by copying the audio and video streams.

ffmpeg -i broken.flv -acodec copy -vcodec copy fixed.flv

Convert M4A to MP3

Convert an M4A audio file to MP3 format.

ffmpeg -i input.m4a -acodec libmp3lame -ab 128k output.mp3

Merge audio and video files with offset

Merge an audio file and a video file with an audio offset.

ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_video.flv

Extract audio from video and keep original format

Extract the audio stream from a video file and keep it in the original format.

ffmpeg -i file.ext -acodec copy -vn out.ext

Convert video to 3GP for mobile devices

Convert a video file to 3GP format with specific settings for mobile devices.

ffmpeg -i input.avi -s qcif -vcodec h263 -r 20 -b 180k -acodec libfaac -ab 64k -ac 2 -ar 22050 output.3gp

Extract audio from video and save as AC3 file

Extract the audio stream from a video file and save it as an AC3 file.

ffmpeg -ss 0:58:15 -i video.mpg -acodec copy audio.ac3

Capture video from video4linux device

Capture video from a video4linux device and save it as an AVI file.

ffmpeg -an -f video4linux -s 320x240 -b 800k -r 15 -i /dev/v4l/video0 -vcodec mpeg4 myvideo.avi

Merge video and audio files with specific mapping

Merge a video file and an audio file with specific mapping of streams.

ffmpeg -i video.mp4 -i audio.mp3 -vcodec copy -acodec copy -map 0.0:0 -map 1.0

:1 mix.mp4

Convert video to MP3

Convert a video file to MP3 format by extracting the audio stream.

ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3

Convert image sequence to video

Convert a sequence of images to a video file.

ffmpeg -i img%04d.png -c:v libx264 -vf fps=15 -pix_fmt yuv420p out.mp4

Merge MP3 files using concat protocol

Merge multiple MP3 files using the concat protocol.

ffmpeg -i "concat:$(find . -name "*.mp3" | sort | tr '\n' '|')" -acodec copy output.mp3 && mp3val -f output.mp3

Convert WAV to MOV

Convert a WAV audio file to MOV format.

ffmpeg -i "inputFile.wav" -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 368x208 M4V00002.MP4

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s wxga -r 25 -i :0.0+0,24 -qscale 0 /tmp/out.mpg

Extract audio from video and convert to MP3

Extract the audio stream from a video file and convert it to MP3 format.

ffmpeg -i video.avi -f mp3 audio.mp3

Create video from image and audio

Create a video file from an image and an audio file.

ffmpeg -r 24 -i %04d.png -i INPUTSOUND -r 24 -aspect 16:9 -s 1920x1080 -vcodec libx264 -vpre hq -acodec ac3 -b 40000k -shortest -threads 0 OUTFILE.mp4

Crop video file

Crop a video file to a specific region.

ffmpeg -acodec copy -vcodec copy -ss 00:05:00 -t 00:20:00 -i file.mp4 file_cropped.mp4

Copy audio and video streams with a timestamp

Copy audio and video streams from the input file to the output file with a specific timestamp.

ffmpeg -i "$input" -c copy -copyts "${input%.*}.mp4"

Crop video file with specific dimensions

Crop a video file to specific dimensions.

ffmpeg -i input.avi -croptop 88 -cropbottom 88 -cropleft 360 -cropright 360 output.avi

Trim video file with specific duration

Trim a video file to a specific duration.

ffmpeg -i video.avi -vcodec copy -acodec copy -ss 00:00:00 -t 00:00:04 trimmed_video.avi

Convert video for VCD

Convert a video file to VCD format.

ffmpeg -i movie.mkv -target vcd movie.avi

Increase volume of audio file

Increase the volume of an audio file.

ffmpeg -i foo.mp3 -vol 20 -acodec libmp3lame bar.mp3

Add overlay image to video

Add an overlay image to a video file.

ffmpeg -

i out.mp4 -i logo.png -filter_complex "overlay=0:0" -codec:a copy out2.mp4 -y

Record desktop screen

Record the desktop screen and save it as an MPG file.

ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg

Convert video with specific settings

Convert a video file with specific video and audio codecs.

ffmpeg -i video.mkv -vcodec libx264 -crf 22 -threads 0 video.mp4

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq ~/movie.mpg

Convert video with specific settings

Convert a video file with specific video and audio codecs and settings.

ffmpeg -i in.mkv -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre max -crf 22 -threads 0 out.mp4

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq ~/movie.mpg

Convert video with specific settings

Convert a video file with specific video and audio codecs and settings.

ffmpeg -i in.mkv -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre max -crf 22 -threads 0 out.mp4

Convert video with specific settings

Convert a video file with specific video and audio codecs and settings.

ffmpeg -i initial.mkv -acodec libvorbis -ab 128k -ac 2 -vcodec libx264 -vpre max -crf 22 -threads 0 final.mkv

Increase volume of audio file

Increase the volume of an audio file.

ffmpeg -i foo.mp3 -vol 20 -acodec libmp3lame bar.mp3

Add overlay image to video

Add an overlay image to a video file.

ffmpeg -i out.mp4 -i logo.png -filter_complex "overlay=0:0" -codec:a copy out2.mp4 -y

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq ~/movie.mpg

Convert video with specific settings

Convert a video file with specific video and audio codecs and settings.

ffmpeg -i in.mkv -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre max -crf 22 -threads 0 out.mp4

Convert video with specific settings

Convert a video file with specific video and audio codecs and settings.

ffmpeg -i input.mp4 -i audio.mp3 -vcodec copy -acodec copy -map 0.0:0 -map 1.0:1 mix.mp4

Extract

audio from video and convert to MP3

Extract the audio stream from a video file and convert it to MP3 format.

ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3

Convert video to animated GIF

Convert a video file to an animated GIF.

ffmpeg -i input.flv -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -loop 0 - output.gif

Record desktop screen with audio

Record the desktop screen with audio and save it as a MKV file.

ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y output.mkv

Convert video with experimental settings

Convert a video file with experimental settings to a specific format.

ffmpeg -i input_file.avi -strict experimental output_file.webm

Convert video to MP4 for iPod

Convert a video file to MP4 format with specific settings for iPod.

ffmpeg -i [source].avi -f mp4 -vcodec mpeg4 -b 250000 -s 480?320 -acodec aac -ar 24000 -ab 64 -ac 2 [destination].mp4

Convert video to MPEG format

Convert a video file to MPEG format for VCD.

ffmpeg -i movie.avi -y -f vcd -vcodec mpeg1video -map 0.0:0.0 -b 1150 -s 352x240 -r 29.97 -g 12 -qmin 3 -qmax 13 -acodec mp2 -ab 224 -ar 44100 -ac 2 -map 0.1:0.1 movie.mpg

Trim video file

Trim a section of a video file.

ffmpeg -i input_file.mp4 -strict experimental -ss 00:01:30 -t 0:0:20 output_file.mp4

Convert audio with specific settings

Convert an audio file with specific audio codec and bitrate.

ffmpeg -f alsa -ac 2 -i hw:1,0 -acodec libmp3lame -ab 96k output.mp3

Fix broken video file

Fix a broken video file by copying the audio and video streams.

ffmpeg -i broken.flv -acodec copy -vcodec copy fixed.flv

Convert M4A to MP3

Convert an M4A audio file to MP3 format.

ffmpeg -i input.m4a -acodec libmp3lame -ab 128k output.mp3

Merge audio and video files with offset

Merge an audio file and a video file with an audio offset.

ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_video.flv

Extract audio from video and keep original format

Extract the audio stream from a video file and keep it in the original format.

ffmpeg -i file.ext -acodec copy -vn out.ext

Convert video to 3GP for mobile devices

Convert a video file to 3GP format with specific settings for mobile devices.

ffmpeg -i input.avi -s qcif -vcodec h263 -r 20 -b 180k -acodec libfaac -ab 64k -ac 2 -ar 22050 output.3gp

Extract audio from video and save as AC3 file

Extract the audio stream from a video file and save it as an AC3 file.

ffmpeg -ss 0:58:15 -i video.mpg -acodec copy audio.ac3

Capture video from video4linux device

Capture video from a video4linux device and save it as an AVI file.

ffmpeg -an -f video4linux -s 320x240 -b 800k -r 15 -i /dev/v4l/video0 -vcodec mpeg4 myvideo.avi

Merge video and audio files with specific mapping

Merge a video file and an audio file with specific mapping of streams.

ffmpeg -i video.mp4 -i audio.mp3 -vcodec copy -acodec copy -map 0.0:0 -map 1.0:1 mix.mp4

Convert video to MP3

Convert a video file to MP3 format by extracting the audio stream.

ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3

Convert image sequence to video

Convert a sequence of images to a video file.

ffmpeg -i img%04d.png -c:v libx264 -vf fps=15 -pix_fmt yuv420p out.mp4

Merge MP3 files using concat protocol

Merge multiple MP3 files using the concat protocol.

ffmpeg -i "concat:$(find . -name "*.mp3" | sort | tr '\n' '|')" -acodec copy output.mp3 && mp3val -f output.mp3

Convert WAV to MOV

Convert a WAV audio file to MOV format.

ffmpeg -i "inputFile.wav" -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 368x208 M4V00002.MP4

Record screen video using x11grab

Record the screen video using x11grab and save it as MPG file.

ffmpeg -f x11grab -s wxga -r 25 -i :0.0+0,24 -qscale 0 /tmp/out.mpg

Extract audio from video and convert to MP3

Extract the audio stream from a video file and convert it to MP3 format.

ffmpeg -i video.avi -f mp3 audio.mp3

Create video from image and audio

Create a video file from an image and an audio file.

ffmpeg -r 24 -i %04d.png -i INPUTSOUND -r 24 -aspect 16:9 -s 1920x1080 -vcodec libx264 -vpre hq -acodec ac3 -b 40000k -shortest -threads 0 OUTFILE.mp4

Crop video file

Crop a video file to a specific region.

```bash ffmpeg -acodec copy -vcodec copy -ss 00:05:00 -t 00: