Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts Shotdetect's results.xml file to CSV
#!/usr/bin/env ruby
# Extracts shotdetect's results.xml file to csv
# with relative position of shot in movie.
# Writes converted file to results.csv file.
#
# Usage: ./shotdetect2csv.rb results.xml
require "nokogiri"
require "csv"
doc = nil
File.open ARGV[0] do |f|
doc = Nokogiri::XML f
end
msduration = doc.at_css('duration').text.to_f
nshots = doc.css('body shot').size.to_i
shots = []
doc.css('shots').children.each do |item|
# Convert Nokogiri::XML::Attr attributes to string and back to attributes hash
shot = Hash[item.attributes.map{ |k, v| [k.to_sym, v.to_s]} ]
# Calculate position of shot in the whole stream
shot[:msratio] = shot[:msbegin].to_f / msduration
shots << shot
end
CSV.open("shots.csv", "w") do |csv|
csv << shots.first.keys
shots.each do |hash|
csv << hash.values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment