Skip to content

Instantly share code, notes, and snippets.

@robertdean
Last active November 14, 2024 18:46
Show Gist options
  • Save robertdean/df8a487a605b1df3c65b5d767be60fea to your computer and use it in GitHub Desktop.
Save robertdean/df8a487a605b1df3c65b5d767be60fea to your computer and use it in GitHub Desktop.
https://github.com/tldr-pages/tldr/blob/master/pages/common/ffmpeg.md
Video to GIF
SET filters="fps=%4,scale=%3:-1:flags=lanczos"
ffmpeg -v warning -i %1 -vf "%filters%,palettegen" -y palette.png
ffmpeg -v warning -i %1 -i palette.png -lavfi "%filters% \[x\]; \[x\]\[1:v\] paletteuse" -y %2
DEL palette.png
togif.bat <input.mp4> <output.gif> <width> <fps>
Extract audio from a video
ffmpeg -i "path\to\my_input_video_file.mp4" "path\to\my_output_audio_only.wav"
Extract specific video and audio stream
ffmpeg -i "path\to\my_input_video_file.mp4" -map 0:0 -c copy video.mp4 -map 0:1 -c copy audio0.m4a -map 0:2 -c copy audio1.m4a
Concatenate two or more video clips
(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4
Convert 10-bit H.265 to 10-bit H.264
ffmpeg -i input -c:v libx264 -crf 18 -c:a copy output.mkv
Convert 10-bit H.265 to 8-bit H.265
ffmpeg -i input -c:v libx265 -vf format=yuv420p -c:a copy output.mkv
Convert 10-bit H.265 to 8-bit H.264 ffmpeg -i input -c:v libx264 -crf 18 -vf format=yuv420p -c:a copy output.mkv
Print subtitles (useful for old TVs that can't select a subtitle from streams or files; I use this to get my kids to watch movies in English):
-vf "ass=subtitle.ass"
or with .srt and in a huge yellow font
-vf "subtitles=subtitles.srt:force_style='Fontsize=36,PrimaryColour=&H0000FFFF'"
Extract 1 second of video every 90 seconds (if you have very long footage of a trip from a dashcam and you don't know what to do with it, that makes for a much shorter "souvenir"):
-vf "select='lt(mod(t,90),1)',setpts=N/FRAME_RATE/TB" -af "aselect='lt(mod(t,90),1)',asetpts=N/SR/TB"
Convert all FLAC files in the directory to MP3s, in parallel using fd:
fd -t f -e flac . "$@" -x ffmpeg -loglevel 16 -i "{}" -qscale:a 0 "{.}.mp3"
fixing incorrect or missing h264/h265 bitstream metadata without re-encoding, for example:
ffmpeg -i source.avc -c copy -bsf:v h264_metadata=colour_primaries=1:matrix_coefficients=1 output.h264
https://ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fme... https://www.itu.int/rec/T-REC-H.264-201906-I/en
# 1. Record your device using QuickTime
# (File->New Movie Recording->Select your phone)
# 2. Run `$ app-preview your-recording.mov`
function app-preview() {
echo "name $1"
ffmpeg -i $1 -vf scale=1080:1920,setsar=1 -c:a copy "out_$1"
}
Convert AVI to MP4
----------------------------
ffmpeg -i input.avi -c:v libx264 -crf 19 -preset slow -c:a libvo_aacenc -b:a 192k -ac 2 out.mp4
Convert MP4 to GIF
---------------------------
mkdir frames
ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.jpg'
cd frames
convert -delay 20 -loop 0 -layers Optimize *.jpg myimage.gif
# Save a RTSP stream to file
ffmpeg -i rtps://someserver/somevideo -c copy out.mp4
# encoding quality settings examples
# H264
ffmpeg -i input.mp4 -a:c copy -c:v libx264 -crf 22 -preset slower -tune film -profile:v high -level 4.1 output.mp4
# H264 10bit (must switch to a different libx264 version)
LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/x264-10bit ffmpeg -i input.mp4 -a:c copy -c:v libx264 -crf 22 -preset slower -tune film -pix_fmt yuv420p10le -profile:v high10 -level 5.0 output.mp4
# resize
ffmpeg -i in.mp4 -s 480x270 -c:v libx264 out.mp4
# change source fps
ffmpeg -r 10 -i in.mp4 out.mp4
# resample fps
ffmpeg -i in.mp4 -r 10 out.mp4
# extract the audio from a video
ffmpeg -i inputfile.mp4 -vn -codec:a copy outputfile.m4a
# merge audio and video files
ffmpeg -i inputfile.mp4 -i inputfile.m4a -codec copy outputfile.mp4
# cut (reencoding)
# set the start time and duration as HH:MM:SS
ffmpeg -ss 00:00:40.000 -i input.mp4 -t 00:00:10 -c:v libx264 output.mp4
# set the start time and duration as seconds
ffmpeg -ss 40.0 -i input.mp4 -t 10.0 -c:v libx264 output.mp4
# skip an exact number of frames at the start (100)
ffmpeg -i input.mp4 -vf 'select=gte(n\,100)' -c:v libx264 output.mp4
# cut (w/o reencoding - cut times will be approximate)
ffmpeg -ss 40.0 -i input.mp4 -t 10.0 -c copy output.mp4
# save all keyframes to images
ffmpeg -i video.mp4 -vf "select=eq(pict_type\,I)" -vsync vfr video-%03d.png
# encode video from images (image numbering must be sequential)
ffmpeg -r 25 -i image_%04d.jpg -vcodec libx264 timelapse.mp4
# flip image horizontally
ffmpeg -i input.mp4 -vf hflip -c:v libx264 output.mp4
# crop and concatenate 3 videos vertically
ffmpeg -i cam_4.mp4 -i cam_5.mp4 -i cam_6.mp4 -filter_complex "[0:v]crop=1296:432:0:200[c0];[1:v]crop=1296:432:0:200[c1];[2:v]crop=1296:432:0:230[c2];[c0][c1][c2]vstack=inputs=3[out]" -map "[out]" out.mp4
# 2x2 mosaic (all inputs must be same size)
ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -i video4.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[row1];[2:v][3:v]hstack=inputs=2[row2];[row1][row2]vstack=inputs=2[out]" -map "[out]" out.mp4
# picture-in-picture
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[1:v]scale=iw/3:ih/3[pip];[0:v][pip]overlay=main_w-overlay_w-20:main_h-overlay_h-20[out]" -map "[out]" out.mp4
# print framerate of every file in dir
for f in *.mp4; do echo $f; mediainfo $f|grep "Frame rate"; done
# print selected info of every file in dir in CSV format
for f in *.mp4; do echo -n $f,; mediainfo --Inform="Video;%Duration%,%FrameCount%,%FrameRate%" $f; done
avi2gif:
ffmpeg -i $1 -vf scale=320:-1:flags=lanczos,fps=10 /tmp/frames/ffout%03d.png
convert -loop 0 /tmp/frames/ffout*.png $1.gif
flac2mp3
parallel-moreutils -i -j$(nproc) ffmpeg -i {} -qscale:a 0 {}.mp3 -- ./*.flac
rename .flac.mp3 .mp3 ./*.mp3
screencast
TIMESTAMP=`date +"%Y-%m-%d_%H"`
TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi)
ffcast -s % ffmpeg -y -f x11grab -show_region 1 -framerate 15 \
-video_size %s -i %D+%c -codec:v huffyuv \
-vf crop="iw-mod(iw\\,2):ih-mod(ih\\,2)" $TMP_AVI \
&& convert -set delay 10 -layers Optimize $TMP_AVI /home/nemo/Desktop/GIFs/$TIMESTAMP.gif
split-audio-by-chapters[0] to split a audiobook from Audible to multiple files by chapters.
split-by-audible[1] to split a audiobook from Audible to multiple files by chapters using the copied timestamps from the Audible Web Player.
split-by-silence[2] to split audiobooks by Silence instead
audiobook2video[3] to generate a video file from audiobooks. Puts up a nice cover.
Cut ffmpeg video without losing quality by keeping the same codecs, very fast:
Example: Start at 52 seconds, take 10 minutes after that:
ffmpeg -ss 52 -i input.mp4 -c copy -t 00:10:00.0 output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment