Last active
August 29, 2015 14:13
-
-
Save halida/e64b071010e5f285608a to your computer and use it in GitHub Desktop.
Convert video to preview gif like those xxx websites, using libav-tools and imagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filename = 'a.mp4' | |
target_path = '../processing' | |
(1..8).each do |i| | |
t = i * 60 | |
target_file = File.join(target_path, "#{i}.jpg") | |
`avconv -i #{filename} -ss #{t} -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 '#{target_file}'` | |
end | |
image_files = File.join target_path, '*.jpg' | |
`convert -delay 50 -loop 0 #{image_files} #{filename}.gif` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
preview_path = File.expand_path 'preview' | |
temp_path = '/tmp/processing' | |
`mkdir -p #{temp_path}` | |
(Dir.glob('*') - ['preview']).each do |dir| | |
`mkdir -p #{File.join preview_path, dir}` | |
puts "list #{dir}: #{Dir.glob(File.join(dir, '*'))}" | |
Dir.glob(File.join(dir, '*')).each do |filename| | |
# filename = '1.mp4' | |
puts "converting: #{filename}" | |
# convert video to images | |
`avconv -i #{filename} -r 1 -s 320x240 #{File.join temp_path, '%08d.jpg'} ` | |
# sample images | |
image_files = Dir.glob(File.join(temp_path, '*.jpg')) | |
step = image_files.count / 8 | |
image_files = (0 .. 8).map { |i| image_files[i*step] }.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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
preview_path = File.expand_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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment