Skip to content

Instantly share code, notes, and snippets.

@whittinghamj
Forked from tiantuxu/ffmpeg-common.sh
Created September 5, 2023 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whittinghamj/5eb8bf1f4dccc27ad064b5df29fe5df8 to your computer and use it in GitHub Desktop.
Save whittinghamj/5eb8bf1f4dccc27ad064b5df29fe5df8 to your computer and use it in GitHub Desktop.
Common ffmpeg Command Lines (with NVIDIA GPU Support)
# Decode a video to yuv420 raw format
ffmpeg -vsync 0 -c:v h264_cuvid -i input.mp4 -c:v rawvideo -pix_fmt yuv420p -f null /dev/null
ffmpeg -vsync 0 -c:v h264_cuvid -i input.mp4 -c:v rawvideo -pix_fmt yuv420p output.yuv
# Resize videos to 640x480
ffmpeg -c:v h264_cuvid -i input.mp4 -vf scale=640:480 -c:v h264_nvenc output.mp4
# Downsampling frame rate to 30fps
ffmpeg -i input.mp4 -r 30 -c:v h264_nvenc output.mp4
# Concat 2 videos
ffmpeg -i initial.mp4 -i ending.mp4 -filter_complex concat=n=2:v=1:a=0 -f MP4 output.mp4 -y
# Get first 20 minutes of a video
ffmpeg -i input.mp4 -ss 00:00:00 -t 00:20:00 -async 1 -c:v h264_nvenc output.mp4
# Check total number of frames
ffmpeg -i input.mp4 -vcodec copy -f rawvideo -y /dev/null 2>&1 | tr ^M '\n' | awk '/^frame=/ {print $2}'|tail -n 1
# Convert to grayscale videos
ffmpeg -i input.mp4 -vcodec rawvideo -pix_fmt gray output.mp4
# Split videos by odd(1,3,5,7,9…)/even(0,2,4,6,8…) frames
ffmpeg -i input -c:v h264_nvenc -vf select='not(mod(n\,2)),setpts=N/FRAME_RATE/TB' -r 60 output.mp4
# Slpit videos by I-frmaes (key frames)
ffmpeg -i input.mp4 -acodec copy -f segment -vcodec copy -reset_timestamps 1 -map 0 output%04d.mp4
# Crop a video, rect(out_w, out_h, x, y)
ffmpeg -i input.mp4 -c:v h264_nvenc -filter:v "crop=out_w:out_h:x:y" output.mp4
# Concat a video side by side
ffmpeg -i left.mp4 -i right.mp4 -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' -map [vid] -c:v h264_nvenc output.mp4
# Concat a video up and down
ffmpeg -i up.mp4 -i bottom.mp4 -filter_complex vstack output.mp4
# Merge images to videos
ffmpeg -starting_number 1 -r 60 -s 640x360 -i 'out%05d.png' -c:v h264_nvenc output.mp4
# Extract all I-frames
ffmpeg -i video.mp4 -vf select="eq(pict_type\,PICT_TYPE_I)" -vsync 0 frame%03d.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment