How to lower file size of gifs from youtube videos?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
LedZeppelin
Posts: 10
Joined: 2013-07-22T14:08:05-07:00
Authentication code: 6789

How to lower file size of gifs from youtube videos?

Post by LedZeppelin »

I'm using a youtube downloader to download music videos in the MP4 container, encoded in H.264 with a bitrate of about .5Mbit/s

I'm currently exporting videos to frames using ffmpeg then converting those images to gifs using imagemagick

Code: Select all

ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.png

Code: Select all

convert -delay 10 -loop 0 frames/ffout*.png output.gif
Instead of using .pngs as the images, I also tried using .gifs

Code: Select all

ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.gif

Code: Select all

convert -delay 10 -loop 0 frames/ffout*.gif output.gif
I feel the file sizes are way too big for the quality. The above samples rendered a 2.8MB for the gif based of the png images and 1.6MB for the gif based off the gif images.

What option should I use to lower the file size? I tried setting

Code: Select all

-colors
to values that were powers of two. Are there any other options I can adjust to exchange quality for file size?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to lower file size of gifs from youtube videos?

Post by fmw42 »

Gifs are limited to -colors less than or equal to 256. You do not have to use powers of 2. see the sections on GIF at http://www.imagemagick.org/Usage/formats/#gif

You could also define a fixed color palette image and recolor (all) your image using that common palette. See http://www.imagemagick.org/Usage/quantize/#remap

You could convert to 8-bit palette png and use some other tools such as pngcrush to reduce the file size. See the sections on PNG at http://www.imagemagick.org/Usage/formats/#png_non-im

What is wrong with just using JPG and compressing as desired?
LedZeppelin
Posts: 10
Joined: 2013-07-22T14:08:05-07:00
Authentication code: 6789

Re: How to lower file size of gifs from youtube videos?

Post by LedZeppelin »

What is wrong with just using JPG and compressing as desired?
I tried the following (saving to jpg), but the results weren't satisfactory. The size was way high.

Code: Select all

$ ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.jpg
$ convert -delay 10 -loop 0 frames/ffout*.jpg output.gif
Do you know how I could achieve this compression you mentioned? Do I do it through ffmpeg when I take the snapshots, or is there an option I can set in image magick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to lower file size of gifs from youtube videos?

Post by fmw42 »

convert your images from ffmpeg to png. then convert the pngs to GIF using the -colors or -remap option if you want an animated gif.

ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.png
convert -delay 10 frames/ffout*.png -colors XX -loop 0 output.gif

or

ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.png
convert -delay 10 frames/ffout*.png -dither none -remap paletteimage -loop 0 output.gif
Last edited by fmw42 on 2013-07-22T16:25:18-07:00, edited 1 time in total.
LedZeppelin
Posts: 10
Joined: 2013-07-22T14:08:05-07:00
Authentication code: 6789

Re: How to lower file size of gifs from youtube videos?

Post by LedZeppelin »

Code: Select all

convert your images from ffmpeg to png. then convert the pngs to GIF using the -colors or -recolor option if you want an animated gif.
Sorry for my ignorance, but isn't this exactly what I did before?

Code: Select all

ffmpeg -i video.mp4 -r 6 -t 5 frames/ffout%03d.png

Code: Select all

convert -delay 10 -loop 0 -colors 256 frames/ffout*.png output.gif
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to lower file size of gifs from youtube videos?

Post by fmw42 »

yes, but I am not sure what values for -color you used or if you tried the -remap option with a common fixed palette.

I missed the fact that you were making an animated gif when I suggested using jpg. I was only focusing on the compression/quality vs file size issue. Sorry for my mistake.
LedZeppelin
Posts: 10
Joined: 2013-07-22T14:08:05-07:00
Authentication code: 6789

Re: How to lower file size of gifs from youtube videos?

Post by LedZeppelin »

I see. I tried 256, 128, 62, 32, 16 as arguments to -colors. I think I'll stick with my original approach and possible -remap to my own pallette later. Thanks for your help :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to lower file size of gifs from youtube videos?

Post by fmw42 »

LedZeppelin wrote:I see. I tried 256, 128, 62, 32, 16 as arguments to -colors. I think I'll stick with my original approach and possible -remap to my own pallette later. Thanks for your help :D
the number of colors is not limited to powers of two. any value between 1 and 256 is fine, though too low would be unacceptable results.
LedZeppelin
Posts: 10
Joined: 2013-07-22T14:08:05-07:00
Authentication code: 6789

Re: How to lower file size of gifs from youtube videos?

Post by LedZeppelin »

I understand it's not limited to powers of two as you mentioned previously.

Thanks for the follow up clarification.
Post Reply