Skip to content

Instantly share code, notes, and snippets.

@ucnv
Created August 7, 2010 06:42
Show Gist options
  • Save ucnv/512523 to your computer and use it in GitHub Desktop.
Save ucnv/512523 to your computer and use it in GitHub Desktop.
require 'echonest'
require 'aviglitch'
audio, *videos = ARGV
apikey = "YOUR-ECHONEST-API-KEY"
beat_threshold = 0.15
fps = 30
tmp = 'tmp.avi'
outfile = "out.mp4"
avis = videos.map do |v|
cmd = "ffmpeg -i %s -sameq -y -an -r %d %s" % [v, fps, tmp]
system cmd
a = AviGlitch.open tmp
a.glitch(:keyframe) do |f|
nil
end
a
end
echonest = Echonest(apikey)
duration = echonest.get_duration(audio)
beats = echonest.get_beats(audio)
beats.push(Beat.new(duration, 1.0))
result = nil
pos = 0
beats.each_with_index do |b, i|
next if b.confidence < beat_threshold
t = avis[rand(avis.size)]
d = b.start - pos
pos = b.start
f = (fps * d).round
s = rand(t.frames.size - f)
x = t.frames[s, f]
if result.nil?
result = x.to_avi
else
result.frames.concat x
end
printf "\rAviGlitch processing..: %4d/%d", i, beats.size
$stdout.flush
end
puts "\rAviGlitch processing..: Done. "
result.output tmp
cmd = "ffmpeg -i %s -i %s -sameq -ab 128k -y %s" % [tmp, audio, outfile]
system cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment