Skip to content

Instantly share code, notes, and snippets.

@mrxinu
Created August 14, 2023 13:02
Show Gist options
  • Save mrxinu/925d44db8bb87cbd112579c4bf5fc902 to your computer and use it in GitHub Desktop.
Save mrxinu/925d44db8bb87cbd112579c4bf5fc902 to your computer and use it in GitHub Desktop.

Output one image per second of the source (-i) video as photos/output_00001.png through the end.

ffmpeg -i your-video-file.mp4 -r 1 photos/output_%05d.png

Convert input media to another type:

ffmpeg -i input.avi output.mp4

Force the frame rate of the output file to be 24 fps:

ffmpeg -i input.avi -r 24 output.mp4

Force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:

ffmpeg -r 1 -i input.m2v -r 24 output.mp4
 _______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |
                                           v
                                       _________
                                      |         |
                                      | decoded |
                                      | frames  |
                                      |_________|
 ________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|

Output one image every 10 seconds:

ffmpeg -i ./DJI_0273.MP4 -r 0.1 images/output_%05d.png

Output a 1080 version of the images but without changing the ratio for the other number (-1):

ffmpeg -i ./DJI_0276.MP4 -vf scale=-1:1080 fps=1/4 images/swinging_smaller-%04d.png

The video filter options (-vf) need to come just before the thing being modified. If you're extracting a segment of a longer video and you want the result to be 1080p then you need to put that option just before the output file name.

ffmpeg -ss 03:44 -to 05:02 -i ./Nampa\ Flea\ Market\ 00004.MP4 -vf scale=-1:1080 ./curmudgeon.mp4

Get 35 seconds of images, one per second, starting at 1m 40s into the video:

ffmpeg -ss 01:40 -t 35 -i ./DJI_0282.MP4 %04d.png

Create a gif from the glob of images in the directory specified:

ffmpeg -pattern_type glob -i '*.png' ./gif-maybe.gif

Convert a 5-second segment of a video to a GIF, starting at the 3-second mark:

ffmpeg -ss 3 -to 8 -i input.mp4 -filter_complex "fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" output.gif

Ref: https://creatomate.com/blog/how-to-make-a-gif-from-a-video-using-ffmpeg

Convert avi file to mp4:

ffmpeg -i ./DVO00022.avi -strict -2 second.mp4

Extract from time code 2m10s to 3m1s to another file:

ffmpeg -ss 02:10 -to 03:01 -i ./output.mp4 first.mp4

Convert mp4 to a series of 1-second screenshots with 4-digit padding:

ffmpeg -i ./VID_20200924_091537_910.mp4 -r 1 screenshot_%04d.png

Convert mp4 to high quality gif scaled down to 320px retaining other dimensions:

ffmpeg -i biting.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 biting.gif

Same thing as above, but no change to size:

ffmpeg -i biting.mp4 -vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 biting-larger.gif

Slowing Down a Video

First convert it to raw format:

ffmpeg -i ./biting.mp4 -map 0:v -c:v copy -bsf:v h264_mp4toannexb raw.h264

Then slow it down, but this produced an error about time codes and deprecation that I didn't quite understand, but it still worked.

ffmpeg -fflags +genpts -r 5 -i raw.h264 -c:v copy output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment