├── README.md └── ass-specs.pdf /README.md: -------------------------------------------------------------------------------- 1 | # Useful FFmpeg commands 2 | 3 | This repo contains examples for useful FFmpeg commands extracted from practical needs. 4 | 5 | ## What is FFmpeg? 6 | 7 | If you haven't met with [FFmpeg](https://www.ffmpeg.org/) yet, this page might be of little use to you. 8 | 9 | FFmpeg is a free, open-source software and it's the Swiss Army knife of video- and audio-related processing. It can be used to do an unbelievable range of things and it's being utilized by virtually anyone who's doing any form of video processing. It comes as a command-line tool, but many programs ship with a built-in version of FFmpeg in them to be able to process multimedia files. FFmpeg will run on Linux, Windows, OS X and other platforms. 10 | 11 | This is my personal collection of FFmpeg commands, recorded here for myself to come back to and check when I need something. I'm making it public because someone else might find some pieces of information here useful. 12 | 13 | **License:** Feel free to use the information here in any way you wish, no attribution is needed, but I take no responsibility for the results of your actions. 14 | 15 | Some of the commands have been taken verbatim from other sources, others have been heavily modified since. You can find some more commands here: 16 | 17 | - – the official FFmpeg wiki contains some very good examples. 18 | - 19 | 20 | Contributions are welcome. 21 | 22 | ## How to read this 23 | 24 | Just search the page for the example you need. 25 | 26 | The examples below have little to no context and usually do not explain all the different options used. That's what the documentation is for – you can [read it online here in this huge one-page HTML document](https://www.ffmpeg.org/ffmpeg-all.html) or just do `man ffmpeg-all` in your terminal. 27 | 28 | The order of the options in an FFmpeg command has some significance, but generally you can use any order you want. 29 | 30 | ## Extract audio from a video file (e.g. an YouTube clip) 31 | 32 | ffmpeg -i skrillex.mp4 -ab 256k skrillex.mp3 33 | 34 | ## Slice a 1-minute portion of an audio file starting at 2m30s, convert it to MP3 and add audio fade in and fade out effects 35 | 36 | ffmpeg -i INPUT.mp4 -c:a libmp3lame -b:a 128k -ss 00:02:30 -to 00:03:30 -af 'afade=t=in:st=150:d=3,afade=t=out:st=207:d=3' OUTPUT.mp3 37 | 38 | ## Slice the first 5 min of a video 39 | 40 | ffmpeg -i 038-Speed-Limit-260.avi -c copy -t 00:05:00 Simone_Origone_Speed_Limit_260.avi 41 | 42 | ## Slice 5 min starting from 12:30 from a video 43 | 44 | ffmpeg -i 038-Speed-Limit-260.avi -c copy -ss 00:12:30 -t 00:05:00 Simone_Origone_Speed_Limit_260.avi 45 | 46 | ## Create x264 mp4 videos suitable for online streaming 47 | 48 | The x264 codec provides a very good compression ratio and a good output quality. The options below are suitable for online embedding/streaming of a video. You will probably need an `webm` version as well as some browsers can't play the mp4 one (see below). Adjust the size and bitrates accordingly. 49 | 50 | ffmpeg -y -i in.suffix -filter:v scale=640:360,setsar=1/1 -pix_fmt yuv420p -c:v libx264 -preset:v slow -profile:v baseline -x264opts level=3.0:ref=1 -b:v 700k -r:v 25/1 -force_fps -movflags +faststart -c:a libfaac -b:a 80k -pass x out.mp4 51 | 52 | ## Create webm videos suitable for online streaming (no audio) 53 | 54 | Suitable for online streaming. You will probably need an mp4 version as well (see above.) Adjust the bitrates and resolution accordingly. 55 | 56 | NB: This example assumes the input has no sound. 57 | 58 | ffmpeg -i bff-teaser-part-1-1000kbps-no-sound.mp4 -filter:v scale=640:360,setsar=1/1 -pix_fmt yuv420p -vpre libvpx-720p -b:v 800k -r:v 25/1 -force_fps -c:a none -pass 2 bff-teaser-part-1-1000kbps-no-sound.webm 59 | 60 | ## Remove sound from webm videos 61 | 62 | ffmpeg -i bff-teaser-part-1-1000kbps-no-sound.mp4 -pix_fmt yuv420p -vpre libvpx-720p -b:v 800k -r:v 25/1 -force_fps -c:a none -pass 2 bff-teaser-part-1-1000kbps-no-sound.webm 63 | 64 | ## Replace video's audio with audio from an external audio file 65 | 66 | Based on [this StackOverflow answer](http://superuser.com/questions/800234/how-to-replace-an-audio-stream-in-a-video-file-with-multiple-audio-streams). 67 | 68 | ffmpeg -i VIDEO -i AUDIO -map 0:v -map 1:a -codec copy -shortest RESULT 69 | 70 | ## Concatenate files with different encodings (but the same resolution) 71 | 72 | A more detailed explanation can be found in [this FFmpeg wiki page](https://trac.ffmpeg.org/wiki/Concatenate). 73 | 74 | ffmpeg -threads 2 -i INPUT1 -i INPUT2 -filter_complex '[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' -c:v mpeg4 -preset:v slow -filter:v scale=768:576,setsar=1/1 -pix_fmt yuv420p -b:v 1900 -r:v 25 -force_fps -c:a mp3 -b:a 256 -s:a 48000 flying2.avi 75 | 76 | ## Create an mpeg4 video with mp3 sound 77 | 78 | This is suitable for playback on a wide range of slower devices. The quality options here are relatively good. 79 | 80 | ffmpeg -threads 2 -i input.avi -qmin 1 -qmax 3 -c:v mpeg4 -filter:v scale=768:576,setsar=1/1 -pix_fmt yuv420p -b:v 2400k -r:v 25 -force_fps -c:a mp3 -b:a 256k -s:a 48000 encoded.avi 81 | 82 | ## Create an mpeg4 video with mp3 sound and resize to 720p 83 | 84 | Used for processing videos for [Bansko Film Fest](http://banskofilmfest.com/en/). 85 | 86 | ffmpeg -i original.mp4 -threads 8 -c:v mpeg4 -c:a mp3 -vf scale=-2:720 -qmin 1 -qmax 4 encoded.avi 87 | 88 | ## Create a 5-second 720p video from an image 89 | 90 | ffmpeg -loop 1 -i INPUT.png -t 5 -c:v mpeg4 -vf scale=-2:720 -qmin 1 -qmax 3 OUTPUT-5s.avi 91 | 92 | The `-2` tells FFMpeg to calculate the output width so that the original aspect ratio is preserved but also to make sure the output width is divisible by 2 as some codecs require even numbers for video width and height. [Read more about scaling here](https://ffmpeg.org/ffmpeg-filters.html#Options-1). 93 | 94 | ## Rip a DVD to an mpeg4+mp3 AVI 95 | 96 | This assumes a PAL DVD. 97 | 98 | To rip a DVD with FFmpeg, you just need the `*.VOB` files in the `VIDEO_TS` folder. Read more about [ripping DVDs with FFmpeg here](http://www.commandlinefu.com/commands/view/6585/dvd-ripping-with-ffmpeg). 99 | 100 | cat VIDEO_TS/VTS_01_*VOB | ffmpeg -i - -s 720x576 -c:v mpeg4 -c:a mp3 -qmin 1 -qmax 3 -b:a 256k -b:v 4000k -y encoded.avi 101 | 102 | ## Rendering videos with subtitles (burn-in subtitles) 103 | 104 | This is tricky. A more detailed explanation can be found on [the FFmpeg wiki](https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo). 105 | 106 | The examples below assume you have subtitles in a SRT format, encoded in UTF-8. 107 | 108 | ### Add (render) subtitles to an already encoded video 109 | 110 | ffmpeg -i INPUT.avi -vf subtitles=SUBTITLES.srt -qmin 1 -qmax 3 OUTPUT-WITH-SUBS.avi 111 | 112 | ### Render subtitles at the top with a semi-transparent background 113 | 114 | This will embed the subtitles at the top of the video instead of at the bottom as usual. It will also render a semi-transparent black rectangle underneath the subtitle font. 115 | 116 | ffmpeg -i INPUT.avi -vf subtitles="f=SUBTITLES.srt:force_style='FontName=Arial,FontSize=16,OutlineColour=&H55000000,BorderStyle=3,Alignment=6'" ENCODED-WITH-SUBS.avi 117 | 118 | The syntax and options for customizing how subtitles are rendered are somewhat unintuitive. Read below to learn more. 119 | 120 | ### Render mp4 video (x264 + AAC) with embedded subtitles with a background 121 | 122 | This is similar to the command above and will render a video with embedded subtitles on a semi-transparent background and a bigger Arial font but in a mp4 container: 123 | 124 | ffmpeg -threads 0 -i INPUT.avi -c:v libx264 -c:a aac -b:a 196k \ 125 | -vf "subtitles=SUBTITLES.srt:force_style='FontName=Arial,FontSize=22,OutlineColour=&H22000000,BorderStyle=3'" \ 126 | -f mp4 -preset slow -profile:v baseline -x264opts level=3.0:ref=1 -b:v 2800k \ 127 | -y OUTPUT_WITH_SUBTITLES.mp4 128 | 129 | The background color is in a HEX format. The alpha channel is represented by the first two digits, `22`. The minimum is `00` (opaque) and the maximum is `FF` (fully transparent). The next three digit-pairs represent the red, green and blue channels, respectively. 130 | 131 | ### Add (render) subtitles with different font, colors, etc. 132 | 133 | Customizing the font, color, size, background color, etc. of the subtitles can be hard. The options for customizations are actually option names from the ASS subtitles spec ("Advanced SubStation Alpha"). This is what FFmpeg uses internally (via `libass`). These can be applied both with the `ass` and `subtitles` filters. When using the `subtitles` video filter, FFmpeg converts the subtitles internally to ASS. 134 | 135 | To see the available subtitle styling options, download this [Word doc file](http://moodub.free.fr/video/ass-specs.doc) (yes, indeed). I've also included [a PDF version of the Advanced SubStation Alpha (ASS) standard here](ass-specs.pdf). Use this to learn the meaning of the the different fields and their values present in the `force_style` setting from the example above. 136 | 137 | The example below prepares a video for burning on a video DVD. The subtitles have a semi-opaque rectangular back background. The `original_size` differs from the resize option because the input is assumed to be 16:9, even though it's stored in a physical resolution of 720x576 (if this confuses you, you're not alone; Google for "SAR" and "DAR"). 138 | 139 | ffmpeg -i INPUT.avi -target pal-dvd -s 720x576 -vf subtitles="f=SUBTITLES.srt:force_style='FontName=Arial,FontSize=22,OutlineColour=&H66000000,BorderStyle=3':original_size=720x405" -y ENCODED-WITH-SUBS.mpg 140 | 141 | ### Rip a DVD, burn-in subtitles and prepare for burning back to a DVD 142 | 143 | The subtitle styling is as in the previous example. See more about ripping DVDs above. More info [here](http://ffmpeg.gusari.org/viewtopic.php?f=25&t=1235). 144 | 145 | This is for a widescreen (16:9) PAL DVD input and prepares the video for burning it back to a DVD. 146 | 147 | cat VIDEO_TS/VTS_01_*.VOB | ffmpeg -i - -target pal-dvd -s 720x576 -vf subtitles="f=SUBTITLES.srt:force_style='FontName=Arial,FontSize=22,OutlineColour=&H66000000,BorderStyle=3':original_size=720x405" -y ENCODED-WITH-SUBS.mpg 148 | 149 | ### Render .ASS subtitles in a DVD video 150 | 151 | Steps: **Rip the DVD** → **add subs** → **mpg file for ripping back to a DVD** 152 | 153 | cat VIDEO_TS/VTS_01_[123456789].VOB | ffmpeg -i - -target pal-dvd -s 720x576 -vf ass=SUBTITLES.ass -y ENCODED-WITH-SUBS.mpg 154 | 155 | ### Convert SRT subtitles to ASS 156 | 157 | ffmpeg -i subtitles.srt subtitles.ass 158 | 159 | You can now edit the plaintext `.ass` file and change the styling accordingly. For documentation on the meaning of options and possible values, read above. 160 | 161 | Here is an excerpt of an `.ass` file for subtitles with a semi-transparent rectangular black background and a white Arial text. 162 | 163 | [V4+ Styles] 164 | Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding 165 | Style: Default,Arial,28,&Hffffff,&Hffffff,&H44000000,&H0,0,0,0,0,100,100,0,0,3,2,0,2,10,10,10,0 166 | 167 | ### Render .ASS subtitles in a video 168 | 169 | ffmpeg -i INPUT.avi -vf ass=SUBTITLES.ass -qmin 1 -qmax 3 OUTPUT-WITH-SUBS.avi 170 | 171 | ## Output to a PAL DVD with top and bottom black bars padding and subtitles 172 | 173 | This is adjusted for a `720:304` video. 174 | 175 | ffmpeg -i INPUT.avi -target pal-dvd -vf 'scale=720:304, pad=720:576:0:136, ass=subtitles.ass' -y OUTPUT-WITH-SUBS.mpg 176 | 177 | ## Prepare a video for burning on a PAL DVD 178 | 179 | ffmpeg -i input.avi -f:v scale=720:576 -target pal-dvd output.mpg 180 | 181 | To burn the DVD itself, on Linux you will have to use `dvdauthor` and `genisoimage`: 182 | 183 | export FOLDER_NAME="YOUR_MOVIE" 184 | export VIDEO_FORMAT=PAL 185 | dvdauthor --title -o $FOLDER_NAME -f out.mpg && dvdauthor -T -o $FOLDER_NAME 186 | genisoimage -dvd-video -o MOVIE.iso $FOLDER_NAME 187 | 188 | On OS X you can use [Burn](http://burn-osx.sourceforge.net/Pages/English/home.html) - it does the DVD video authoring and burning for you. It also has an embedded ffmpeg in it and can convert videos to the proper format if needed. 189 | -------------------------------------------------------------------------------- /ass-specs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitio/useful-ffmpeg-commands/c040f82d7438e67c8e7f119485735d0cdfc5a298/ass-specs.pdf --------------------------------------------------------------------------------