Skip to content

Instantly share code, notes, and snippets.

@tsumare
Last active May 19, 2020 13:43
Show Gist options
  • Save tsumare/5a26bc454d40831a34f9 to your computer and use it in GitHub Desktop.
Save tsumare/5a26bc454d40831a34f9 to your computer and use it in GitHub Desktop.
ffmpeg notes & useful commands
# Image output:
# -r: framerate of output
ffmpeg -i inputfile.mp4 -r 1 -f image2 image-%3d.jpeg # one per second
ffmpeg -i inputfile.mp4 -frames:v 1 thumb.png # one
# crop=w:h:xoffset:yoffset
-vf "crop=610:475:835:385"
# scale to w:h. -1 is 'preserve aspect ratio in this dimension' -2 is 'preserve aspect ratio but keep the dimension a multiple of 2'
-vf 'scale=-1:min(iw\,480)'
#-vf 'scale=trunc((oh/a)/2)*2:trunc(min(ih\,480)/2)*2' # -1:min(orig,480), if I can't use -1 and get confused by a TAG:rotate=90 in the video source
# Rotate/Hardcode Rotation
-metadata:s:v rotate="0" -vf "hflip,vflip"
# Define audio languages
ffmpeg -i input.avi -map 0 -codec copy -metadata:s:a:0 language=eng -metadata:s:a:1 language=jpn output.avi
# Encode for HTML5 mp4
-acodec aac -strict experimental -ac 2 -vcodec libx264 -f mp4
-codec:v libx264 -b:v 500k -maxrate 500k -bufsize 1000k
# -b:v - sets video bitrate in bits/s
# -maxrate and -bufsize - forces libx264 to build video in a way, that it could be streamed over 500kbit/s line considering device buffer of 1000kbits. Very useful for web - setting this to bitrate and 2x bitrate gives good results.
# Eureka's coding
-vcodec libx264 -b:v 555k
# A decent LQ coding
-vf 'scale=-2:min(480\,ih)' -c:a aac -strict experimental -b:a 96k -c:v libx264 -crf 22 -maxrate 600k -bufsize 1000k
# https://trac.ffmpeg.org/wiki/Encode/H.264
# CRF 22 and -maxrate/-bufsize for optimal targeting. -preset slower, for higher quality within even that. (smarter compression)
# Stream Mappings
-map 0:1 # Select source file 0, stream 1
-map 0:v:0 # Select source file 0, video stream 0
-map 0:a:1 # Select source file 0, audio stream 1
# Verbosity and console noise
-hide_banner
# -v log levels: http://superuser.com/questions/326629/how-can-i-make-ffmpeg-be-quieter-less-verbose/438280#438280
# Use silent audio
ffmpeg -f lavfi -i aevalsrc=0 -i video.mp4 -shortest -c:v copy -c:a aac -strict experimental -map 0:a -map 1:v output.mp4
# Vertical video side-blur
# http://stackoverflow.com/a/30832903
-filter_complex '[0:v]scale=ih*16/9:-1,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[bg][0:v]overlay=(W-w)/2:(H-h)/2,crop=h=iw*9/16'
# GIF output (scaled)
ffmpeg -i D-fin.mp4 -vf palettegen D-fin.palette.png
ffmpeg -i D-fin.mp4 -i D-fin.palette.png -filter_complex '[0]scale=320:-1:flags=lanczos[x];[x][1]paletteuse' D-fin.gif
# Use blank-black video
# https://lists.ffmpeg.org/pipermail/ffmpeg-user/2011-October/002602.html
convert -size 320x240 xc:black black.png
ffmpeg -loop 1 -i black.png -i music.mp3 -vcodec libx264 -preset medium -tune stillimage -crf 24 -strict experimental -acodec aac -b:a 192 -shortest output.mp4
# Use blank-black video
# https://stackoverflow.com/a/11453109
ffmpeg -s 1920x1080 -f rawvideo -pix_fmt rgb24 -r 24 -i /dev/zero ...
# X11 Screen Capture
# ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -y output.mkv
ffmpeg -y -f x11grab -r 25 -s 1600x900 -i :0.0 -vcodec libx264 -crf 22 -threads 3 /tmp/screencast.mp4
# or
WINOFFSET="$(xwininfo | xargs echo | sed -re 's/.*Absolute upper-left X:\s+([0-9]+)\s.*Absolute upper-left Y:\s+([0-9]+)\s.*Width: ([0-9]+)\s.*Height: ([0-9]+)\s.*/-s \3x\4 -i '"$DISPLAY"'+\1,\2/')"
ffmpeg -y -f x11grab -r 25 $WINOFFSET -vcodec libx264 -crf 22 -threads 3 -y /tmp/screencast.mp4
# Stream webcam video to a remote ffmpeg target.
ffmpeg -f v4l2 -video_size 640x480 -pixel_format yuyv422 -i /dev/video0 -c:v libx264 -crf 28 -preset ultrafast -f flv tcp://0.0.0.0:4434/?listen
# Encode from DVD
# -analyzeduration/-probesize are related to seeking for subtitles since VOB has no index.
dvdbackup -pF
# Make sure that 5.1 sources are given enough bits per each of 6 streams, rather than 2. "-b:a 384k -b:a:1 128k"
# Alternately use -ac 2 (or -ac:0 2) to convert to standard stereo.
# -ifo_palette is an INPUT option, to provide the subtitle color palette that ffmpeg can't get from the VOBs themselves.
# -vf setsar=dar makes sure the display aspect ratio is the resultant video's output ratio. pixel stretching is weird
ffmpeg -fflags +genpts -analyzeduration 1000000k -probesize 1000000k -ifo_palette VIDEO_TS/*.IFO -i concat:"$(ls -1 VIDEO_TS/*.VOB | sort | xargs echo | sed 's/ /|/g')" -vf setsar=dar -c:v libx264 -crf 22 -strict experimental -c:a aac -b:a 384k -b:a:1 128k -map 0:v -map 0:a -map 0:s -c:s copy -y AllStreams.mkv
# Various Useful Documentation:
# https://github.com/MediaCrush/mediacrush.github.io/blob/master/_posts/2013-12-23-The-right-way-to-encode-HTML5-video.md
# Encode album art into a MP3
# from http://ffmpeg.org/ffmpeg-formats.html#mp3
ffmpeg -i input.mp3 -i cover.png -c copy -map 0 -map 1 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" out.mp3
# On encoding rates & crf values (see comments):
# http://goughlui.com/2016/08/27/video-compression-testing-x264-vs-x265-crf-in-handbrake-0-10-5/
# Retime a video stream that has the wrong concept of its own FPS.
# https://superuser.com/questions/320045/change-the-frame-rate-of-an-mp4-video-with-ffmpeg
mkvmerge --default-duration 0:12fps --fix-bitstream-timing-information 0 original-video.mp4 -o temp-video.mkv
ffmpeg -i temp-video.mkv -c:v copy slow-video.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment