Best way to export a gif from oF

Hello,

I’m wondering what is the best way to export/save a gif file from oF. I’m working on a series of animated visuals and would like to be able to export small gifs (< 6sec) as documentation.

At the moment, to do this, I capture part of the screen as the animation is running and then convert the resulting .mov file into .gif online. It’s super tedious and the quality of the visual/animation drops significantly, as you can see in the uploads.

I’ve looked into ofxGifEncoder but for some reason my code won’t find the main .h file.

Looking forward to hearing your advice ! Much love to the community.


Hi, I don’t know what’s “the best way”, but what I do is saving the frames to a folder, and then calling command line programs to produce a good quality gif: http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html

The best command line program to compress the gif and reduce the color count is http://www.lcdf.org/gifsicle/

So my approach is:

  1. Save frames
  2. Call ffmpeg to calculate the ideal palette
  3. Call ffmpeg to produce an animated gif
  4. Call gifsicle to optimize the file size

Another one-step approach is to use this library https://github.com/ginsweater/gif-h but it produces non-optimized gifs and it’s not very fast if I remember right.

I forked ofxGifEncoder and fixed it so it works on my OF (compiled from Git, 0.9.8).

Hi @hamoid !

Thanks so much for your replies / fork.

I downloaded ofxGifEncoder and tried it but I am running into an issue. In this simple test animation, I am trying to get the code to export a gif of 4 seconds when key ‘g’ is pressed.

I get a error ‘Expected expression’ on line :

gifEncoder.addFrame(foo.getPixels(), frameW, frameH);

That line doesn’t make sense to me.

My code in the ofApp.cpp

void ofApp::setup(){

frameW  = ofGetWidth();
frameH  = ofGetHeight();

gifEncoder.setup(frameW, frameH, 4.0, 256);
gifEncoder.addFrame(foo.getPixels(), frameW, frameH);
}

//--------------------------------------------------------------
void ofApp::draw(){

ofColor bg_color=ofColor::fromHex(0x303030);
ofColor color1=ofColor::fromHex(0xbaba8f);

ofBackground(bg_color);
ofTranslate(ofGetWidth()/2,ofGetHeight()/2);

int radius=100;

ofSetColor(color1);
ofDrawSphere(0, 0, ofGetMouseX()/2);
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

if (key == 'g') {

    gifEncoder.save("test_"+ofGetTimestampString()+".gif");
    cout << "saving gif" << endl;
}
}

Can you tell what I am missing ?

Hi,

What if you replace

gifEncoder.addFrame(foo.getPixels(), frameW, frameH);

by

gifEncoder.addFrame(foo.getPixels().getData(), frameW, frameH);

That’s what the included example uses.

Hey @hamoid , thanks for the reply.

Tried using gifEncoder.addFrame(foo.getPixels().getData(), frameW, frameH); like you suggested but it does not work. I get this error:

Use of undeclared identifier 'foo’

I don’t understand what is ‘foo’.

My ofApp.h file :

#pragma once
#include "ofMain.h"
#include "ofxGifEncoder.h"

class ofApp : public ofBaseApp{

public:
void setup();
void draw();

void keyPressed(int key);

int frameW, frameH;

ofxGifEncoder gifEncoder;
};

:confused:

“foo” is used in programming languages as “bla bla”, or “whatever”. It’s supposed to be replaced by something meaningful, and it’s not yet declared in your program, that’s why it fails.

I added another example to the repo: https://github.com/hamoid/ofxGifEncoder (example-draw). It produces a pulsing circle anim gif. Cheers :slight_smile: