x1n13y84issmd42
x1n13y84issmd42

Reputation: 970

ffmpeg: save separate frames as still gifs

The question is simple: I don't want ffmpeg to create an animated GIF from given video stream, I want separate frames, each in GIF format. But when I set output file to something like frame%09d.gif ffmpeg tends to create an animation (and stores it exactly as frame%09d.gif). The same for -f gif option.

Of course, I could save PNGs and use ImageMagic's convert utility to transform them to GIFs, but I don't want any additional invocation overhead because I'm dealing with live streams and going to crunch large amounts of data.

Here is what I do, nothing special:

ffmpeg -i http://brightcove03-f.akamaihd.net/valgbodmandag1378107345_1_300k@80362 -f gif -y frame_%09d.gif

ffmpeg version N-54643-g15cee5e Copyright (c) 2000-2013 the FFmpeg developers
  built on Jul 11 2013 03:35:11 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnu
tls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --ena
ble-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger -
-enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-lib
vpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 39.100 / 52. 39.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.101 / 55. 12.101
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 80.100 /  3. 80.100
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
[flv @ 00000000002cb700] Stream discovered after head already parsed
Input #0, flv, from 'http://brightcove03-f.akamaihd.net/valgbodmandag1378107345_1_300k@80362':
  Metadata:
    encoder         : Lavf54.6.100
  Duration: 00:00:00.00, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Constrained Baseline), yuv420p, 480x270 [SAR 1:1 DAR 16:9], 25 tbr, 1k tbn, 50 tbc
    Stream #0:1: Audio: aac, 44100 Hz, stereo, fltp, 128 kb/s
    Stream #0:2: Data: none
[swscaler @ 0000000004d051e0] No accelerated colorspace conversion found from yuv420p to bgr8.
Output #0, gif, to 'frame_%09d.gif':
  Metadata:
    encoder         : Lavf55.12.101
    Stream #0:0: Video: gif, bgr8, 480x270 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 100 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> gif)
Press [q] to stop, [?] for help
frame=  141 fps=130 q=-1.0 Lsize=    4960kB time=00:00:05.68 bitrate=7153.1kbits/s
video:5100kB audio:0kB subtitle:0 global headers:0kB muxing overhead -2.743247%

After all I get a file named "frame_%03d.gif", but instead I want to have several files "frame_001.gif", "frame_002.gif", etc.

Any ideas? Thanks in advance.

Upvotes: 1

Views: 6816

Answers (1)

slhck
slhck

Reputation: 38740

When you use -f gif, you're forcing ffmpeg to use the GIF muxer, which writes the converted video stream into a single animated GIF file.

With recent ffmpeg:

Use this to force ffmpeg to mux every frame as a GIF to a separate file:

ffmpeg -i input -c:v gif -f image2 out%04d.gif

With ffmpeg 1.2.1 and lower:

When you leave out -f gif, ffmpeg correctly produces single images for each frame:

ffmpeg -i input output%04d.gif

If you're stuck on a version where you still have this bug (i.e. probably 2.0 and 2.1), ffmpeg doesn't follow the above syntax. Your only option here is to create PNG images instead:

ffmpeg -i input output%04d.png

And use ImageMagick to convert them:

for f in output*.png; do convert "$f" "${f%.png}.gif"; done

Upvotes: 6

Related Questions