How to do I convert an webm (video) to a (animated) gif on the command line?

33,323

Solution 1

From here:

ffmpeg -i input.webm -pix_fmt rgb24 output.gif

Solution 2

Barafu's answer is alright. But, the resulting gif may have color conversion issue as ffmpeg complains on Incompatible pixel format 'rgb24' for codec 'gif'. Here is what I find works:

First, create PNG Palette:

ffmpeg -y -i input.webm -vf palettegen palette.png

Then, use the palette to produce gif:

ffmpeg -y -i input.webm -i palette.png -filter_complex paletteuse -r 10 output.gif

Source:

Covert MP4/Webm - ubuntubuzz.com

Solution 3

Extending Raynal's answer, here's a script one can add to .bashrc to do the conversion:

function webm2gif() {
    ffmpeg -y -i "$1" -vf palettegen _tmp_palette.png
    ffmpeg -y -i "$1" -i _tmp_palette.png -filter_complex paletteuse -r 10  "${1%.webm}.gif"
    rm _tmp_palette.png
}

e.g.

webm2gif recording.webm

will create recording.gif.

Share:
33,323

Related videos on Youtube

brubaker
Author by

brubaker

Updated on September 18, 2022

Comments

  • brubaker
    brubaker almost 2 years

    I suppose ffmpeg is the weapon of choice but I didn't find out how to reach my goal.

  • brubaker
    brubaker almost 10 years
    I am already testing your solution. Just a moment.
  • brubaker
    brubaker almost 10 years
    Wow! It works! AND.. 2.6 MB webm -> 48 MB gif ^^ -- any thought to reduce this?
  • brubaker
    brubaker almost 10 years
    I suggest to change your command in your answer to: "ffmpeg -i input.webm -pix_fmt rgb24 output.gif".
  • kenn
    kenn almost 10 years
    gifsicle is a fantastic tool to reduce gif size gifsicle -O2 input.gif -o output.gif
  • Barafu Albino
    Barafu Albino almost 10 years
    Try -pix_fmt rgb16
  • brubaker
    brubaker almost 10 years
    Thanks @kenn but it only reduces the file size in this particular example down to 46 MB. I think it is maybe better trying to reduce ffmpeg output in the first place.
  • brubaker
    brubaker almost 10 years
    @BarafuAlbino Thanks buddy, but I got an error: "Unknown pixel format requested: rgb16."
  • wchargin
    wchargin over 8 years
    @brubaker I think I got you beat: 120K .webm → 2.7G .gif. Yes, that's with a G.
  • Kane Blueriver
    Kane Blueriver over 7 years
    rgb24 is not supported for gif, ffmpeg would use rgb8 instead automatically.
  • Sudhir Singh Khanger
    Sudhir Singh Khanger over 6 years
    @BarafuAlbino will this only work for webm or will it also work for other containers like mp4, mkv, ogg, etc.
  • Barafu Albino
    Barafu Albino over 6 years
    Should work for anything ffmpeg can understand. And you should omit -pix_fmt these days.
  • rodrigob
    rodrigob over 6 years
    works with avconv too (same command line options). gifsicle is awesome indeed !
  • Dan Dascalescu
    Dan Dascalescu over 5 years
    To crop the video, pass the starting second via -ss and the length via the -t option: ffmpeg input.web -ss 1 -t 10 output.gif.
  • Dan Dascalescu
    Dan Dascalescu over 5 years
    Passing -pix_fmt rgb16 produced "Unknown pixel format requested", and passing rgb24 warned "Incompatible pixel format 'rgb24' for codec 'gif', auto-selecting format 'rgb8'". If you don't mind, I'll edit your answer to remove the -pix_fmt option.
  • Eugene
    Eugene almost 5 years
    produced a much better result in my case than the accepted answer
  • Lok
    Lok about 3 years
    Confirmed that this also works on Windows 10😂