├── FFMPEG-Snippets.png ├── README.md ├── assets ├── FFMPEG-Snippets.ai ├── FFMPEG-Snippets.png └── The.Code.FFMpeg.Scene.jpg └── scripts └── normalize.sh /FFMPEG-Snippets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in-tech-gration/FFmpeg-Snippets/38f7287629d4b58013c4bf033afd5171b434aab1/FFMPEG-Snippets.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![FFmpeg Snippets](FFMPEG-Snippets.png) 2 | 3 | ## List of useful ffmpeg commands for common tasks 4 | 5 | > FFMPEG is a video and audio converter as well as a live video/audio capture program with on-the-fly conversion capabilities. 6 | 7 | Installation: [Windows](https://www.wikihow.com/Install-FFmpeg-on-Windows) | [Linux](https://askubuntu.com/questions/426543/install-ffmpeg-in-ubuntu-12-04-lts?rq=1) | [Mac](https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/wiki/Installing-ffmpeg-on-Mac-OS-X) 8 | 9 | General Syntax: `ffmpeg [input file options] -i [output file options] ` 10 | 11 | References: [19 ffmpeg commands for all needs 12 | ](https://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs) 13 | 14 | Table of contents 15 | ================= 16 | 17 | * [Get Video Information](#get-video-information) 18 | * [Grab Frame Thumbnail](#grab-frame-thumbnail) 19 | * [Add Audio Track](#add-audio-track) 20 | * [Format Conversion](#format-conversion) 21 | * [Convert Video to MP3](#convert-video-to-mp3) 22 | * [Join / Concatenate MP3 Files](#join--concatenate-mp3-files) 23 | * [Mix a Video with a Sound File](#mix-a-video-with-a-sound-file) 24 | * [Trim and Cut](#trim-and-cut) 25 | * [Resizing Video](#resizing-video) 26 | * [Capture Video](#capture-video) [Linux] 27 | * [Crop Video](#crop-video) [Linux] 28 | * [Export Audio](#export-audio) 29 | * [Show Video Information as JSON](#show-video-information-as-json) 30 | * [Encode Video Portion](#encode-video-portion) 31 | * [Replace Audio in a Video File](#replace-audio-in-a-video-file) 32 | * [Audio Slow Down](#audio-slow-down) 33 | * [Create a Video with Audio from an Image Still](#create-a-video-with-audio-from-an-image-still) 34 | * [Extract Images from a Video](#extract-images-from-a-video) 35 | * [Convert VIDEO_TS folder to video](#convert-video_ts-folder-to-video) 36 | * [From left or right-only stereo to mono](#from-left-or-right-only-stereo-to-mono) 37 | * [Normalize/Boost audio](#normalize-boost-audio) 38 | * [Convert Images into a Video](#convert-images-into-a-video) 39 | * [Adding sound wave overlays to videos and pictures](#adding-sound-wave-overlays-to-videos-and-pictures) (Source: [Christian Heilmann](https://christianheilmann.com/2023/08/31/adding-sound-wave-overlays-to-videos-and-pictures-using-ffmpeg/)) 40 | 41 | [![](./assets/The.Code.FFMpeg.Scene.jpg)](https://www.youtube.com/embed/lsCrY2vWSr8?si=uir7ltKSuvi6YdJ3&start=205&end=290) 42 | 43 | [_(FFMpeg in the movies)_](https://www.youtube.com/embed/lsCrY2vWSr8?si=uir7ltKSuvi6YdJ3&start=205&end=290) 44 | 45 | --- 46 | 47 |
48 | 49 |

Get Video Information

50 | 51 | `$ ffmpeg -i filename.flv` 52 | 53 | `$ ffmpeg -ao dummy -vo dummy -identify filename.flv` 54 | 55 | `$ ffprobe -hide_banner -stats -i toggle-custom-post-types.mp4` 56 | 57 | GET SPECIFIC INFORMATION: 58 | 59 | `$ ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 input.mp4` 60 | 61 | Will output: `h264` 62 | 63 | `ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=nokey=1:noprint_wrappers=1 toggle-custom-post-types.mp4` 64 | 65 | Will output: `1280` 66 | 67 | Other stream keys include: 68 | 69 | codec_name=h264 70 | codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 71 | width=1280 72 | height=772 73 | r_frame_rate=8/1 74 | 75 | **References:** 76 | 77 | [Is there a way to use ffmpeg to determine the encoding of a file before transcoding?](https://stackoverflow.com/questions/5618363/is-there-a-way-to-use-ffmpeg-to-determine-the-encoding-of-a-file-before-transcod) 78 | 79 |
80 | 81 |
82 | 83 |

Grab Frame Thumbnail

84 | 85 | `$ ffmpeg -i input.mov -vframes 1 -s 320x240 -ss 10 thumb.jpg` 86 | 87 | -vframes *Single Frame*
88 | -ss *Offset* 89 | 90 | `$ ffmpeg -i rtmp://streamurl -r 1 frames/%04d-frame.png` 91 | 92 | This will consume the stream at rtmp://streamurl and output it as one PNG per second. 93 | 94 |
95 | 96 |
97 |

Add Audio Track

98 | 99 | `$ ffmpeg.exe -i input.flv -i input.audio.m4a -vcodec copy -acodec copy -map 0:0 -map 1:0 output.flv` 100 | 101 |
102 | 103 |
104 |

Format Conversion

105 | 106 | AAC to WAV 107 | 108 | `$ ffmpeg -i input.aac output.wav` 109 | 110 | FLV to MPEG4 111 | 112 | `$ ffmpeg -i input.flv -acodec copy output.mp4` 113 | 114 |
115 | 116 |
117 |

Convert Video to MP3

118 | 119 | MP3 Quality => 320k 120 | 121 | `$ ffmpeg -i video.flv -ab 320k output.mp3` 122 | 123 | `$ ffmpeg -i video.avi -f mp3 audio.mp3` 124 | 125 | -f *Force the format* 126 | 127 |
128 | 129 |
130 |

Join / Concatenate MP3 Files

131 | 132 | `$ ffmpeg -i "concat:file1.mp3|file2.mp3" -acodec copy output.mp3` 133 | 134 |
135 | 136 |
137 |

Join / Concatenate Video Files

138 | 139 | USING concat VIDEO FILTER (Performs re-encoding. Problem when dealing with videos of different resolution) 140 | 141 | `$ ffmpeg -i opening.mp4 -i content.mp4 -i ending.mp4 -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4` 142 | 143 | Use if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering. (You could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding the other inputs). 144 | 145 | USING concat DEMUXER () 146 | 147 | $ cat mylist.txt 148 | file '/path/to/file1' 149 | file '/path/to/file2' 150 | file '/path/to/file3' 151 | 152 | $ ffmpeg -f concat -i mylist.txt -c copy output 153 | 154 | Use when you want to avoid a re-encode and your format does not support file level concatenation (most files used by general users do not support file level concatenation). 155 | 156 | USING concat PROTOCOL 157 | 158 | $ ffmpeg -i "concat:input1|input2" -codec copy output 159 | 160 | This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method. 161 | 162 | Use with formats that support file level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4. 163 | 164 | Reference: [How to concatenate two MP4 files using FFmpeg?](https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg) 165 | 166 |
167 | 168 |
169 |

Mix a Video with a Sound file

170 | 171 | `$ ffmpeg -i audio.wav -i video.avi output.mpg` 172 | 173 |
174 | 175 |
176 |

Trim and Cut

177 | 178 | REMOVE LAST 30" FROM A VIDEO FILE 179 | 180 | Note: Video is 130" 181 | 182 | `ffmpeg -i input.mp4 -c:v copy -c:a copy -to 100 output.mp4` 183 | 184 | REMOVE FIRST 30" FROM AN MP3 FILE 185 | 186 | `$ ffmpeg -i input.mp3 -ss 30 -acodec copy output.mp3` 187 | 188 | Or (using the -c shortcut): 189 | 190 | `$ ffmpeg -i input.mp3 -ss 30 -c:a copy output.mp3` 191 | 192 | KEEP FIRST 30" OF A VIDEO FILE 193 | 194 | `$ ffmpeg -i input.mkv -t 30 -acodec copy -vcodec copy output.mkv` 195 | 196 | KEEP FIRST 30" OF AN MP3 FILE 197 | 198 | -t 30 *Keep only first 30 seconds* 199 | 200 | `$ ffmpeg -i input.mp3 -t 30 -acodec copy output.mp3` 201 | 202 | TRIM USING START AND END POSITION 203 | 204 | `$ ffmpeg -i input.mp4 -ss 00:00:03.000 -to 00:00:11.000 -c copy output.mp4` 205 | 206 | -ss and -to *Using start and end position* 207 | 208 | General Syntax: 209 | `$ ffmpeg -i [input file] -ss hh:mm:ss[.xxx] -t [duration in seconds or 210 | hh:mm:ss[.xxx]] -vcodec copy -acodec copy [output file]` 211 | 212 |
213 | 214 |
215 |

Resizing Video

216 | 217 | `$ ffmpeg -i input.avi -vf scale=320:240 output.avi` 218 | 219 | Reference: [Scaling](https://trac.ffmpeg.org/wiki/Scaling) 220 | 221 |
222 | 223 |
224 |

Capture Video

225 | 226 | [Linux] 227 | 228 | `$ ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/output.mpg` 229 | 230 |
231 | 232 |
233 |

Crop Video

234 | 235 | `$ ffmpeg -i input.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4` 236 | 237 | For parameters out_w, out_h, x and y see [this SO answer](https://video.stackexchange.com/a/4571/42182) 238 | 239 |
240 | 241 |
242 |

Export Audio

243 | 244 | `$ ffmpeg -i audio.aac outpuf.aiff` 245 | 246 | `$ ffmpeg -i video.avi -vcodec copy -acodec copy -ss 00:00:00 -t 00:00:04 trimmed_video.avi` 247 | 248 | FLV -> MP3 249 | 250 | `$ ffmpeg -i input.flv -acodec libmp3lame -aq 4 output.mp3` 251 | 252 | FLV -> WAV 253 | 254 | `$ ffmpeg -i input.flv -vn -f wav output.wav` 255 | 256 | MP4 -> MP4-AUDIO 257 | 258 | `$ ffmpeg -i input.flv -c copy -map 0:a output_audio.mp4` 259 | 260 | MP4 -> MP3 261 | 262 | `$ ffmpeg -i input.flv [-b:a 192K -vn] music.mp3` 263 | 264 | MP4 -> FLAC 265 | 266 | `$ ffmpeg -i audio.xxx -c:a flac audio.flac` 267 | 268 |
269 | 270 |
271 |

Show Video Information as JSON

272 | 273 | `$ ffprobe -v quiet -print_format json -show_format -show_streams somefile.asf` 274 | 275 |
276 | 277 |
278 |

Encode Video Portion

279 | 280 | `$ ffmpeg -i move.avi -ss -t OutPutFile.avi` 281 | 282 |
283 | 284 |
285 |

Replace Audio in a Video File

286 | 287 | `$ ffmpeg -i video.avi -i audio.mp3 -map 0.0:1 -map 1:0 -f avi -vcodec copy -acodec copy output.avi` 288 | 289 |
290 | 291 |
292 |

Audio Slow Down

293 | 294 | `$ ffmpeg -i input.mp4 -filter:a "atempo=0.5" -vn output.aac` 295 | 296 |
297 | 298 |
299 |

Create a Video with Audio from an Image Still

300 | 301 | Given an image and an audio file, creates a video which is basically a still from the image with the audio file in the background. 302 | 303 | The 66 below represents the length of the audio in seconds. 304 | 305 | CREATE A 66" VIDEO FROM THE IMAGE 306 | 307 | ```$ cat `for i in $(seq 1 66); do echo -n " black_still.jpg "; done;` | ffmpeg -r 1 -f mjpeg -i - -r 1 out1.mp4``` 308 | 309 | TRIM THE MP3 FILE TO KEEP THE FIRST 66" OF AUDIO 310 | 311 | `$ ffmpeg -i audio.mp3 -t 66 -acodec copy output.mp3` 312 | 313 | MIX AUDIO AND VIDEO 314 | 315 | `$ ffmpeg -i out1.mp4 -i output.mp3 -vcodec copy finish.mp4` 316 | 317 |
318 | 319 |
320 |

Extract Images from a Video

321 | 322 | `$ ffmpeg -i input.mpg image%d.jpg` 323 | 324 | This will create 25 images for every 1 second, but it may serve us to have more or less images, this can be achieved with the parameter -r 325 | 326 | -r fps *Set frame rate (default 25)* 327 | 328 | `$ ffmpeg -i test.mpg -r 1 image%d.jpg` 329 | 330 | With this command you’ll get 1 image for every second. 331 | 332 | You can also give a start time and the duration with the flags: 333 | 334 | -ss position Seek to given time position in seconds. “hh:mm:ss[.xxx]” syntax is also supported. 335 | 336 | -t duration Restrict the transcoded/captured video sequence to the duration specified in seconds. “hh:mm:ss[.xxx]” syntax is also supported. 337 | 338 | This command will take 25 images images every second beginning at the tenth second, and continuing for 5 seconds 339 | 340 | `$ ffmpeg -i test.mpg -r 25 -ss 00:00:10 -t 00:00:05 images%05d.png` 341 | 342 |
343 | 344 |
345 |

Convert VIDEO_TS folder to video

346 | 347 | `$ cat ./VIDEO_TS/*.VOB | ffmpeg -i - .` 348 | 349 | [References](https://askubuntu.com/questions/86320/how-to-convert-video-ts-folder-to-video-format) 350 | 351 |
352 | 353 |
354 |

From left or right only stereo to mono

355 | 356 | `ffmpeg -i INPUT.mp4 -c:v copy -ac 1 OUTPUT.mp4` 357 | 358 | [References](https://www.youtube.com/watch?v=IyQD6mYqrYA) 359 | 360 |
361 | 362 |
363 |

Normalize (boost) audio

364 | 365 | VIDEO: `ffmpeg -i original.mov -af "volume=18dB" -c:v copy -c:a aac -b:a 192k normalized.mov` 366 | 367 | **Important:** watch out for the typos, e.g. `18db` will fail, since the correct syntax is `18dB` (uppercase B). 368 | 369 | We also have created a handy shell (zsh) script that will normalize a file like this: 370 | 371 | ```bash 372 | normalize file.mov 373 | # Will create a file file.normalized.mov, normalized at 16dB (default) 374 | normalize --db 12 movie.mp4 375 | # Will create a file movie.normalized.mp4, normalized at 12dB 376 | ``` 377 | 378 | You can find the file here: [./scripts/normalize.sh](./scripts/normalize.sh) 379 | 380 | [References](https://superuser.com/questions/323119/how-can-i-normalize-audio-using-ffmpeg) 381 | 382 |
383 | 384 |
385 |

Convert Images into a Video

386 | 387 | Suppose you have images in the format: `frame01.jpg`, `frame02.jpg`, `frame03.jpg`, etc. 388 | 389 | `ffmpeg -f image2 -i frame%d.jpg output.mp4` 390 | 391 |
392 | 393 |
394 |

Adding Sound Wave Overlays to Videos and Pictures

395 | 396 | Adding sound wave overlay to a video: 397 | 398 | `ffmpeg -i Understandable.mp4 \ 399 | -filter_complex "[0:a]showwaves=colors=0xff1646@0.3\ 400 | :scale=sqrt:mode=cline,format=yuva420p[v];\ 401 | [v]scale=1280:400[bg];\ 402 | [v][bg]overlay=(W-w)/2:H-h[outv]"\ 403 | -map "[outv]" -map 0:a -c:v libx264 -c:a copy \ 404 | waveform-sqrt-cline.mp4` 405 | 406 | You can find a detailed explanation for each of the parameters [here](https://christianheilmann.com/2023/08/31/adding-sound-wave-overlays-to-videos-and-pictures-using-ffmpeg/). 407 | 408 | Adding sound wave overlay to a picture: 409 | 410 | `ffmpeg -i Understandable.mp4 -i chris.jpg\ 411 | -filter_complex "[0:a]showwaves=colors=0xff1646@0.3\ 412 | :scale=sqrt:mode=cline,format=yuva420p[v];\ 413 | [1:v]scale=400:400[bg];\ 414 | [bg][v]overlay=(W-w)/2:(H-h)/2[outv]"\ 415 | -map "[outv]" -map 0:a -c:v libx264 -c:a copy \ 416 | static-image.mp4` 417 | 418 |
419 | 420 | --- 421 | 422 | TODO 423 | ==== 424 | 425 | - Add entry about detecting the volume. Based on [this post](https://creatomate.com/blog/how-to-change-the-volume-of-a-media-file-using-ffmpeg) 426 | 427 | --- 428 | 429 | If you found this resource useful, please consider [supporting our cause](https://ko-fi.com/intechgration). 430 | 431 | For more information [visit our website](https://intechgration.io/). 432 | -------------------------------------------------------------------------------- /assets/FFMPEG-Snippets.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in-tech-gration/FFmpeg-Snippets/38f7287629d4b58013c4bf033afd5171b434aab1/assets/FFMPEG-Snippets.ai -------------------------------------------------------------------------------- /assets/FFMPEG-Snippets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in-tech-gration/FFmpeg-Snippets/38f7287629d4b58013c4bf033afd5171b434aab1/assets/FFMPEG-Snippets.png -------------------------------------------------------------------------------- /assets/The.Code.FFMpeg.Scene.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in-tech-gration/FFmpeg-Snippets/38f7287629d4b58013c4bf033afd5171b434aab1/assets/The.Code.FFMpeg.Scene.jpg -------------------------------------------------------------------------------- /scripts/normalize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | 3 | function normalize(){ 4 | 5 | dB="16" 6 | input="" 7 | arg_count=$# 8 | 9 | case "$arg_count" in 10 | 1) 11 | echo "No --db argument passed. Normalizing $1 at $dB" 12 | input="$1" 13 | ;; 14 | 2) 15 | echo "Invalid number of arguments." 16 | return 1; 17 | ;; 18 | 3) 19 | echo "Three arguments are passed: $1, $2, and $3" 20 | if [ "$1" == "--db" ]; then 21 | dB="$2" 22 | input="$3" 23 | echo "Normalizing at $dB" 24 | else 25 | dB="$3" 26 | input="$1" 27 | echo "Normalizing at $dB" 28 | fi 29 | ;; 30 | *) 31 | echo "Invalid number of arguments." 32 | return 1; 33 | ;; 34 | esac 35 | 36 | cmd="ffmpeg -i $input -af \"volume="$dB"dB\" -c:v copy -c:a aac -b:a 192k ${input%.*}.normalized.${input##*.}" 37 | 38 | echo $cmd 39 | eval "$cmd" 40 | 41 | } --------------------------------------------------------------------------------