
Stupid Programmer Tricks and Star Wars GIFs - chewxy
http://rarlindseysmash.com/posts/stupid-programmer-tricks-and-star-wars-gifs
======
btbuildem
I would suggest ffmpeg as an alternative -- it peels, it slices and it dices.
It will definitely convert a range of a video file to gif while adding
subtitles, and you won't have to frankenstein things together..

~~~
sigil
Totally agree, although the author's use of vlc definitely qualifies as a
clever hack.

I want more hackers to realize how insanely easy it is to programmatically
generate video with ffmpeg. Just write a program that dumps raw RGB frames to
stdout! You can turn that into any video format conceivable, including gif.

Here's a little demo I wrote. Generates a video of the Mandelbrot set coming
into focus:

[https://github.com/acg/generating-video-
demo](https://github.com/acg/generating-video-demo)

Example gif:

[http://imgur.com/RiqEHJJ](http://imgur.com/RiqEHJJ)

~~~
goostavos
Interesting! At work I hacked together a video writer that compiles still
frames to a movie using OpenCV. But it is _slow_ \--ungodly slow. I'll have to
check out ffmpeg!

~~~
sigil
Try this:

    
    
        ffmpeg -i frame%04d.png -vcodec mjpeg -sameq test.avi

~~~
unlikelymordant
If I wanted to strip the audio into a big wav or mp3 file, how would that be
done?

~~~
djeikyb
Something like this probably works:

    
    
        ffmpeg -i myvideo.mkv -vn -acodec ogg myvid_sans_video.ogg

~~~
mtrimpe
Or even just

    
    
        ffmpeg -i myvideo.mkv myaudio.mp3

------
ryanthejuggler
THIS.

Remember when you first discovered programming? When you did all those little
projects that were, honestly, fairly useless, but brought you the spine-
tingling sensation of having power over your domain?

Remember how when you started programming professionally, and you quit those
projects? Why did that need to happen?

Bravo Lindsey, never stop making cool things.

(YMMV. I'm assuming of course that you learned programming as a hobby--if you
learned by taking a college course then _gasp_ you might have skipped the
pointless-project phase __entirely __. I suggest you get on top of that.)

~~~
Cthulhu_
> Remember how when you started programming professionally, and you quit those
> projects? Why did that need to happen?

It didn't, I now get to spend 8+ hours a day working on things that matter,
things that get me paid, thinks that make my customers (and their customers)
happy, things that I see people use daily on their phone on my way to my next
assignment.

Just because I work for a boss - and no, not some cool startup in the Valley -
doesn't mean I don't do cool projects anymore.

(My pointless project phase was amongst others a scraper for InvisionFree
forums, they would charge hundreds of dollars to get a copy of the database of
a <50.000 post forum - and ours was over half a million posts big)

------
ZoFreX
Very, very cool. I love the approach, especially the interface that ties it
all together! Nice trick to use subtitle files to grab the right bit of video
automatically, too.

If anyone is thinking of doing something similar, or just wants to script some
video in some way, I can _highly_ recommend AviSynth. It fits into the
extremely flexible DirectShow pipeline and has earned a permanent place in my
video editing toolchain (I use it as the frameserver for encoding DVDs).

Here's an example script (if you have AviSynth installed and create a file
with these contents named hello.avs, you can open it in any media player you
choose to see the results):

    
    
        BlankClip()
        Subtitle("Hello, world!")

------
noelwelsh
Take this approach and apply it with CSS, a web framework, and a DB and you
can go quite far in the .com game. You'll run into problems if you get
popular, but that's a good problem to have.

~~~
Cthulhu_
And yet, you won't - it mostly depends, I think, where your video comes from.
gifsoup (iirc) will scrape a youtube video and turn it into a .gif. There's
probably quite a few youtube-to-gif services out there, come to think of it.

It'll be a problem if you use the original star wars video in one way or
another though. I think. Depends on whether it can be considered fair use or
not.

------
chewxy
This is what I call a great hack.

~~~
filearts
Seconded!

~~~
skimmas
yep. Brilliant hack.

------
jlgreco
Assuming a random distribution of quotes from the twitter stream, and assuming
there isn't any control on repeats, how long can we expect to be able to
recreate the movies (well, the portions with dialog) in gif form?

~~~
psuter
That would be the Coupon collector's problem [1]. Roughly, you should expect
to go through O(n log n) quotes before you have seen them all.

[1]
[http://en.wikipedia.org/wiki/Coupon_collector's_problem](http://en.wikipedia.org/wiki/Coupon_collector's_problem)

~~~
jlgreco
Awesome link, thanks! I love learning when stuff like this has a name.

------
akx
Neat. For the hell of it, I wanted to see how the same could be done with
ffmpeg, so here's what I got in ~20 minutes:

[https://gist.github.com/akx/7217852](https://gist.github.com/akx/7217852)

(You could add a `-vf ass=my_subtitle_file.ass` to the ffmpeg command line
burn subtitles into the gif.)

~~~
KnightHawk3
Not being good at ffmpeg, if I were to run it on a file with subtitles in the
file (The most recent episode of an Anime for example), would it burn the
subtitles in?

~~~
akx
Yes, it would. -vf are video filters, ie. they affect the output video stream.

EDIT: Misread your question, but yeah, I think you can make -vf ass read the
ASS file from the (MKV, I assume?) file itself.

------
sonnyz
The subtitles files you can find online are formatted with timestamps to allow
the video player to sync the text with the video, so for this purpose it makes
perfect sense. Maybe set top boxes could make use of subtitles to allow users
to search for a certain point in a video.

------
mark_olson
I did something similar
([http://markolson.github.io/storyboard/](http://markolson.github.io/storyboard/))
earlier this year using ffmpeg and ImageMagick to generate either GIFs around
lines of dialog (like this project), or PDFs where each page is a frame of
text or a new scene. Optimizing GIFs is _by far_ the least enjoyable part.

~~~
hox
Damn you I was going to come post your storyboard. :(

------
_pmf_
The prevalence of the animated GIF is an eloquent statement about the current
video encoding mess. While the typical small video clip today, encoded in a
fancy new standard, is orders of magnitudes smaller that the JS+CSS+HTML site
containing it, we use GIFs because nobody has any idea who is able to see the
content is using a modern endoder.

------
sdoering
Was really informative to read. I am a novice in every programming aspect, as
I am formally a product (or content) manager.

But using python as my tool for answering my bosses questions with data, I
really enjoy reading posts like these, giving me ideas to learn and try new
things.

Thanks a lot for that!

------
gutsy
This is awesome! I had no idea that you could programmatically do this (I
still consider myself a novice even though I've been professionally coding for
two years). Very, very cool!

Now I kinda want to try this.

------
rflrob
I wonder how hard it would be to introduce a cut detector, so that the (to me)
annoying cinematic cuts that are sometimes at the beginning or end of a quote
can be trimmed off. I think a simple heuristic of something like "If the frame
cuts in the last half-second of the gif, trim off the extra frames" would work
well, although I don't know how well a simple detector would work, nor how
many quotes have a reaction shot that would get cut off unnecessarily.

------
moccajoghurt
I also enjoy gifs. I have been creating gifs with VirtualDub so far.

I usually prefer writing scripts to do stuff but VirtualDub is one of the few
tools I simply use because I enjoy the open source and hacker spirit behind
it.

An interesting plugin I miss for VD is an color reduction algorithm which
would help to create very small gifs.

On reddit and tumblr gifs are a growing trend and it might be worth to put
some effort into gif creation.

~~~
lmm
If you're going to do serious video editing, it's well worth learning
avisynth.

------
bazzargh
Wonder if this could be done for youtube, since it provides its subtitles via
an api?
[https://developers.google.com/youtube/2.0/developers_guide_p...](https://developers.google.com/youtube/2.0/developers_guide_protocol_captions#Retrieve_Caption_Track)

(and there are a plethora of yt-to-gif sites, so clearly frame capture works)

------
HarrietJones
TAAS - Tumblr as a Service.

------
ge0rg
This is brilliant. Now, instead of using random quotes, please take the IMDB
quotes and make anigifs of them :)

------
parennoob
Am the only one who puts animated GIFs in the same category as the blink and
marquee tags? Absolutely hate when there is more than one of these attention-
seeking horrors on the page.

I've had to stop reading github's blog since they started putting thousands of
these abhorrences there and started making it looks like a Buzzfeed page.

