<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: elber valim</title>
    <description>The latest articles on DEV Community by elber valim (@elberino).</description>
    <link>https://dev.to/elberino</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F379359%2Ff9e0fa4c-6183-4295-9075-d80928539c7d.jpg</url>
      <title>DEV Community: elber valim</title>
      <link>https://dev.to/elberino</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elberino"/>
    <language>en</language>
    <item>
      <title>Converting video files to gif with -filter_complex on ffmpeg</title>
      <dc:creator>elber valim</dc:creator>
      <pubDate>Mon, 03 May 2021 18:54:07 +0000</pubDate>
      <link>https://dev.to/elberino/converting-video-files-to-gif-with-filtercomplex-on-ffmpeg-387n</link>
      <guid>https://dev.to/elberino/converting-video-files-to-gif-with-filtercomplex-on-ffmpeg-387n</guid>
      <description>&lt;p&gt;&lt;strong&gt;HELLO WORLD,&lt;/strong&gt; 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.   &lt;/p&gt;

&lt;p&gt;Like the title said this articles is about conversion files usinf ffmpeg&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ffmpeg is an awesome library able to convert, transcode, trimm and mapping multimedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First of all you need ffmpeg install &lt;a href="https://ffmpeg.org/download.html"&gt;here more information related with that&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Okay let's cut the chase, the basic of how ffmpeg work is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to transcode your file into a nice gif it's a tricky with two steps.&lt;/p&gt;

&lt;p&gt;For socialmedia purpose it's nice if my final file could be &amp;lt; 10mb, so because of that the parameter of frame rate and sizes are important to set in -filter_complex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/opmykUgiNm2ba9Vbz0/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/opmykUgiNm2ba9Vbz0/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/kZ9OGICTcsOk67UBHX/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kZ9OGICTcsOk67UBHX/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;How yo ucan see I choose only overwrite the palette and just take other name for the output to compare the final files.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/B25UaNGPj3h5XWJelb/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/B25UaNGPj3h5XWJelb/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, I got it!&lt;br&gt;
The final file had less thean 1mb, the timming and the crop are also good. &lt;/p&gt;

&lt;p&gt;My .gif with a small megabyte is perfect to share on socialmedia like twitter, reddit, thumblr and giphy.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Bye.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Um abraço do tamanho da internet"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>ffmpeg</category>
      <category>multimedia</category>
      <category>linux</category>
      <category>transcoding</category>
    </item>
  </channel>
</rss>
