Skip to content

Instantly share code, notes, and snippets.

@hakonrossebo
Created August 30, 2011 20:48
Show Gist options
  • Save hakonrossebo/1181978 to your computer and use it in GitHub Desktop.
Save hakonrossebo/1181978 to your computer and use it in GitHub Desktop.
Ruby script to rename the NDC2011 torrent files to also show the title (old was "Track3 1020-1120
#Script to correct NDC video file naming (ie:Track3 1140-1240.mp4 to the correct title)
#This script will parse a specific xml file and rename the NDC2011 videos according to the information in the xml file
#Copy xml text from here: https://gist.github.com/1034721 (thanks to GitHub user dauger)
require 'rexml/document'
require 'pathname'
require 'find'
class NDCSession
@@DayMapping = {"Day 1" => "Day1 Wednesday",
"Day 2" => "Day2 Thursday",
"Day 3" => "Day3 Friday"}
attr_accessor :existing_filepath, :new_filepath
def initialize (basepath, day, track, title, comments)
@existing_filepath = nil
@new_filepath = nil
if comments and comments.include? "wmv"
filename_part2 = File.basename(comments)
filename_part2["wmv"] = "mp4"
if !(filename_part2.include? "Live") #These videos does not reference mp4 files
session_track = track[0,7].gsub(/\s/, '') #Remove all but track number, remove space
new_title = title[4..-1].gsub(/[^0-9A-Za-z ]/, '')#Only valid characters, remove beginning of title
if day
@existing_filepath = (basepath.to_s + '/' + @@DayMapping[day] + '/' + session_track + " " + filename_part2).strip
@new_filepath = basepath.to_s + '/' + @@DayMapping[day] + '/' + session_track + " " + new_title + ".mp4"
end
end
end
end
def RenameFile
puts "Renaming file: " + existing_filepath + " to " + new_filepath
File.rename(existing_filepath, new_filepath) #comment out this line to only verify correct renaming
end
def to_s
"#@existing_filepath"
end
end
NDCVideoLocation = Pathname.new(File.join('e:', 'utvikling', 'NDC2011mp4'))
NDCXMLPath = Pathname.new(File.join('e:', 'utvikling', 'NDC2011mp4', 'ndc2011.xml')) #copy xml text into this file
xml = File.read(NDCXMLPath)
doc = REXML::Document.new(xml)
lookup = {}
doc.elements.each('TODOLIST/TASK/TASK/TASK') do |taskelement|
ndcsession = NDCSession.new( NDCVideoLocation,
taskelement.parent.parent.attributes['TITLE'],
taskelement.parent.attributes['TITLE'],
taskelement.attributes['TITLE'],
taskelement.attributes['COMMENTS'])
if ndcsession.existing_filepath
lookup[ndcsession.existing_filepath] = ndcsession
end
end
Find.find(NDCVideoLocation.to_s) do |mp4file|
if mp4file.match(/\.mp4\Z/)
if lookup.has_key?(mp4file)
lookup[mp4file].RenameFile
else
puts "No new name found in xml for: " + mp4file
end
end
end
@hakonrossebo
Copy link
Author

I've only tested this script on Windows. Please feel free to add improvements/corrections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment