<-- home

Making gifsets for tumblr (or library instruction?)

I just wanted to post this here! I did a ruby script for making gifsets (for tumblr) but it can be for other things as well.

I especially have this Leadpipe article about using animated gifs for library instruction in mind. Since this script could be run for from an instructional video to create a series of gifs with very little effort.

”` ruby Making Gifsets

!$path-to-ruby/bin/ruby

This is a ruby script I made with the intention of creating gifsets for tumblr.

This is intended to be run from the *nix commandline and requires both ffmpeg and imagemagick to be already installed on your machine.

require ‘csv’

Having an ‘info.csv’ isn’t strictly necessary, but I found that it helped a great deal if you’re planning on making more than one gif from a video. The csv should have four columns with (timestamp,duration,frames,width) as the columns)

file = “info.csv”

Gets user input for the video’s filenname (inclusive of the extension)

puts “video name”

fname = gets.chomp

Gets input for the resulting gifname. No extension necessary.

puts “gif filename”

gname = gets.chomp

timestamps, in the csv, should be of the form HH:MM:SS

timestamp = []

the ‘duration’ is the number of seconds after the timestamp. My experimenting shows that durations longer than 4 seconds produce gifs over 1mb.

duration = []

‘frames’ determines the number of images ffmpeg extracts from the video. My usual method is the great number of seconds, the lower the frames. So for 2s duration, I usually put ‘15’. For 3s or 4s, I put ‘10’. You can play around to see how this goes.

frames = []

I’ll be honest, my intentions for this is really to make gifsets for tumblr. So full width is 520, half 268. This sets up your resize for those widths and automatically adjusts the height to retain the aspect ratio.

width = []

This gets each column of the csv and puts them into arrays.

list = CSV.foreach(file, :col_sep => “,”, :return_headers => false) do row

timestamp « row[0]

duration « row1

frames « row[2]

width « row[3]

end

Again, since this was done with tumblr in mind, you can only do a maximum of 10 images per photoset. Obviously, you can adjust this range as needed.

for i in 0..9

These make the necessary commands with the data from the csv arrays

ffmpeg = "ffmpeg -i #{fname} -r #{frames.fetch(i)} -vf scale=#{width.fetch(i)}:-1 -ss #{timestamp.fetch(i)} -t #{duration.fetch(i)} %03d.png"

convert = "convert -ordered-dither o8x8,16 -delay 10 *.png -coalesce -layers OptimizeTransparency +map #{gname}#{i}.gif"

Runs the commands in the shell.

`#{ffmpeg}`
`#{convert}`

cleans up the images

`rm *png`

end

”`