Skip to content

Instantly share code, notes, and snippets.

@willprice
Created January 31, 2018 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willprice/b55202687aac7aa40fa61ffd5d7d9309 to your computer and use it in GitHub Desktop.
Save willprice/b55202687aac7aa40fa61ffd5d7d9309 to your computer and use it in GitHub Desktop.

FFmpeg Cheatsheet for video ML practitioners

Dump single frame from a video:

$ ffmpeg -ss 0.5 -i in.mp4 -t 1 -s 1920x1080 -f image2 out.jpg
# -ss specifies the start position in seconds, or timestamp
# -t the number of frames, in this case 1
# -s size of output
# -f image2 forces image output

Dump all frames from a video

$ ffmpeg -i in.mp4 frame_%06d.jpg
# %06d is a template for ffmpeg, 0 = zero padded, 6 = 6 digits, d = integer

HW accelerated frame dump and resize:

$ ffmpeg -hwaccel_device 0 - hwaccel cuvid -c:v h264_huvid -i in.mp4 \
         -vf 'scale_npp=-2:256,hwdownload,format=nv12' \
         -r 60 frame_%06d.jpg
# -hwaccel_device N specifies the GPU to use for hardware decoding
# -hwaccel cuvid specifies we're cuvid acceleration
# -c:v h264_cuvid specified h264 decoding using cuvid
# scale_npp is a hardware accelerated filter, it'll resize frames on the GPU (where they are decoded)
# hwdownload moves the frames off the GPU onto the CPU
# format=nv12 specifies the format to output frames from the filter, it is a type of YUV formatting
#     see https://www.fourcc.org/yuv.php for more details
# -r 60 specifies the output FPS of the frames, if you have a constant FPS video and don't want to change
#     the FPS then you can omit this flag, but if instead you have a variable FPS video and want a
#     constant time between frames then this flag MUST BE INCLUDED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment