Last active
December 10, 2015 21:09
-
-
Save cruinh/4492921 to your computer and use it in GitHub Desktop.
ruby script for generating contact sheet images for video files.
This file contains 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
# ruby script for generating contact sheet images for video files. | |
# can be configured via an ini file which is expected to be in the current working directory. | |
# Requirements: | |
# - VLC (http://videolan.org) must be installed and available in the system PATH. | |
# - gem iniparse. | |
# | |
# vidsnaps will open the given video in VLC and save a screenshot at predetermined time intervals. | |
# By default it will save 20 images 500 seconds apart starting at the very beginning of the video. | |
# If these settings take it beyond the end of the video, a number of black screens will be generated, which can be ignored/deleted | |
# images are saved as png files using the format "##h##m##s.png". | |
# | |
# On Windows this script works well compiled into an ocra EXE (https://github.com/larsch/ocra). Videos can then just be dragged right onto the EXE icon to produce the screens. | |
# | |
# example ini file: | |
# [Defaults] | |
# outputFolder=. ; output to current directory | |
# timeOffset=10 ; first image 10 seconds in | |
# imageCount=20 ; produce 20 images | |
# imageSpacing=500 ; each image 500 seconds appart | |
# inputFile=test.avi | |
# outputFormat=png | |
require 'iniparse' | |
ininame = 'vidsnaps.ini' | |
outputFolder = "." | |
timeOffset = 0.0 | |
imageCount = 1 | |
imageSpacing = 0.5 | |
runTime = 2 | |
useOutputPrefix = false | |
outputFormat = "png" | |
if File.exists?(ininame) | |
iniDoc = IniParse.parse(File.read(ininame)) # => IniParse::Document | |
if !iniDoc['Defaults']['outputFolder'].nil? | |
outputFolder = iniDoc['Defaults']['outputFolder'] | |
end | |
if !iniDoc['Defaults']['timeOffset'].nil? | |
timeOffset = iniDoc['Defaults']['timeOffset'].to_f | |
end | |
if !iniDoc['Defaults']['runTime'].nil? | |
runTime = iniDoc['Defaults']['runTime'].to_f | |
end | |
if !iniDoc['Defaults']['imageCount'].nil? | |
imageCount = iniDoc['Defaults']['imageCount'].to_i | |
end | |
if !iniDoc['Defaults']['imageSpacing'].nil? | |
imageSpacing = iniDoc['Defaults']['imageSpacing'].to_f | |
end | |
if !iniDoc['Defaults']['outputFormat'].nil? | |
outputFormat = iniDoc['Defaults']['outputFormat'] | |
end | |
if !iniDoc['Defaults']['inputFile'].nil? | |
inputFile = iniDoc['Defaults']['inputFile'] | |
end | |
if !iniDoc['Defaults']['prefixOutputWithInput'].nil? | |
useOutputPrefix = iniDoc['Defaults']['prefixOutputWithInput'] | |
end | |
end | |
if inputFile.nil? && ARGV.count < 1 | |
puts "First argument must be video filename" | |
exit 1 | |
end | |
if ARGV.count == 1 | |
inputFile = ARGV[0] | |
end | |
puts "generating screens for #{inputFile}" | |
puts "with parameters: " | |
puts " outputFolder #{outputFolder}" | |
puts " timeOffset #{timeOffset}" | |
puts " imageCount #{imageCount}" | |
puts " imageSpacing #{imageSpacing}" | |
puts " runTime #{runTime}" | |
puts " useOutputPrefix #{useOutputPrefix}" | |
puts " outputFormat #{outputFormat}" | |
puts " inputFile #{inputFile}" | |
for i in 1..imageCount | |
startTime = i*imageSpacing+timeOffset | |
hours = (startTime / 60 / 60).to_i | |
minutes = (startTime / 60 - hours*60).to_i | |
seconds = startTime - minutes*60.0 - hours*3600.0 | |
puts "snapshot at #{"%02d" % hours}:#{"%02d" % minutes}:#{"%02f" % seconds}..." | |
if useOutputPrefix | |
filePrefix = "#{inputFile.chomp(File.extname(inputFile))}_" | |
else | |
filePrefix = "" | |
end | |
imagePrefix = "#{filePrefix}#{"%02d" % hours}:#{"%02d" % minutes}:#{"%02.3f" % seconds}" | |
command = "vlc \"#{inputFile}\" --verbose=0 --quiet-synchro --start-time=#{startTime} --run-time=#{runTime} --scene-format=#{outputFormat} --aout=dummy --video-filter=scene --vout=dummy --scene-path=. --scene-replace --scene-prefix=#{imagePrefix} -Idummy -q vlc://quit" | |
puts command | |
system(command) | |
puts "done." | |
end | |
puts "done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment