From charlesreid1

This page gives some detailed information about how to use ffmpeg to convert images to video, video to images, and video to video.

One of the most common uses I have for ffmpeg is stitching together lots of individual images to create a video. This is useful for, say, outputting plots from Matplotlib or Matlab at regular intervals, then stitching them together into a single video at the end.

Installing

ffmpeg is an all-in-one video conversion swiss army knife. The power of ffmpeg lies in its ability to convert just about anything to just about anything else.

You can do a number of different operations, but these can be grouped into a few categories, as they are here: image-to-video, video-to-image, and video-to-video.

2016 update: use brew!

If you're on a Mac, you can now use Brew to install ffmpeg! This saves you the headache of downloading/installing a dozen dependencies.

Getting the source

You can go to http://ffmpeg.org/download.html to download either a bleeding-edge SVN or Git version, or a release version.

Configuring

There's a very helpful guide to installing ffmpeg here: http://howto-pages.org/ffmpeg/

The executive summary is,

# Run configure
# Run make
# Run make install
./configure          \
 --enable-gpl        \
 --enable-liba52     \
 --enable-libxvid    \
 --enable-libamr_nb  \
 --enable-libamr_wb  \
 --enable-libmp3lame \
 --enable-libogg     \
 --enable-libvorbis  \
 --enable-libfaac    \
 --enable-libfaad    \
 --enable-shared     \
 --extra-cflags="-I/path/to/some/include -I/path/to/another/include" \
 --extra-libs="-L/path/to/some/lib -libsomelibrary -L/path/to/another/lib -libanotherlibrary"

--enable-liba52 is for encoding/decoding AC3 aduio

--enable-libxvid is for encoding/decoding using the DivX standard (which uses the XviD library, available at http://xvid.org)

--enable-libamr_nb and --enable-libamr_wb is for encoding/decoding narrowband and wideband audio streams from cellphones (linux-friendly versions of these libaries are available at http://www.penguin.cz/~utx/amr)

--enable-libmp3lame allows encoding/decoding MP3s

--enable-libogg and --enable-libvorbis and --enable-libfaac and --enable-libfaad allow you to stick it to the (fee-charging MP3-owning) man by using a free codec, available at http://sourceforge.net/projects/faac/

--extra-cflags allows you to point the compiler to include files that are not in standard locations (/usr/lib or /usr/local/lib). Useful if the codecs mentioned above are installed to non-standard locations.

--extra-libs allows you to point the compiler to additional libraries. Useful if the codecs mentioned above are installed to non-standard locations.

Using

It is easy to see the flexibility and limitless combinations of conversions possible by typing

$ ffmpeg

which returns all of the options available:

usage: ffmpeg [[infile options] -i infile]... {[outfile options] outfile}...
Hyper fast Audio and Video encoder

Main options:
-L                  show license
-h                  show help
-version            show version
-formats            show available formats, codecs, protocols, ...
-f fmt              force format
-img img_fmt        force image format
-i filename         input file name
-y                  overwrite output files
-t duration         set the recording time
-fs limit_size      set the limit file size
-ss time_off        set the start time offset
-itsoffset time_off  set the input ts offset
-title string       set the title
-timestamp time     set the timestamp
-author string      set the author
-copyright string   set the copyright
-comment string     set the comment
-v verbose          control amount of logging
-target type        specify target file type ("vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...)
-dframes number     set the number of data frames to record
-scodec codec       force subtitle codec ('copy' to copy stream)
-newsubtitle        add a new subtitle stream to the current output stream
-slang code         set the ISO 639 language code (3 letters) of the current subtitle stream

Video options:
-b bitrate          set video bitrate (in kbit/s)
-vframes number     set the number of video frames to record
-r rate             set frame rate (Hz value, fraction or abbreviation)
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-croptop size       set top crop band size (in pixels)
-cropbottom size    set bottom crop band size (in pixels)
-cropleft size      set left crop band size (in pixels)
-cropright size     set right crop band size (in pixels)
-padtop size        set top pad band size (in pixels)
-padbottom size     set bottom pad band size (in pixels)
-padleft size       set left pad band size (in pixels)
-padright size      set right pad band size (in pixels)
-padcolor color     set color of pad bands (Hex 000000 thru FFFFFF)
-vn                 disable video
-bt tolerance       set video bitrate tolerance (in kbit/s)
-maxrate bitrate    set max video bitrate tolerance (in kbit/s)
-minrate bitrate    set min video bitrate tolerance (in kbit/s)
-bufsize size       set ratecontrol buffer size (in kByte)
-vcodec codec       force video codec ('copy' to copy stream)
-sameq              use same video quality as source (implies VBR)
-pass n             select the pass number (1 or 2)
-passlogfile file   select two pass log file name
-newvideo           add a new video stream to the current output stream

Advanced Video options:
-pix_fmt format     set pixel format
-g gop_size         set the group of picture size
-intra              use only intra frames
-vdt n              discard threshold
-qscale q           use fixed video quantiser scale (VBR)
-qmin q             min video quantiser scale (VBR)
-qmax q             max video quantiser scale (VBR)
-lmin lambda        min video lagrange factor (VBR)
-lmax lambda        max video lagrange factor (VBR)
-mblmin q           min macroblock quantiser scale (VBR)
-mblmax q           max macroblock quantiser scale (VBR)
-qdiff q            max difference between the quantiser scale (VBR)
-qblur blur         video quantiser scale blur (VBR)
-qsquish squish     how to keep quantiser between qmin and qmax (0 = clip, 1 = use differentiable function)
-qcomp compression  video quantiser scale compression (VBR)
-rc_init_cplx complexity  initial complexity for 1-pass encoding
-b_qfactor factor   qp factor between p and b frames
-i_qfactor factor   qp factor between p and i frames
-b_qoffset offset   qp offset between p and b frames
-i_qoffset offset   qp offset between p and i frames
-ibias bias         intra quant bias
-pbias bias         inter quant bias
-rc_eq equation     set rate control equation
-rc_override override  rate control override for specific intervals
-me method          set motion estimation method
-me_threshold       motion estimaton threshold
-mb_threshold       macroblock threshold
-bf frames          use 'frames' B frames
-preme              pre motion estimation
-bug param          workaround not auto detected encoder bugs
-strict strictness  how strictly to follow the standards
-deinterlace        deinterlace pictures
-psnr               calculate PSNR of compressed frames
-vstats             dump video coding statistics to file
-vhook module       insert video processing module
-intra_matrix matrix  specify intra matrix coeffs
-inter_matrix matrix  specify inter matrix coeffs
-top                top=1/bottom=0/auto=-1 field first
-sc_threshold threshold  scene change threshold
-me_range range     limit motion vectors range (1023 for DivX player)
-dc precision       intra_dc_precision
-mepc factor (1.0 = 256)  motion estimation bitrate penalty compensation
-vtag fourcc/tag    force video tag/fourcc
-skip_threshold threshold  frame skip threshold
-skip_factor factor  frame skip factor
-skip_exp exponent  frame skip exponent
-genpts             generate pts
-qphist             show QP histogram

Audio options:
-aframes number     set the number of audio frames to record
-ab bitrate         set audio bitrate (in kbit/s)
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-newaudio           add a new audio stream to the current output stream
-alang code         set the ISO 639 language code (3 letters) of the current audio stream

Advanced Audio options:
-atag fourcc/tag    force audio tag/fourcc

Subtitle options:
-scodec codec       force subtitle codec ('copy' to copy stream)
-newsubtitle        add a new subtitle stream to the current output stream
-slang code         set the ISO 639 language code (3 letters) of the current subtitle stream

Audio/Video grab options:
-vd device          set video grab device
-vc channel         set video grab channel (DV1394 only)
-tvstd standard     set television standard (NTSC, PAL (SECAM))
-ad device          set audio device
-grab format        request grabbing using
-gd device          set grab device

Advanced options:
-map file:stream[:syncfile:syncstream]  set input stream mapping
-map_meta_data outfile:infile  set meta data information of outfile from infile
-benchmark          add timings for benchmarking
-dump               dump each input packet
-hex                when dumping packets, also dump the payload
-re                 read input at native frame rate
-loop_input         loop (current only works with images)
-loop_output        number of times to loop output in formats that support looping (0 loops forever)
-threads count      thread count
-vsync              video sync method
-async              audio sync method
-vglobal            video global header storage type
-copyts             copy timestamps
-shortest           finish encoding within shortest input
-dts_delta_threshold   timestamp discontinuity delta threshold
-ps size            set packet size in bits
-error rate         error rate
-muxrate rate       set mux rate
-packetsize size    set packet size
-muxdelay seconds   set the maximum demux-decode delay
-muxpreload seconds  set the initial demux-decode delay

Conversion operations

Images to video

Before you begin

If you have a big set of images, ffmpeg can convert them into a video. You have your choice of options: image format, video format, frames per second, bitrate, quality, etc etc etc.

There are a few things you have to do. First, your images have to be sequentially numbered, starting at 0 or 1. This means your files should be in the form

$ ls
file0001.jpg
file0002.jpg
file0003.jpg
file0004.jpg
file0005.jpg

etc... (Note that they don't have to be jpg format, that's just an example).

When you point ffmpeg to your images, you can't use the usual bash syntax, like ffmpeg -i file*.jpg (the -i is the flag telling ffmpeg which files are the input files). You have to use printf format, like this:

$ ffmpeg -i file%04d.jpg ...

where %04d means "a 4-digit number, zero-filled," and ffmpeg starts the number at 0 or 1.

You could also do:

$ ffmpeg -i file0%03d.jpg ...

where %03d means "a 3-digit number, zero-filled," and the 0 in front means the digit before that number is always 0.

Mass-Renaming Tricks

I had a bunch of files numbered sequentially from 400 to 740, and I needed the numbering to start at 1 and go to 341. To rename them, I used a Bash for loop with a little bit of Bash Math:

#!/bin/sh

for i in {400..740}; do 
    mv "moment_000000_t`printf "%04d" ${i}`.jpeg" "moment_000000_t`printf "%04d" $((i-399))`.jpeg"
done

Picking a video format

To see a list of video formats available to ffmpeg, you can run ffmpeg with the formats command:

$ ffmpeg -formats

File formats:
  E 3g2             3gp2 format
  E 3gp             3gp format
 D  4xm             4X Technologies format
 D  RoQ             Id RoQ format
 D  aac             ADTS AAC
 DE ac3             raw ac3
  E adts            ADTS AAC
 DE aiff            Audio IFF
 DE alaw            pcm A law format
 DE amr             3gpp amr file format
 DE asf             asf format
  E asf_stream      asf format
 DE au              SUN AU Format
 DE avi             avi format
 D  avs             avs format
  E crc             crc testing format
 D  daud            D-Cinema audio format
 D  dts             raw dts
 DE dv              DV video format
  E dvd             MPEG2 PS format (DVD VOB)
 D  ea              Electronic Arts Multimedia Format
 DE ffm             ffm format
 D  film_cpk        Sega FILM/CPK format
 D  flic            FLI/FLC/FLX animation format
 DE flv             flv format
  E framecrc        framecrc testing format
 DE gif             GIF Animation
 DE h261            raw h261
 DE h263            raw h263
 DE h264            raw H264 video format
 D  idcin           Id CIN format
 DE image           image sequence
 DE image2          image2 sequence
 DE image2pipe      piped image2 sequence
 DE imagepipe       piped image sequence
 D  ingenient       Ingenient MJPEG
 D  ipmovie         Interplay MVE format
 DE m4v             raw MPEG4 video format
 D  matroska        Matroska file format
 DE mjpeg           MJPEG video
 D  mm              American Laser Games MM format
 DE mmf             mmf format
  E mov             mov format
 D  mov,mp4,m4a,3gp,3g2,mj2 QuickTime/MPEG4/Motion JPEG 2000 format
  E mp2             MPEG audio layer 2
 DE mp3             MPEG audio layer 3
  E mp4             mp4 format
 DE mpeg            MPEG1 System format
  E mpeg1video      MPEG video
  E mpeg2video      MPEG2 video
 DE mpegts          MPEG2 transport stream format
 D  mpegvideo       MPEG video
  E mpjpeg          Mime multipart JPEG format
 DE mulaw           pcm mu law format
 D  nsv             NullSoft Video format
  E null            null video format
 DE nut             nut format
 D  nuv             NuppelVideo format
 D  ogg             Ogg
  E psp             psp mp4 format
 D  psxstr          Sony Playstation STR format
 DE rawvideo        raw video format
 D  redir           Redirector format
 DE rm              rm format
  E rtp             RTP output format
 D  rtsp            RTSP input format
 DE s16be           pcm signed 16 bit big endian format
 DE s16le           pcm signed 16 bit little endian format
 DE s8              pcm signed 8 bit format
 D  sdp             SDP
 D  shn             raw shorten
 D  smk             Smacker Video
 D  sol             Sierra SOL Format
  E svcd            MPEG2 PS format (VOB)
 DE swf             Flash format
 D  tta             true-audio
 DE u16be           pcm unsigned 16 bit big endian format
 DE u16le           pcm unsigned 16 bit little endian format
 DE u8              pcm unsigned 8 bit format
  E vcd             MPEG1 System format (VCD)
 D  vmd             Sierra VMD format
  E vob             MPEG2 PS format (VOB)
 DE voc             Creative Voice File format
 DE wav             wav format
 D  wc3movie        Wing Commander III movie format
 D  wsaud           Westwood Studios audio format
 D  wsvqa           Westwood Studios VQA format
 DE yuv4mpegpipe    YUV4MPEG pipe format

Image formats (filename extensions, if any, follow):
 DE gif    gif

Codecs:
 D V    4xm
 D V D  8bps
 DEA    aac
 D V D  aasc
 DEA    ac3
 DEA    adpcm_4xm
 DEA    adpcm_adx
 DEA    adpcm_ct
 DEA    adpcm_ea
 DEA    adpcm_ima_dk3
 DEA    adpcm_ima_dk4
 DEA    adpcm_ima_qt
 DEA    adpcm_ima_smjpeg
 DEA    adpcm_ima_wav
 DEA    adpcm_ima_ws
 DEA    adpcm_ms
 DEA    adpcm_sbpro_2
 DEA    adpcm_sbpro_3
 DEA    adpcm_sbpro_4
 DEA    adpcm_swf
 DEA    adpcm_xa
 DEA    adpcm_yamaha
 D A    alac
 DEA    amr_nb
 DEA    amr_wb
 DEV D  asv1
 DEV D  asv2
 D V D  avs
 D V    bmp
 D V D  camstudio
 D V D  camtasia
 D V D  cinepak
 D V D  cljr
 D A    cook
 D V D  cyuv
 DES    dvbsub
 DES    dvdsub
 DEV D  dvvideo
 DEV D  ffv1
 DEVSD  ffvhuff
 D A    flac
 D V D  flic
 DEVSD  flv
 D V D  fraps
 DEA    g726
 DEV D  h261
 DEVSDT h263
 D VSD  h263i
  EV    h263p
 DEV DT h264
 DEVSD  huffyuv
 D V D  idcinvideo
 D V D  indeo2
 D V    indeo3
 D A    interplay_dpcm
 D V D  interplayvideo
  EV    jpegls
 D V    kmvc
  EV    ljpeg
 D V D  loco
 D A    mace3
 D A    mace6
 D V D  mdec
 DEV D  mjpeg
 D V D  mjpegb
 D V D  mmvideo
 DEA    mp2
 DEA    mp3
 D A    mp3adu
 D A    mp3on4
 DEVSDT mpeg1video
 DEVSDT mpeg2video
 DEVSDT mpeg4
 D A    mpeg4aac
 D VSDT mpegvideo
 DEVSD  msmpeg4
 DEVSD  msmpeg4v1
 DEVSD  msmpeg4v2
 D V D  msrle
 D V D  msvideo1
 D V D  mszh
 D V D  nuv
 DEV    pam
 DEV    pbm
 DEA    pcm_alaw
 DEA    pcm_mulaw
 DEA    pcm_s16be
 DEA    pcm_s16le
 DEA    pcm_s24be
 DEA    pcm_s24daud
 DEA    pcm_s24le
 DEA    pcm_s32be
 DEA    pcm_s32le
 DEA    pcm_s8
 DEA    pcm_u16be
 DEA    pcm_u16le
 DEA    pcm_u24be
 DEA    pcm_u24le
 DEA    pcm_u32be
 DEA    pcm_u32le
 DEA    pcm_u8
 DEV    pgm
 DEV    pgmyuv
 DEV    png
 DEV    ppm
 D A    qdm2
 D V D  qdraw
 D V D  qpeg
 D V D  qtrle
 DEV    rawvideo
 D A    real_144
 D A    real_288
 D A    roq_dpcm
 D V D  roqvideo
 D V D  rpza
 DEV D  rv10
 DEV D  rv20
 D A    shorten
 D A    smackaud
 D V    smackvid
 D V D  smc
 DEV    snow
 D A    sol_dpcm
 DEA    sonic
  EA    sonicls
 D V D  sp5x
 DEV D  svq1
 D VSD  svq3
 D V    theora
 D V D  truemotion1
 D V D  truemotion2
 D A    truespeech
 D A    tta
 D V D  ultimotion
 D V    vc9
 D V D  vcr1
 D A    vmdaudio
 D V D  vmdvideo
 D A    vorbis
 D V    vp3
 D V D  vqavideo
 D A    wmav1
 D A    wmav2
 DEVSD  wmv1
 DEVSD  wmv2
 D V D  wnv1
 D A    ws_snd1
 D A    xan_dpcm
 D V D  xan_wc3
 D V D  xl
  EV    xvid
 DEV D  zlib
 D V    zmbv

Supported file protocols:
 file: pipe: udp: rtp: tcp: http:
Frame size, frame rate abbreviations:
 ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif
Motion estimation methods:
 zero(fastest) full(slowest) log phods epzs(default) x1 hex umh iter

Note, the names of encoders and decoders dont always match, so there are
several cases where the above table shows encoder only or decoder only entries
even though both encoding and decoding are supported for example, the h263
decoder corresponds to the h263 and h263p encoders, for file formats its even
worse

A few of the more popular video codecs include:

  • mpg, mjpeg (.mpg or mpeg format; the second one is specifically for converting series of jpeg images to mpegs)
    • This format doesn't allow you to control the frame rate (it's fixed at 30)
  • mov (.mov format)
  • avi (.avi format)
  • mpeg4 (.mp4 format)
  • wmv1/wmv2 (.wmv format)
  • gif (creates animated gifs, useful for short animations)

Typically you simply specify the extension of the output file, and the corresponding video codec is automatically used:

$ ffmpeg -i files%04d.jpg movie.mpg

Other times you may want to specify the video codec explicitly:

$ ffmpeg -i files%04d.jpg -vcodec mov movie.mov

Video output options

While there are a plethora of options to fiddle with, there are a couple important ones.


The first is the quality of the video. This assumes you don't care about the final size of the video, you simply want to specify the quality of the final video. This can be done using the -qscale argument. This outputs a video with a quality on the scale of 1 (the very best) to 30 (the very worst).

$ ffmpeg -i files%04d.jpg -qscale 1 movie.mov

If you don't specify a quality, the final video can potentially look really bad. Specifying the quality is a good way to check if the quality is bad because of the images you're using, or because of the video encoding process.


Another important option is the framerate. This controls how quickly your movie will progress. I typically use ffmpeg to visualize CFD results, and sometimes I want a slow video. To do this, I can set the framerate with the -r option, and give the value in Hz.

$ ffmpeg -i files%04d.jpg -r 10 movie.mov

This option doesn't work with mpg/mpeg movie formats though.


You can also set the bitrate by using the -b option,

$ ffmpeg -i files%04d.jpg -b 1200 movie.mov


And you can set the resolution to be X pixels by Y pixels with the -s XxY flag:

$ ffmpeg -i files%04d.jpg -s 320x180 movie.mov


You can set the aspect ratio as well, using the -aspect option:

$ ffmpeg -i files%04d.jpg -aspect 16:9 movie.mov


However, if the video is too tall/short or too wide/narrow, the picture will be distorted. If the input file is a video, you just run ffmpeg without specifying an output file or any options. However, if it's an image, you can just open it in any image editor and there should be an option to see what the resolution is.

Let's say we have a series of images that are 1200px x 900px, and we want to make them into a movie that has an aspect ratio of 16:9. The current aspect ratio is 4:3, and we want an aspect ratio of 16:9, so we have to crop the image.

Let's crop it vertically. We should get rid of 225 pixels from the top and bottom, or 112 pixels from the top and 112 pixels from the bottom. The extra pixel is ok because it will only stretch the image by 1 pixel.

$ ffmpeg -i files%04d.jpg -croptop 112 -cropbottom 112 -aspect 16:9 movie.mov

The options -cropleft and -cropright can also be used.

Just be careful not to crop any essential information from the image!

Slow Motion Video

The -r option should, in theory, slow down the framerate of the movie. However, the way it does this is pretty stupid. Instead of interpreting -r 10 as 10 frames per second, and thus using 10 images per second, it uses the default 26 frames per second, and just cuts out all but 10 images. So instead of 26 frames per second, it's 26 frames per second with the last 16 frames all being the same image.

To get around this idiocy, you can duplicate images to stretch everything out. The only downside is that this only allows an integer factor of stretching (unless you want to get fancy).

So let's say you have 100 images you want to stretch out into slow motion, and you want your video to be twice as slow. Then rename each of the 100 images so their numbers range from 1-200 but are all even (0, 2, 4, 6, 8, etc.). Then, copy image 0 into image 1, image 2 into image 3, and so on. In this way, your frames will show up for twice the amount of time they would have originally.

So, whereas before you would have had:

image000.jpg
image001.jpg
image002.jpg
image003.jpg
image004.jpg
image005.jpg

now every even image is an original, and every odd image is a symbolic link to the corresponding original:

image000.jpg
image001.jpg -> image000.jpg
image002.jpg 
image003.jpg -> image002.jpg
image004.jpg
image005.jpg -> image004.jpg
image006.jpg
image007.jpg -> image006.jpg
image008.jpg
image009.jpg -> image008.jpg
image010.jpg
image011.jpg -> image010.jpg

You can do this with a little bit of Bash Math, as well as some of the renaming tricks listed on the Xargs page.

Video to images

A video can be converted into a set of images using the command,

ffmpeg -i movie.mpg image%03d.jpg

The output image format can also be png, jpeg, gif, or tiff.

If you want to grab a single image from the video, e.g. to create a thumbnail, you can issue the command:

ffmpeg -ss 3 -i movie.mov -f image2 -vframes 1 thumb.jpg

which will capture a thumbnail image 3 seconds into the video, using "image2" as the output format (this works for outputting jpg files, but other formats can be set with the -f option), and grabbing only N frames (set with the -vframes N option). If I wanted to grab 5 frames, and create a different image for each, I could issue the command:

ffmpeg -ss 3 -i movie.mov -f image2 -vframes 5 thumb%02d.jpg

which would yield the files thumb01.jpg,thumb02.jpg, etc...

To extract images from the video at a given rate (frames or thumbnails per second), you can use the -r N option. This will output N thumbnails for each second of video.

For example, to output 2 thumbnails per second of video:

ffmpeg -r 2 -i movie.mov -f image2 thumb%02d.jpg

The rate can also be a fraction. For example, to output 1 thumbnail for every 5 minutes (300 seconds) of video, set the rate to be 1/300:

ffmpeg -r 1/300 -i movie.mov -f image2 thumb%02d.jpg

This can also be used in combination with -vframes to limit the number of thumbnails saved. Likewise, the option -t N limits thumbnail capturing to N seconds, or -t HH:MM:SS to limit thumbnail capturing for HH hours, MM minutes, and SS seconds.

Video to video

See examples below, Ffmpeg#Video_to_video_2

Audio to Audio

One of the great features of ffmpeg is that it can convert audio files, too. If you have your songs locked up in Apple's ridiculous M4A/AAC format, you can use ffmpeg to break them out.

The easiest way to do this is to use ffmpegx, which gives a nice gui interface and makes it easy to do this.

Using the command-line interface is also very easy. To convert 1.m4a into 1.mp3:

$ ffmpeg -i 1.m4a -acodec mp3 -ac 2 -ab 128 1.mp3

Video and Audio Muxing

You can use ffmpeg to split and combine audio and video streams. A couple of common operations real quick:

Removing Audio from Video

Here's how to remove audio from a video with an audio track:

ffmpeg -i movie_withsound.mov -vcodec copy -an movie_nosound.mov

The -vcodec copy means copy the video, the -an means use nothing for the audio.

Muxing Audio with Video

Video Has No Sound

If you have an audio file, and a video file with no sound, you can mux the two like this:

$ ffmpeg -i sound.mp3 -i movie_nosound.avi -acodec copy -vcodec copy movie_withsound.avi

Video Has Sound

If the video already has sound, and you want to replace it with a different audio track, you can do this:

$ ffmpeg -i sound.mp3 -i movie_withsound.avi -map 0:0 -map 1:0 -acodec copy -vcodec copy -shortest movie_withdifferentsound.avi

Here, the map arguments refer to the streams: numbered 0 and 1.

The first -map 0:0 means copy the video from stream 0 to stream 0. (Keep the original video.)

The second -map 1:0 means copy the audio from stream 1 to stream 0. (Copy the sound file stream into the new video stream.)

You can imagine that these notations would get particularly handy if you're dealing with multiple video files and multiple audio tracks.

Shortening

What if the length of your video and your audio are different? By default, ffmpeg is conservative and doesn't delete anything. The length of the final video will be the length of the longer of the two videos (a three minute video track and a ten minute audio track will result in seven minutes of black screen).

You can tell ffmpeg to trim the length to the shortest of the two, by using the -shortest flag.

Gifs

See Gifs (specifically the Gifs#ffmpeg section) for how to use ffmpeg to create/convert gifs.

Scripts, tricks, and tips

There's a very handy method for renaming files covered here: Xargs#Moving_Files

Examples

Images to video

mpeg


# Convert a set of .jpeg images to an .mpeg movie
$ ffmpeg -i moment0_instant_%03d.jpeg movie_noqual.mpg

# Specify a high quality
$ ffmpeg -i moment0_instant_%03d.jpeg -qscale 1 movie_hiqual.mpg

# Explicitly specify the video codec
$ ffmpeg -i 

# You can't specify a frame rate with the mpeg video codec...
# This will throw an error!
$ ffmpeg -i moment0_instant_%03d.jpeg -qscale 1 -r 10 movie_highqual_framerate10.mpg


mp4


# Convert a set of .jpeg images to an .mp4 movie
$ ffmpeg -i moment0_instant_%03d.jpeg -vcodec mpeg4 movie_noqual.mp4

# Specify a high quality
$ ffmpeg -i moment0_instant_%03d.jpeg -vcodec mpeg4 -qscale 1 movie_hiqual.mp4

# Set a framerate of 10 fps (default is 30 fps)
$ ffmpeg -i moment0_instant_%03d.jpeg -vcodec mpeg4 -qscale 1 -r 10 movie_hiqual_framerate10.mp4


mov


# Convert a set of .jpeg images to a .mov movie
$ ffmpeg -i moment0_instant_%03d.jpeg movie_noqual.mov

# Specify a high quality
$ ffmpeg -i moment0_instant_%03d.jpeg -qscale 1 movie_highqual.mov

# Set a framerate of 20 fps (default is 30 fps)
$ ffmpeg -i moment0_instant_%03d.jpeg -qscale 1 -r 20 movie_highqual_framerate20.mov


Video to video

mov to mpg


# Convert an .mov file into an .mpg file
$ ffmpeg -i input.mov -r 25 -qscale 1 output.mpg

# You can also specify the bitrate:
$ ffmpeg -i input.mov -r 25 -b 7500000 -qscale 1 output.mpg

Combining Videos

Same Resolution

If we want to combine two videos and display them side-by-side in-frame, we can use the pad and overlay features. Links:

The basic strategy is to pad the left video with extra spacing on the right side, then overlay the right video in the extra space created.

ffmpeg -i maya.mp4 -i cronus.mp4 -filter_complex "[0:v:0]pad=width=iw*2:ih[bg]; [bg][1:v:0]overlay=w" output.mp4

The first thing the command does is provide two inputs with the -i flags. Next, the -filter_complex flag will filter/process/combine the inputs and combine them into a single video. Basically we feed filter complex a string, which contains a command that will combine the videos in some particular way.

The command that is passed to filter_complex starts with [0:v:0] and later has a [1:v:0], where the left side of the v refers to which input, and the right side of the output refers to which output. In this case we have two inputs, so the 0 and 1 refer to maya.mp4 or cronus.mp4, respectively. There is only one output so we probably don't need the :0 at the end, but we add it anyway just to be sure.

We are telling filter complex to pad the first video by doubling it, and then we overlay the second video. There is a bit of magic [1] happening here, but the [bg] placement tells it which video to put on which side. Finally, we tell the command where to output all of this stuff.

My final result looked awful because the two videos had completely different resolutions. Thus, I had to make do.

Different Resolutions

I had a high-resolution and a low-resolution video, and didn't want to reduce the resolution of both to be poor, so I modified the above overlay command to eliminate the padding:

ffmpeg -i maya.mp4 -i cronus.mp4 -filter_complex "[0:v:0]pad=0[bg]; [bg][1:v:0]overlay=w" output.mp4

This led to the following video output: https://vimeo.com/229620373

Flags