Converting video files to gif with -filter_complex on ffmpeg

elberino

elber valim

Posted on May 3, 2021

Converting video files to gif with -filter_complex on ffmpeg

HELLO WORLD, this is my first post on dev.to, I use this website so long for read tech articles, but never feel comfortable to write some content to post here.

Like the title said this articles is about conversion files usinf ffmpeg

ffmpeg is an awesome library able to convert, transcode, trimm and mapping multimedia

First of all you need ffmpeg install here more information related with that.

The second thing you need it's a raw video to try the following command lines, in my case I will use an video input file of adventure time, s6e36.

Okay let's cut the chase, the basic of how ffmpeg work is this:

// e.g. documentation
ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

// e.g. my samples in this articles
ffmpeg -i 'input.mp4' -filter_complex"" output.gif
Enter fullscreen mode Exit fullscreen mode

Now to transcode your file into a nice gif it's a tricky with two steps.

For socialmedia purpose it's nice if my final file could be < 10mb, so because of that the parameter of frame rate and sizes are important to set in -filter_complex.

// converting 720p to 576p (scaled size reduction)

// step 1: generate palette
ffmpeg -y -ss 04:36 -to 04:38.0 -i adv-time-s6-e36.mp4 -filter_complex "fps=15,scale=-1:576,setsar=1,palettegen" palette.png

// step 2: using palette as input, for 576p
ffmpeg -y -ss 04:36.0 -to 04:38.0 -i adv-time-s6-e36.mp4 -i palette.png -filter_complex "[0]fps=15,scale=576:-1,setsar=1[x];[x][1:v]paletteuse" owl_coffee_576p.gif

Enter fullscreen mode Exit fullscreen mode

This palette it's really important because if we don't do this, the quality will significantly decrease of the ouput file.

How you can see, my file are with aspect ratio as 16:9 and had a burned watermark of the fansub, besides the watermark of cartoon network and my aim time of staring and ending don't suit at all.

It's a crap file but we can try again rewrite in this way. Because of the -y in the argument of ffmpeg every time the palette is overwrited and is much better generate again with new tempos.

// converting 720p to 432p (40% of size reduction and crop)

// step 1: generate palette, alter 1 of time
ffmpeg -y -ss 04:36.3 -to 04:38.3 -i adv-time-s6-e36.mp4 -filter_complex "fps=15,scale=432:-1,setsar=1,palettegen" palette.png

// step 2: using palette as input, alter 1 of time
ffmpeg -y -ss 04:36.3 -to 04:38.3 -i adv-time-s6-e36.mp4 -i palette.png -filter_complex "[0]fps=15,scale=432:-1,crop=ih:ih,setsar=1[x];[x][1:v]paletteuse" owl_coffee_432p-cropped.gif
Enter fullscreen mode Exit fullscreen mode

Oh my gosh, almost close, now we had and gif file with 1:1 aspect ratio, the television watermark gone but a little piece of the fansub watermark still and I get so close with the timming as well.

How yo ucan see I choose only overwrite the palette and just take other name for the output to compare the final files.

Let's rewrite again the ffmpeg command, i will still with 432p, and aspect of 1:1, but istead of set ih:ih i will set 385:385, this will crop the output inside of this 1:1 432p mapping process.

// converting 720p to 385p (reduction of 432p and crop an inside area)

// step 1: generate palette, alter 1 of time
ffmpeg -y -ss 04:36.5 -to 04:38.5 -i adv-time-s6-e36.mp4 -filter_complex "fps=15,scale=-1:432,crop=385:385,setsar=1,palettegen" palette.png

// step 2: using palette as input, alter 1 of time
ffmpeg -y -ss 04:36.5 -to 04:38.5 -i adv-time-s6-e36.mp4 -i palette.png -filter_complex "[0]fps=15,scale=-1:432,crop=385:385,setsar=1[x];[x][1:v]paletteuse" owl_coffee_385p-cropped.gif
Enter fullscreen mode Exit fullscreen mode

Yes, I got it!
The final file had less thean 1mb, the timming and the crop are also good.

My .gif with a small megabyte is perfect to share on socialmedia like twitter, reddit, thumblr and giphy.

That's it folk, thank you for reading, try use it for slice and croping raw videos, isn't complex deal with ffmpeg if you can handle a draft file to write the command arguments.

Bye.

"Um abraço do tamanho da internet"

💖 💪 🙅 🚩
elberino
elber valim

Posted on May 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related