Skip to content

Instantly share code, notes, and snippets.

@timwaters
Created May 21, 2014 22:25
Show Gist options
  • Save timwaters/d47545b2fa4d99711058 to your computer and use it in GitHub Desktop.
Save timwaters/d47545b2fa4d99711058 to your computer and use it in GitHub Desktop.
Ubuweb video mashup mix generation
#ubu web
#read sqlite
#get X random moves
#randomly clip them
#make new movie
require 'sqlite3'
require 'net/http'
db = SQLite3::Database.new( "UbuWeb.db" )
filenames = []
db.execute( "select link from Films order by random() limit 10" ) do |row|
url = URI.parse(row[0])
filename = File.basename(url.path)
filenames << filename
puts "downloading #{row[0]}"
file_path = File.join("videos", filename)
unless File.exists?(file_path)
File.write(file_path, Net::HTTP.get(url))
end
sleep 10
end
puts "creating..."
filenames.each_with_index do | filename, index |
file_path = File.join("videos", filename)
tmp_path = File.join("tmp", index.to_s)
duration_command = "ffprobe -show_format #{file_path} 2>&1 | grep 'duration=' | sed s/duration=// "
duration = `#{duration_command}`
puts duration
max_length = duration.to_i < 8 ? duration.to_i : 8
clip_length = rand(1..max_length)
first_cut = rand(1..(duration.to_i - clip_length))
puts "length = #{clip_length}"
puts "first cut = #{first_cut}"
cut_command = "avconv -y -i #{file_path} -ss #{first_cut} -t #{clip_length} -c:v mpeg2video -c:a copy #{tmp_path}.mpg"
puts cut_command
`#{cut_command}`
end
shuffs = (0..filenames.length-1).to_a.shuffle.map{|m| m = "tmp\/"+m.to_s+".mpg"}
cat_command = "cat #{shuffs.join(' ')} > tmp\/out.mpg "
puts cat_command
`#{cat_command}`
final_convert = "avconv -y -i #{File.join('tmp', 'out.mpg')} #{File.join('tmp', 'out.flv')}"
puts final_convert
`#{final_convert}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment