Created
May 3, 2019 13:31
-
-
Save roberttravispierce/a6faf5450de2bcf2ce9dabec4948cbff to your computer and use it in GitHub Desktop.
Rails Service - import_pnotes_service.rb
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
require 'csv' | |
require 'nokogiri' | |
# Invoke: > ImportPnotesService.new.call | |
class ImportPnotesService | |
def call | |
log = ActiveSupport::Logger.new('log/import_pnotes.log') | |
puts "\n What is the program ID?" | |
pgm_title = STDIN.gets.chomp | |
markers_array = [] | |
fcpmarkers_list = "" | |
start_offset = 216384168/60000 | |
in_time = "" | |
out_time = "" | |
csv_text = File.read(Rails.root.join('db', 'seeds', 'pnotes', 'pnotes.csv')) | |
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') | |
csv.each do |row| | |
# Types = 'In','Out','Todo','Note','Chapter' | |
case row['type'] | |
when 'In' | |
in_time = row['time'] | |
when 'Out' | |
out_time = row['time'] | |
end | |
cleaned_note = row['note'].delete('"').delete("''") # quotes in the notes will mess with xml parsing | |
markers_array << {time: row['time'], note: cleaned_note, type: row['type']} | |
end | |
log.info markers_array | |
markers_array.each.with_index(1) do |marker, idx| | |
marker_tc = Timecode.parse(marker[:time], fps = 59.94) | |
marker_seconds = marker_tc.to_seconds | |
marker_elapsed = (marker_seconds - tc_in.to_seconds) + start_offset | |
fcpmarkers_list << fcpxml_marker(marker, marker_elapsed, idx) | |
end | |
open("tmp/#{pgm_title}-Markers.fcpxml", 'w') { |f| | |
f.puts build_fcpxml(pgm_title, fcpmarkers_list) | |
} | |
log.close | |
end | |
private | |
def fcpxml_marker(marker, marker_elapsed, idx) | |
rational_seconds = "#{(marker_elapsed * 60000).round} /60000" | |
marker_string = "\n" + %q( <marker start=") + rational_seconds + %q(s" duration="1001/60000s" value=") + idx.to_s + %q(. ) + marker[:note] + %q( [) + marker[:time] + %q(]" completed="0"/>) | |
end | |
def build_fcpxml(program_id, fcpmarkers_list) | |
fcpxml_string = <<END | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE fcpxml> | |
<fcpxml version="1.7"> | |
<resources> | |
<format id="r1" name="FFVideoFormat1080p5994" frameDuration="1001/60000s" width="1920" height="1080" colorSpace="1-1-1 (Rec. 709)"/> | |
<effect id="r2" name="Basic Title" uid=".../Titles.localized/Bumper:Opener.localized/Basic Title.localized/Basic Title.moti"/> | |
</resources> | |
<library location=""> | |
<event name="Assets" uid=""> | |
<project name="#{program_id} Markers" uid="" modDate=""> | |
<sequence duration="208799591/60000s" format="r1" tcStart="0s" tcFormat="DF" audioLayout="stereo" audioRate="48k" renderFormat="FFRenderFormatProRes422LT"> | |
<spine> | |
<gap name="Gap" offset="0s" duration="208799591/60000s" start="215999784/60000s"> | |
<title name="Post-Production Working Copy - #{program_id}" lane="1" offset="215999784/60000s" ref="r2" duration="417599182/120000s" start="216075860/60000s" enabled="0"> | |
<param name="Position" key="9999/999166631/999166633/1/100/101" value="-36.8896 407.999"/> | |
<param name="Flatten" key="9999/999166631/999166633/2/351" value="1"/> | |
<param name="Alignment" key="9999/999166631/999166633/2/354/999169573/401" value="1 (Center)"/> | |
<text> | |
<text-style ref="ts1">Post-Production Working Copy</text-style> | |
</text> | |
<text-style-def id="ts1"> | |
<text-style font="Helvetica" fontSize="94" fontFace="Regular" fontColor="0.999993 1 1 0.3183" alignment="center"/> | |
</text-style-def>#{fcpmarkers_list} | |
</title> | |
</gap> | |
</spine> | |
</sequence> | |
</project> | |
</event> | |
<smart-collection name="Projects" match="all"> | |
<match-clip rule="is" type="project"/> | |
</smart-collection> | |
<smart-collection name="All Video" match="any"> | |
<match-media rule="is" type="videoOnly"/> | |
<match-media rule="is" type="videoWithAudio"/> | |
</smart-collection> | |
<smart-collection name="Audio Only" match="all"> | |
<match-media rule="is" type="audioOnly"/> | |
</smart-collection> | |
<smart-collection name="Stills" match="all"> | |
<match-media rule="is" type="stills"/> | |
</smart-collection> | |
<smart-collection name="Favorites" match="all"> | |
<match-ratings value="favorites"/> | |
</smart-collection> | |
</library> | |
</fcpxml> | |
END | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment