Skip to content

Instantly share code, notes, and snippets.

@vladislav805
Last active January 13, 2024 18:58
Show Gist options
  • Save vladislav805/0ad4880729a29f32430bb5ebbd49da41 to your computer and use it in GitHub Desktop.
Save vladislav805/0ad4880729a29f32430bb5ebbd49da41 to your computer and use it in GitHub Desktop.

ffmpeg tips

Examples

Download HLS to file

ffmpeg -i input.m3u8 -c copy -bsf:a aac_adtstoasc output.mp4

Capture screen (Linux)

ffmpeg \
    -f x11grab \ # Video
    -select_region 1 \ # Manually draw area on display (default is 0)
    -draw_mouse 1 \ # Draw cursor on video (default is 1)
    -follow_mouse centered \ # "centered" - center of area follows the mouse, if specified number - follows only when the mouse pointer reaches within PIXELS
    -framerate 60 \ # FPS (default is NTSC=30000/1001=29.97)
    -show_region 1 \ # Show grabbed region on screen
    -video_size 1920x1080 \ # If not specify - fullscreen (all displays)
    -i :0.0 \ # Input video "[hostname]:display_number.screen_number[+x_offset,y_offset]"
    -f pulse \ # Audio
    -ac 2 \ # Audio channels
    -i default \ # Input audio
    output.mp4 # Output
# With audio. Check pavucontrol in tab "recording" device!

# One line
ffmpeg -f x11grab -select_region 1 -draw_mouse 1 -follow_mouse centered -framerate 60 -show_region 1 -video_size 1920x1080 -i :0.0 -f pulse -ac 2 -i default output.mp4
ffmpeg -framerate 24 -i img%03d.png -pix_fmt yuv420p output.mp4
# -start_number 126 
# -pix_fmt yuv420p - require for h264

# RegExp
ffmpeg -pattern_type glob -i "*.jpg" -an -vcodec h264 output.mp4
ffmpeg -i input.mp4 output%04d.png

Crop video

ffmpeg -i input.mp4 -vf "crop=<OUT_WIDTH>:<OUT_HEIGHT>:<IN_POS_X>:<IN_POS_Y>" -c:a copy output.mp4

# Can be used variables: in_w and in_h - sizes of input video

# In center auto
ffmpeg -i input.mp4 -vf "crop=in_h:in_h:in_w/2-in_h/2:0" -c:a copy output.mp4

Resize video

ffmpeg -i input.mp4 -vf scale=320:-1 -c copy output.mp4

Rotate video

ffmpeg -i input.mp4 -vf "transpose=N" output.mp4
# Where N:
# 0 = 90CounterCLockwise and Vertical Flip (default)
# 1 = 90Clockwise
# 2 = 90CounterClockwise
# 3 = 90Clockwise and Vertical Flip
# Current docs note that "Numerical values are deprecated, and
# should be dropped in favor of symbolic constants. Thus cclock_flip,
# clock, cclock or clock_flip instead of 0, 1, 2 or 3
# (ffmpeg.org/ffmpeg-filters.html#transpose)

Speed up/down

ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" output.mp4 # faster 2x
ffmpeg -i input.mp4 -filter:v "setpts=2*PTS" output.mp4 # slower 0.5x

Handle all files with extension in directory

old_extension=$1 # from ext
new_extension=$2 # to ext

for i in *."$old_extension";
  do ffmpeg -i "$i" -vcodec h264 -acodec aac <...> "${i%.*}.$new_extension";
done

# Using
script.sh avi mp4

Params

-video_size, -s

Size. Example: -s 1280x720

-ss

Start time. Example: -ss 00:00:30

If specified before -i, it very fast (input will be parsed using keyframes).

If specified after -i, it very slowly (frame by frame; the main advantage is that when applying filters to the output stream, the timestamps aren't reset prior to filtering, but the drawback is that it will take a lot of time until it finally reaches that time point).

Note that if you specify -ss before -i only, the timestamps will be reset to zero, so -t and -to will have the same effect. If you want to keep the original timestamps, add the -copyts option.

-t duration

Duration. Example: -t 00:00:15

Example: -ss 00:00:15 -t 00:00:10 (select 00:00:15-00:00:25)

-to duration

End timestamp. Example: -to 00:00:15

Example: -ss 00:00:15 -to 00:00:40 (select 00:00:15-00:00:40)

-framerate, -r

Frame rate. Example: -r 30

-an

No audio. Example: -an

-map <SOURCE_IDX>:p:<PROGRAM_NUMBER>

Get some stream from HLS. For get info about streams need execute only ffmpeg -i <URL>.

-vf, -v:filter

Video filter.

-af, -a:filter

Video filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment