X Windowを動画にするスクリプトを書いた。
#!/usr/bin/ruby
require 'digest/md5'
require 'fileutils'
fps = 4
skip = 2
bitrate = '800k'
xwininfo = `xwininfo`
id = xwininfo.match(/Window id: ([^ ]+)/)[1]
lastmd5 = nil
loop {
break unless system("xwd -id #{id} >a")
md5 = Digest::MD5.file('a')
next if lastmd5 == md5
lastmd5 = md5
now = Time.now
system("convert a t%08x.%05x.png" % [ now.to_i, now.usec ])
}
count = 0
start = nil
Dir.glob("t*.png").sort.each {|f|
sec = f[1, 8].to_i(16)
usec = f[10, 5].to_i(16)
start = sec - skip unless start
sec -= start
tick = ((usec / 1000000.0) * fps).to_i
pos = sec * fps + tick
while count < pos
FileUtils.ln_sf(f, "s%04d.png" % (count + 1))
count += 1
end
}
system("ffmpeg -y -r #{fps} -i s%04d.png -r #{fps} -b #{bitrate} out.flv")