Skip to content

Instantly share code, notes, and snippets.

@fo0nikens
Forked from revolunet/ffmpeg-tips.md
Created July 12, 2018 09:18
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 fo0nikens/aa600e41fd9c0de61d5aac7b3ef5b6af to your computer and use it in GitHub Desktop.
Save fo0nikens/aa600e41fd9c0de61d5aac7b3ef5b6af to your computer and use it in GitHub Desktop.
ffmpeg tips

convert to another format, resize withouth quality loss

ffmpeg -i vitrine.mp4 -vf scale=1024:-1 -q:vscale 0 vitrine.avi

concat video files

ffmpeg -f concat -i repeat.txt -c copy vitrine2.mp4

in repeat.txt:

file 'file1.avi'
file 'file2.avi'
file 'file3.avi'
file 'file4.avi'
...

Create high-quality animated gifs

-> http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html

ffmpeg -i in.mov -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif

Extract thumbnails

extract image every 5sec and resize to 90px width

ffmpeg -i input.mp4 -f image2 -vf "fps=fps=1/5, scale=90:-1" thumb-%04d.jpg

Join thumbs

join images to a single horizontal image

convert *.jpg +append out.jpg

Split image horizontally

create 11 equal-width thumbs from image

convert input.jpg -crop 11x1@ +repage +adjoin output-%04d.jpg

Create video sequence from thumbnails

create a 30fps mp4 with 3 frames per image (30/10). (vf fix image scale if incorrect)

ffmpeg -framerate 10 -i output-%04d.jpg -c:v libx264 -r 30 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p output.mp4

Produce m3u8 + ts files for HLS streaming (OSX)

in you get Unknown encoder 'libfdk_aac' on the mac, do this:

sudo brew unlink ffmpeg ; sudo brew reinstall ffmpeg --with-fdk-aac

or compile ffmpeg

Download the apple HLS tools from https://developer.apple.com/downloads/index.action?=http%20live%20streaming%20tools

this generates m3u8 + ts fragments for a given bitrate

ffmpeg -i input.mp4 -hls_time 30 -s 1024x768 -c:v libx264 -b:v 528k -b:a 128k -ar 44100 -ac 2 -c:a libfaac out/768.m3u8

Generate Thumbs with scene detection

ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.8)" -frames:v 100 -vsync vfr ./out/out%02d.jpg

add text on existing video

Ex: Add frame number in the center

ffmpeg -i input.mp4 -vf "drawtext=fontfile=/Library/Fonts/Arial.ttf:fontsize=30: fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text=%{expr\\\\\:n+1}" output.mp4

create a video with text from scratch

Add time + frame number

ffmpeg -f lavfi -i color=c=black:s=320x240:d=600 -vf "drawtext='fontfile=/Library/Fonts/Arial.ttf:fontsize=15:fontcolor=white:x=10:y=(h-text_h*4):text=time %{pts}', drawtext='fontfile=/Library/Fonts/Arial.ttf:fontsize=15:fontcolor=white:x=10:y=(h-text_h*2):text=frame %{expr\:n+1}'" blank-10min.mp4

Extract sound

ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 sound.mp3

Slow down video and sound

ffmpeg -i cadran-contact.mp4 -filter_complex "[0:v]setpts=2*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" output.mp4

Extract sequence

ffmpeg -i output.mp4 -ss 00:00:04 -to 00:00:11 output2.mp4

Crop

ffmpeg -i output5.mp4 -filter:v "crop=width:height:start_x:start_y" output6.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment