Saturday, April 24, 2021

Handy ffmpeg Cheatsheet

 https://gist.github.com/steven2358/ba153c642fe2bb1e47485962df07c730

First convert the subtitles to .ass format:
````
ffmpeg -i sub.srt sub.ass
````
Then add them using a video filter:

````
ffmpeg -i in.mp4 -vf ass=sub.ass out.mp4
````

## Extract the frames from a video

To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:

````
ffmpeg -i in.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 out%d.png
````

To extract one frame per second only:

````
ffmpeg -i in.mp4 -fps=1 -vsync 0 out%d.png
````

## Rotate a video

Rotate 90 clockwise:

````
ffmpeg -i in.mov -vf "transpose=1" out.mov
````

For the transpose parameter you can pass:

````
0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip
````

Use `-vf "transpose=2,transpose=2"` for 180 degrees.

## Download "Transport Stream" video streams

1. Locate the playlist file, e.g. using Chrome > F12 > Network > Filter: m3u8
2. Download and concatenate the video fragments:

````
ffmpeg -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4
````

If you get a "Protocol 'https not on whitelist 'file,crypto'!" error, add the `protocol_whitelist` option:

````
ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4
````

## Mute some of the audio

To replace the first 90 seconds of audio with silence:

````
ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='lte(t,90)':volume=0" out.mp4
````

To replace all audio between 1'20" and 1'30" with silence:

````
ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='between(t,80,90)':volume=0" out.mp4
````

## Deinterlace

Deinterlacing using "yet another deinterlacing filter".

````
ffmpeg -i in.mp4 -vf yadif out.mp4
````

## Create a video slideshow from images

Parameters: `-r` marks the image framerate (inverse time of each image); `-vf fps=25` marks the true framerate of the output.

````
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
````

## Extract images from a video

- Extract all frames: `ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner`
- Extract a frame each second: `ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner`
- Extract only one frame: `ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg`

## Metadata: Change the title

````
ffmpeg -i in.mp4 -map_metadata -1 -metadata title="My Title" -c:v copy -c:a copy out.mp4

Sunday, April 18, 2021

Stripping audio from video

 Handy ffmpeg command to strip audio from video with high quality:

ffmpeg -i ../video/$i -q:a 0 -map a $i.mp3