Skip to content

Instantly share code, notes, and snippets.

@halida
Created March 6, 2015 06:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save halida/c7924e97cf50d61b3f8f to your computer and use it in GitHub Desktop.
Create GIF preview for Videos, and a summarized html page
def make_preview_html
preview_path = 'preview'
styles = """
body .blk {
margin: 0 10px 10px 0;
display: inline-block;
}
body .blk img {
width: 160px;
height: 120px;
}
table, th, td {
border: 1px solid black;
}
"""
header = """
<html>
<head> <style>#{styles}</style> </head>
<body>
<h1>Preview</h1>
<div>
"""
footer = """<div> </body> </html> """
out = [header]
out << "<table>"
# get all preview file
Dir.glob(File.join preview_path, '**/*.gif').map do |filename|
origin_filename = filename.match(/preview\/(.*).gif/)[1]
path = origin_filename.split('/')[0]
[path, filename, origin_filename]
end.group_by(&:first).each do |path, data|
out << "<tr> <td> #{path} </td> <td>"
data.each do |d|
path, filename, origin_filename = d
out << """
<div class='blk'>
<a href='#{origin_filename}' target='_blank'> <img src='#{filename}'> </a>
</div>
"""
end
out << "</td></tr>"
end
out << "/<table>"
out << footer
File.open('preview.html', 'w+') do |f|
f.write out.map(&:strip).join("\n\n")
end
end
def convert
preview_path = 'preview'
temp_path = '/tmp/processing'
# cleanup
`rm -rf #{temp_path}`
`mkdir -p #{temp_path}`
`rm -rf #{preview_path}`
`mkdir -p #{preview_path}`
(Dir.glob('*') - ['preview']).each do |dir|
# dir = (Dir.glob('*') - ['preview']).first
`mkdir -p #{File.join preview_path, dir}`
puts "list #{dir}: #{Dir.glob(File.join(dir, '*'))}"
Dir.glob(File.join(dir, '*')).each do |filename|
# filename = Dir.glob(File.join(dir, '*')).first
puts "converting: #{filename}"
# get duration
duration = `avprobe #{filename} -show_format -loglevel panic`.split("\n").compact.select{|l| l =~ /duration/}.first.split('=')[1].to_i
# for each sample time, save a image
step = duration / 8
(0..8).each do |i|
`avconv -ss #{i*step} -i #{filename} -r 1 -vframes 1 -s 320x240 #{File.join temp_path, "#{i.to_s.rjust(8, '0')}.jpg"} -loglevel panic`
end
image_files = Dir.glob(File.join(temp_path, '*.jpg')).sort.join(' ')
# convert image to gif
`convert -delay 50 -loop 0 #{image_files} #{File.join preview_path, dir, File.basename(filename)}.gif`
`rm #{File.join temp_path, '*.jpg'}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment