|
| 1 | +--- |
| 2 | +title: "FFmpeg" |
| 3 | +date: 2024-05-03T09:37:14+01:00 |
| 4 | +description: FFmpeg collection of useful video/audio editor commands |
| 5 | +categories: |
| 6 | + - FFmpeg |
| 7 | +tags: |
| 8 | + - ffmpeg |
| 9 | +math: false |
| 10 | +author: "Me" |
| 11 | +type: "post" |
| 12 | +layout: "post" |
| 13 | +cover: |
| 14 | + hidden: false # hide everywhere but not in structured data |
| 15 | + image: "covers/ffmpeg.jpeg" |
| 16 | + alt: |
| 17 | + caption: |
| 18 | + relative: true |
| 19 | +--- |
| 20 | + |
| 21 | +FFmpeg is a collection of libraries and tools to process multimedia content such |
| 22 | +as audio, video, subtitles and related metadata. |
| 23 | + |
| 24 | +<!--more--> |
| 25 | + |
| 26 | +# FFmpeg |
| 27 | + |
| 28 | +Any of the following global options can be seen in the [ffmpeg manpage](https://manpage.me/?q=ffmpeg) |
| 29 | + |
| 30 | +## Commands |
| 31 | + |
| 32 | +| Description | Command | |
| 33 | +| --------------- | --------------- | |
| 34 | +| Change Loglevel and print a less verbose stats | `ffmpeg -i input.mp4 -loglevel warning -stats output.mkv` | |
| 35 | +| Converting Media Formats | `ffmpeg -i input.mp4 output.mkv` | |
| 36 | +| Extracting Audio From Videos | `ffmpeg -i input.mp4 -vn audio.mp3` | |
| 37 | +| Resizing Videos | `ffmpeg -i input.mp4 -vf scale=1280:720 resized.mp4` | |
| 38 | +| Trimming a video (From 3s) | `ffmpeg -i input.mp4 -ss 00:03 -c copy cut.mp4` | |
| 39 | +| Trimming a video (To 5s) | `ffmpeg -i input.mp4 -to 00:05 -c copy cut.mp4` | |
| 40 | +| Trimming a video (From 3s to 5s) | `ffmpeg -i input.mp4 -ss 00:03 -to 00:05 -c copy cut.mp4` | |
| 41 | +| Trimming a video (From 3s during 5s) | `ffmpeg -i input.mp4 -ss 00:03 -t 00:05 -c copy cut.mp4` | |
| 42 | +| Adding Subtitles | `ffmpeg -i input.mp4 -vf "subtitles=subtitles.srt" output-subtitles.mp4` | |
| 43 | +| Creating a Video Slideshow from Images | `ffmpeg -framerate 1 -i img%03d.jpg slideshow.mp4` | |
| 44 | +| Extracting Frames from a Video | `ffmpeg -i input.mp4 -vf "select=mod(n\,100)" -vsync vfr frame%03d.png` | |
| 45 | +| Extracting a frame each second from a video | `ffmpeg -i input.mp4 -vf fps=1 output_%04d.jpg` | |
| 46 | +| Speeding Up or Slowing Down a Video | `ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" speed.mp4` | |
| 47 | +| Concatenating Videos | `ffmpeg -i "concat:input1.mp4\|input2.mp4\|input3.mp4" -c copy concatenated.mp4` | |
| 48 | +| Rotating a video (transpose=0,1,2,3 1=90º) | `ffmpeg -i input.mp4 -vf "transpose=1" rotated.mp4` | |
| 49 | +| Creating a video thumbnail (@ 3 seconds) | `ffmpeg -i input.mp4 -ss 00:03:00 -vframes 1 thumbnail.jpg` | |
| 50 | +| Extracting Audio Channels | `ffmpeg -i input.mp4 -map_channel 0.1.0 audio_channel.wav` | |
| 51 | +| Changing Volume (80% Volume) | `ffmpeg -i input.mp4 -filter:a "volume=0.80" output-80-volume.mp4` | |
| 52 | +| Creating a Video Loop (10min=600s loop) | `ffmpeg -stream_loop -1 -i input.mp4 -c copy -fflags +genpts -t 600 loop.mp4` | |
| 53 | +| Adding a Video Overlay (Between 0-20s @-10,10) | `ffmpeg -i main.mp4 -i overlay.mp4 -filter_complex "[0:v][1:v] overlay=main_w-overlay_w-10:10:enable='between(t,0,20)'" -pix_fmt yuv420p -c:a copy overlayed.mp4` | |
| 54 | +| Creating a Picture-in-Picture Effects (Between 0-20s @25,25) | `ffmpeg -i main.mp4 -i pip.mp4 -filter_complex "[0:v][1:v] overlay=25:25:enable='between(t,0,20)'" -pix_fmt yuv420p -c:a copy output-pip.mp4` | |
| 55 | +| Adding a Soundtrack to a Video | `ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output-with-audio.mp4` | |
| 56 | +| Looping video back and forth (loop 4 times @20fps) | `ffmpeg -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat,loop=4:250,setpts=N/20/TB" back-forth.mp4` | |
| 57 | + |
| 58 | +## [Concatenating videos by applying a filter and a concat file](https://trac.ffmpeg.org/wiki/Concatenate) |
| 59 | + |
| 60 | +This section covers concatenating videos by providing ffmpeg a file in a given |
| 61 | +syntax and by applying the `concat` filter. |
| 62 | + |
| 63 | +This approach could be useful if you want to concat multiple videos by a given |
| 64 | +section specific to each video. |
| 65 | + |
| 66 | +Example: |
| 67 | + |
| 68 | +``` |
| 69 | +# Comment |
| 70 | +file './input1.mkv' |
| 71 | +file './input2.mkv' |
| 72 | +inpoint 00:10 |
| 73 | +file './input3.mkv' |
| 74 | +outpoint 00:20 |
| 75 | +file './input4.mkv' |
| 76 | +inpoint 00:10 |
| 77 | +outpoint 00:20 |
| 78 | +``` |
| 79 | + |
| 80 | +```sh |
| 81 | +ffmpeg -f concat -safe 0 -i concat.txt -c copy -loglevel warning -stats concat.mkv |
| 82 | +``` |
0 commit comments