Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2014 22:35
  • 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
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 ARGV[0] do |f|
doc = Nokogiri::XML f
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[{ |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"shots.csv", "w") do |csv|
csv << shots.first.keys
shots.each do |hash|
csv << hash.values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment