Skip to main content

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec - the leading audio/video codec library.

# To extract the sound from a video and save it as MP3:
ffmpeg -i <video.mp4> -vn <sound>.mp3

# To convert frames from a video or GIF into individual numbered images:
ffmpeg -i <video.mpg|video.gif> <frame_%d.png>

# To combine numbered images (frame_1.jpg, frame_2.jpg, etc) into a video or GIF:
ffmpeg -i <frame_%d.jpg> -f image2 <video.mpg|video.gif>

# To quickly extract a single frame from a video at time mm:ss and save it as a 128x128 resolution image:
ffmpeg -ss <mm:ss> -i <video.mp4> -frames 1 -s <128x128> -f image2 <image.png>

# To trim a video from a given start time mm:ss to an end time mm2:ss2 (omit the -to flag to trim till the end):
ffmpeg -ss <mm:ss> -to <mm2:ss2> -i <video.mp4> -codec copy <output.mp4>

# To convert AVI video to MP4. AAC Audio @ 128kbit, h264 Video @ CRF 23:
ffmpeg -i <input_video>.avi -codec:audio aac -b:audio 128k -codec:video libx264 -crf 23 <output_video>.mp4

# To remux MKV video to MP4 without re-encoding audio or video streams:
ffmpeg -i <input_video>.mkv -codec copy <output_video>.mp4

# To convert MP4 video to VP9 codec. For the best quality, use a CRF value (recommended range 15-35) and -b:video MUST be 0:
ffmpeg -i <input_video>.mp4 -codec:video libvpx-vp9 -crf <30> -b:video 0 -codec:audio libopus -vbr on -threads <number_of_threads> <output_video>.webm

# -----------------------------------------------------------------------------

##
# To join/concatenate two mp4 videos (use double-quotes in Windows):
#
# http://www.kolor.com/wiki-en/action/view/Autopano_Video_-_Concatenate_several_mp4
##
ffmpeg -i one.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts one.ts
ffmpeg -i two.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts two.ts
ffmpeg -i "concat:one.ts|two.ts" -c copy -bsf:a aac_adtstoasc combined.mp4
# cleanup
rm -f one.ts two.ts


# To print file metadata:
ffmpeg -i path/to/file.ext

# To convert a video to compatible HTML5 video format (mp4): (https://gist.github.com/yellowled/1439610)
ffmpeg -i infile.ext -acodec aac -strict experimental -ac 2 -ab 128k -vcodec libx264 -preset slow -f mp4 -crf 22 outfile.mp4

# To convert all m4a files to mp3:
for f in *.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 320k "${f%.m4a}.mp3"; done

# To generate a 10-second audio clip:
#
#         -ss : start time
#         -t  : seconds to cut
#   -autoexit : closes ffplay as soon as the audio finishes
ffmpeg -ss 00:34:24.85 -t 10 -i path/to/file.mp4 -f mp3 pipe:play | ffplay -i pipe:play -autoexit

# To generate a 5-second video clip (from the beginning "-ss 0" of video):
#
#   -ss : start time of clip (position)
#   -t  : duration of clip
ffmpeg -i video.mp4 -ss 0 -t 5 -vcodec copy -acodec copy clip.mp4

# Get media file info
ffmpeg -i video.avi

# Turn X images to a video sequence
ffmpeg -f image2 -i image%d.jpg video.mpg

# Turn a video to X images
ffmpeg -i video.mpg image%d.jpg

# Encode a video sequence for the iPpod/iPhone
ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4

# Extract 2 images for each second of the video starting at 30-seconds and ending 40-seconds
ffmpeg -i video.mp4 -r 2 -ss 00:00:30 -t 00:00:10 img%d.jpg

# Extracting sound from a video, and save it as Mp3
ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3

# Convert a wav file to Mp3
ffmpeg -i son_origine.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3

# Crop an audio file
#
# "`-acodec copy` is to keep the original sound, `-ss 00:00:25` is to tell where
# to start cropping and `-t 00:02:00` is the length of the cropped output file.
ffmpeg -i input.mp3 -acodec copy -ss 00:00:25 -t 00:02:00 output.wav

# Crop a video file
#
# The only change in this command is that you need to define the video encoder
# when cropping video files, the usual option is "-vcodec copy" to keep
# the original video.
ffmpeg -i input.avi -vcodec copy -acodec copy -ss 00:00:25 -t 00:02:00 output.flv

# Convert .avi video to .mpg
ffmpeg -i video_origine.avi video_finale.mpg

# Convert .mpg to .avi
ffmpeg -i video_origine.mpg video_finale.avi

# To convert the entire video to GIF, use the following command:
ffmpeg -i small.mp4 small.gif

# To convert a gif to video:
ffmpeg -f gif -i animation.gif animation.mp4

# To convert just a portion of a video clip to GIF, use the following command:
ffmpeg -t 3 -ss 00:00:02 -i small.webm small-clip.gif

# Mix a video with a sound file
ffmpeg -i son.wav -i video_origine.avi video_finale.mpg

# Convert .avi to .flv
ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv

# Convert .avi to dv
ffmpeg -i video_origine.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 video_finale.dv
# Or:
ffmpeg -i video_origine.avi -target pal-dv video_finale.dv

# Convert .avi to mpeg for dvd players
ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg

# Compress .avi to divx
ffmpeg -i video_origine.avi -s 320x240 -vcodec msmpeg4v2 video_finale.avi

# Compress Ogg Theora to Mpeg dvd
ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film_termin.mpg

# Compress .avi to SVCD mpeg2 (NTSC format)
ffmpeg -i video_origine.avi -target ntsc-svcd video_finale.mpg

# Compress .avi to SVCD mpeg2 (PAL format)
ffmpeg -i video_origine.avi -target pal-svcd video_finale.mpg

# Compress .avi to VCD mpeg2 (NTSC format)
ffmpeg -i video_origine.avi -target ntsc-vcd video_finale.mpg

# Compress .avi to VCD mpeg2 (PAL format)
ffmpeg -i video_origine.avi -target pal-vcd video_finale.mpg

# Multi-pass encoding with ffmpeg
ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

# Convert to wmv
ffmpeg -i winter_clip.mov -vcodec wmv2 -b 12000k -ar 44100 -ab 192k -ac 2 -y -s 720x406 output.wmv

# Write metadata `title` to mp4 clip (without re-encoding)
ffmpeg -loglevel quiet -v 0 -i clip.mp4 -metadata title="jon test clip" -acodec copy -vcodec copy -copyts cliptitle.mp4

# Gather stream information and print it in JSON format
ffprobe -loglevel quiet -show_format -show_streams -i clip.mp4 -print_format json

# Transcode to WebM with ffmpeg (http://paulrouget.com/e/funwithwebm/)
ffmpeg -i girltalk.mp4 -f webm -vcodec libvpx -acodec libvorbis -aq 90 -ac 2 girltalk.webm

# Convert to ipad
ffmpeg -y -i input.avi -acodec aac -ar 48000 -ab 128k -ac 2 -s 1024x768 -vcodec libx264 -b 1200k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 1200k -maxrate 1200k -bufsize 1200k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 16:9 -r 30 -g 90 -async 2 output.mp4

# Create a single image video with audio
ffmpeg -loop 1 -i image.png -i sound.mp3 -shortest video.mp4

# Find all .flv files in a dir, grab the first frame and make a jpg.
for f in *.flv; do
    ffmpeg -y -i "$f" -f image2 -ss 10 -vframes 1 -an "${f%.flv}.jpg";
done

# Convert .wma files to .ogg with ffmpeg
find -name '*wma' -exec ffmpeg -i {} -acodec vorbis -ab 128k {}.ogg \;

# Synchronize audio with video by adding a 0.5 second (1/2 second) delay to the audio stream without re-encoding the file (https://alien.slackbook.org/blog/fixing-audio-sync-with-ffmpeg/):
ffmpeg -i out_of_sync_file.mp4 -itsoffset 0.5 -i out_of_sync_file.mp4 -map 0:0 -map 1:1 -acodec copy -vcodec copy synced_file.mp4

# Removing syncronization problems between audio and video
#
# This assumes that there is a 10.2 sec delay between the video and the
# audio (delayed). To extract the original video into a audio and video
# composites look at the command on extracting audio and video from a movie.
ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_video.flv

# Cut Out A Piece Of Film From A File. Choose an arbitrary length and starting time.
ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile

# Extract Audio And Video From A Movie
# Rips the audio and video stream of a movie. The two streams are stored separately.
ffmpeg -i source_movie.flv -vcodec mpeg2video target_video.m2v -acodec copy target_audio.mp3

# Rotate a video file 90 clockwise (http://stackoverflow.com/a/9570992):
ffmpeg -i in.mov -vf "transpose=1" out.mov

##
# ffmpeg, libx264 and presets (for web encoding)
#
# Here the source can be either mp4 or some other file format as FLV for instance
#   -- http://www.stoimen.com/blog/2010/10/26/ffmpeg-libx264-and-presets/
##
ffmpeg -i source.mp4 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre normal -threads 0 -crf 22 output.mp4

###
# Convert Adobe Flash FLV Files to MP4 Files
# http://www.guguncube.com/1103/ffmpeg-convert-adobe-flash-flv-files-to-mp4-files
###

# Simple Conversion of FLV to MP4 using FFMPEG
find . -regextype posix-extended -iregex ".*.avi|.*.flv" -type f | while IFS= read -r file;
do
    fn="${file}";
    echo "###$fn###";
    dest="${fn%.*}.mp4";
    echo "###$dest###";
    if [ -f "$dest" ];then
        rm "$dest";
    fi;
    ffmpeg -nostdin -v 0 -i "$fn" -acodec copy -vcodec copy -copyts "$dest";
done;

# Convert FLV to MP4 through container copy
#   NOTE: only works if video content is H264 and audio AAC or MP3
ffmpeg -i input.flv -acodec copy -vcodec copy -copyts output.mp4

# Transcode FLV to MP4
ffmpeg -i input.flv -copyts output.mp4

# Re-encode Audio
ffmpeg -i input.ext -vcodec copy -acodec libfaac -ab 128k -copyts output.mp4

# Copy Directory FLV TO MP4 (Version 1)
for f in *.flv; do
  fn="${f%.*}";
  ffmpeg -i "$f" -acodec copy -vcodec copy -copyts "$fn.mp4";
done;

# Copy Directory FLV TO MP4 (Version 2)
for f in *.flv; do a=$(echo $f|sed -e 's/.flv/.mp4/ig');
    echo $a;
    ffmpeg -i "$f" -acodec copy -vcodec copy -copyts "$a";
done;

# It you want particular prefixes for the Android, something like:
ffmpeg -i source-video.avi -s 480x320 -vcodec mpeg4 -acodec libfaac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 output-video.G1.mp4
# My ffmpeg didn't want -aspect 3:2 and you can omit it.

# Convert FLV files in tree to mp4 -- preserve already existing files and MP4
find . -name "*.flv" -type f -exec ffmpeg -n -i '{}' -acodec copy -vcodec copy -copyts '{}.mp4' ;

# convert directory tree from AVI, FLV to mp4
find . -regextype posix-extended -iregex ".*.avi|.*.flv" -type f -exec ffmpeg -n -i '{}' '{}.mp4' ;
# -regextype posix-extended -iregex ".*.txt|.*.mp4"
for f in *; do fn="${f%.*}";echo "fn=$fn,f=$f"; done;
for f in *.flv.mp4; do fn="${f%.*}";echo "$fn,f=$f"; done;

# Remove FLV.MP4 double extensions
for f in *.flv.mp4; do fn="${f%.*}";fn="${fn%.*}";mv "$f" "$fn.mp4"; done;

# Converts flac files to mp3 (shell)
for FILE in *.flac;
do
    ffmpeg -i "$FILE" -ab 320k "${FILE%.*}.mp3";
done

##
# How to encode video in H264 format
#
# We have successfully been using ffmpeg/libx264 with two pass encoding using
# the following commands
#
# http://h264.code-shop.com/trac/wiki/Encoding#HowtoencodevideoinH264format
##

## Ffmpeg/x264 (profile High, level 3.0) (latest versions of x264)
infile="video.avi"
  tmpfile="video_tmp.mp4"
  outfile="video.mp4"
  options="-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256
     -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8
     -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3
     -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16
           -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10
     -qmax 51 -qdiff 4"

  ffmpeg -y -i "$infile" -an -pass 1 -threads 0 $options "$tmpfile"
  ffmpeg -y -i "$infile" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 0 $options "$tmpfile"
  qtfaststart "$tmpfile" "$outfile"

## Ffmpeg/x264 (profile High, level 3.0) (older versions)
  options="-vcodec libx264 -b 512k -bf 3 -subq 6 -cmp 256 -refs 5 -qmin 10
           -qmax 51 -qdiff 4 -coder 1 -loop 1 -me hex -me_range 16 -trellis 1
           -flags +mv4 -flags2 +bpyramid+wpred+mixed_refs+brdo+8x8dct
           -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -g 250
           -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71"

## Ffmpeg/x264 (profile Baseline, level 3.0) (iPhone)
  options="-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256
     -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8
     -me_method hex -subq 7 -trellis 1 -refs 5 -bf 0
     -flags2 +mixed_refs -coder 0 -me_range 16
           -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10
     -qmax 51 -qdiff 4"

##
# Windows BAT script
#
# Encode XVID H.263 to H.264 video, copy source audio stream.
##

echo OFF
cls

for %%f in (*.avi) do (
    echo "%%~nf"

    ffmpeg -i "%%~nf.avi" -vcodec libx264 -acodec copy -copyts -threads auto -y "%%~nf.mp4"
)

pause "Done."

##
# Encoding for web
#
# In the following replace x in -pass x with 1 and 2 for the first and second
# pass/run respectively. We assume the original movie in.suffix has an aspect
# ratio of 16/9 and a frame rate of 25 frames per second.
#
# http://flowplayer.org/docs/encoding.html
##

# WEBM encoding sample
ffmpeg -y -i in.suffix -filter:v scale=640:360
       -vpre libvpx-720p -b:v 500k -r:v 25/1 -force_fps
       -c:a libvorbis -b:a 80k -pass 1 out.webm

# MP4 encoding sample
ffmpeg -y -i in.suffix -filter:v scale=640:360 -pix_fmt yuv420p
       -c:v libx264 -preset:v slow -profile:v baseline
       -x264opts level=3.0:vbv-maxrate=10000:vbv-bufsize=10000:ref=1
       -b:v 700k -r:v 25/1 -force_fps -movflags +faststart
       -c:a libfaac -b:a 80k -pass 1 out.mp4

##
# Include subtitles from the input file
#
# https://askubuntu.com/a/214351
##
ffmpeg -i input.mkv \
    -map 0 \
    -vcodec libx264 \
    -acodec aac -ac 2 -b:a 256k \
    -scodec mov_text -metadata:s:s:0 language=eng -metadata:s:s:1 language=ipk \
    output.mp4

# Convert flac audio files to mp3:
# Source: https://stackoverflow.com/questions/26109837/convert-flac-to-mp3-with-ffmpeg-keeping-all-metadata
# Source: https://superuser.com/questions/1349737/converting-flac-to-mp3-using-ffmpeg
ffmpeg.exe -i file.flac -vcodec copy -ab 320k file.mp3