Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Last active November 16, 2023 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liviaerxin/5d5787aecfcede100388a158c8e5a7d9 to your computer and use it in GitHub Desktop.
Save liviaerxin/5d5787aecfcede100388a158c8e5a7d9 to your computer and use it in GitHub Desktop.
FFmpeg samples #ffmpeg

FFmpeg Samples

FFmpeg Wiki

List private options of the codec

# Show available `presets`
ffmpeg -h encoder=h264_nvenc

List all frames timestamp

ffprobe -select_streams v -show_entries frame=pict_type,pts_time -of csv=p=0 -i input.mp4

List all keyframe(I-frame) timestamp

ffprobe -select_streams v -show_entries frame=pict_type,pts_time -of csv=p=0 -skip_frame nokey -i input.mp4

Reduce video size

Reduce video size by transcoding

# libx264
ffmpeg -i input.avi -vcodec libx264 -preset fast output.avi

# mp4v/mpeg4
ffmpeg -i input.avi -vcodec mpeg4 -preset fast output.avi

# NVIDIA GPU
ffmpeg -i input.avi -vcodec h264_nvenc -preset fast output.avi
# keep quality
ffmpeg -i input.avi -vcodec h264_nvenc -preset fast -rc constqp -cq 19 output.avi

Set keyframe interval

# mpeg4
ffmpeg -i input.avi -vcodec mpeg4 -preset fast -g 10 -keyint_min 10 -sc_threshold 0 output.avi 

# NVIDIA GPU
# This sets the I-frame interval at 10 and ensures that no I-frames will be inserted in scene changes
ffmpeg -i input.avi -vcodec h264_nvenc -preset fast -g 10 -keyint_min 10 -sc_threshold 0 output.avi

Cut/Clip video

# Fast clip with stream copy and faster seeking(700x)
ffmpeg -ss 00:00:10 -i video.mp4 -to 00:00:50 -c:v copy output.mp4

# Fast clip with stream copy and slower seeking(600x)
ffmpeg -i video.mp4 -ss 00:00:10 -to 00:00:50 -c:v copy output.mp4

# Slow clip with re-encoding and faster seeking(1x)
ffmpeg -ss 00:00:10 -i video.mp4 -to 00:00:50 -c:v libx264 output.mp4

📝 Cutting video with stream copy will lead the start frame is not precise!

Slow down/Speed up video

rescale timestamp(fast, copy, lossless, keep all frames)

# Slow down to 1/2x in fast way
ffmpeg -y -itsscale 2 -i video.mp4 -c:v copy output.mp4

# Speed up to 2x in fast way
ffmpeg -y -itsscale 0.5 -i video.mp4 -c:v copy output.mp4

# Speed up to 2x with re-encoding in slow way
ffmpeg -y -itsscale 0.5 -i video.mp4 -c:v libx264 output.mp4

📝 If the original input timestamp doesn't have a accurate resolution, it should set the timescale of the original video accurately in first by running ffmpeg -i input.mp4 -c copy -video_track_timescale 90k -an temp.mp4, then itsscale on the temp file.

setpts filter(re-encoding, slow, keep all frames)

# Speed up to 2x with `setpts filter` in slow way, keeping all frames
ffmpeg -i video.mp4 -filter:v "setpts=0.5*PTS" output.mp4

fps filter(re-encdoing, slow, drop frames but keep duration)

# Change fps to slow down/speed up but keeping duration
ffmpeg -i video.mp4 -filter:v "fps=30" output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment