Skip to content

Instantly share code, notes, and snippets.

@ucnv
Created September 12, 2010 09:37
Show Gist options
  • Save ucnv/575963 to your computer and use it in GitHub Desktop.
Save ucnv/575963 to your computer and use it in GitHub Desktop.
require 'aviglitch'
require 'fileutils'
base = "resource.avi"
insert = "text.avi"
audio = "bgm.mp3"
#--------------
FileUtils.rm_r 'tmp' if File.exist? 'tmp'
FileUtils.mkdir 'tmp'
cmd = "ffmpeg -i %s -s hd1080 -aspect 16:9 -an -sameq -r 30 %s"
system(cmd % [base, 'tmp/base.avi'])
system(cmd % [insert, 'tmp/insert.avi'])
#--------------
unit = 15 #3
step = 5 #1
a = AviGlitch.open 'tmp/base.avi'
a.glitch :keyframe do |x|
nil
end
q = nil
(a.frames.size / step).to_i.times do |i|
pos = i * step
f = a.frames[pos, unit]
if q.nil?
q = f
else
q.concat f
end
printf "\r>> %4d/%d", pos, a.frames.size
$stdout.flush
end
puts
AviGlitch.open(q).output('tmp/outx.avi')
m = AviGlitch.open q
#--------------
n = AviGlitch.open insert
key = nil
n.frames.each_with_index do |f, i|
if f.is_keyframe?
key = n.frames[i, 1]
break
end
end
delta = m.frames
step = [2,2,2,20]
o = nil
pos = 0
while 1 do
s = step[rand(step.size)]
f = key + delta[pos, s]
if o.nil?
o = f
else
o.concat f
end
break if pos > delta.size
pos += s
printf "\r>> %4d/%d", pos, delta.size
$stdout.flush
end
puts
AviGlitch.open(o).output('tmp/outv.avi')
#--------------
cmd = "ffmpeg -i %s -i %s -y -sameq %s" % ['tmp/outv.avi', audio, 'out.mp4']
system cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment