Hacker News new | past | comments | ask | show | jobs | submit login
Stupid Programmer Tricks and Star Wars GIFs (rarlindseysmash.com)
311 points by chewxy on Oct 29, 2013 | hide | past | favorite | 45 comments



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..


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

Example gif:

http://imgur.com/RiqEHJJ


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!


Try this:

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


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


Something like this probably works:

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


Or even just

    ffmpeg -i myvideo.mkv myaudio.mp3


There's even this (awesome) guide on optimizing video to gif conversion with ffmpeg and ImageMagick: http://blog.room208.org/post/48793543478

The file size reduction was quite impressive for our use case (roughly 10x smaller.)

In the end we did decide that while putting 5 second video intros in emails is cool, a 200k image is still a bit much for mobile users.


> putting 5 second video intros in emails is cool

You must have a very different experience of email than I do. That sounds absolutely terrible to me. Am I missing some reason that this would be useful, or is this just a case of wildly diverging preferences?


I meant 'cool' more as in 'hack-value'.

Our product (Interactly) is virtual video interactions, so our main value prop is being able to see real people respond.

In that context we figured it would be interesting to see if we could include a 5 second GIF intro right into the notification email; mostly out of curiosity just to see if it would technically be possible.

I was surprised to find out it was mostly feasible, but you're right that it doesn't make sense beyond the fact that it would be a cool gimmick.


Oh. That's totally reasonable. Cool, thanks.


Indeed, I have to vouch for ffmpeg as a the best solution. When I built GifMachine[0], it's pretty much just a web interface and automation layer to ffmpeg/imagemagick. Imagemagic and ffmpeg I found to be easy to use, fast, and delightfully scriptable. Using them together also produces the prettiest results.

For example, here's a gif produced with just ffmpeg: http://i.imgur.com/DWc4OdD.gif

And here is the version produced by ImageMagick: http://i.imgur.com/OdojPSo.gif

While the one made using ImageMagick looks better, it's also less compressed and takes up more space. So it's a tradeoff between the two.

[0] http://gifmachine.xwl.me/


I have to ask. What the heck is that gif from?


It's rather late to be posting this, but the gif is from this music video:

Duke Dumont - Need U @ 1:42

http://www.youtube.com/watch?feature=player_detailpage&v=FnJ...


gstreamer too can be used. I prefer gstreamer as it really does embody the unix philosophy (in its own weird way though)


I'm unfamiliar with gstreamer. Can you elaborate on this weird way?


The arguments to the gstreamer binary are encoders,decoders,filters and i/o piped together like commands in a shell (with the ! operator). For example, this is a shell command to show a h264 network stream on a raspberry pi:

    gst-launch-1.0 rtspsrc location=rtsp://192.168.2.112:8080/stream.sdp ! rtph264depay ! h264parse ! omxh264dec ! autovideosink
Gstreamer is more than this, but it'is a neat example.


avconv seems to be more up-to-date than ffmpeg.


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.)


> 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)


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!")


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.


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.


This is what I call a great hack.


Seconded!


yep. Brilliant hack.


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?


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


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


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

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


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?


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.


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.


I did something similar (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.


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


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.


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!


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.


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.


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.


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


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...

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


TAAS - Tumblr as a Service.


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


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: