Skip to content

Instantly share code, notes, and snippets.

@cairey
Created February 7, 2016 10:59
Show Gist options
  • Save cairey/4bd50729642fcd1720aa to your computer and use it in GitHub Desktop.
Save cairey/4bd50729642fcd1720aa to your computer and use it in GitHub Desktop.
Grab an offset in seconds from a UTC timecode on a Unified Streaming Server. Takes it to account encoder reconnects / restarts. This is the Ruby version.
def offset_from_timecode (nokogiri_doc, start_pos_utc, in_point_utc)
video_el = nokogiri_doc.search('video').first
c_els = video_el.css('c')
final_c_els = []
total_seconds = 0
start_pos_time = Time.parse(start_pos_utc.to_s)
c_els.each do |c_el|
end_of_c_utc = DateTime.parse(c_el['end'])
final_c_els << c_el if end_of_c_utc > in_point_utc
end
end
final_c_els.each do |c_el|
start_of_c_utc = DateTime.parse(c_el['start'])
end_of_c_utc = DateTime.parse(c_el['end'])
start_of_c_time = Time.parse(c_el['start'])
end_of_c_time = Time.parse(c_el['end'])
# override starts if they fall into the middle part
if (in_point_utc >= start_of_c_utc && in_point_utc <= end_of_c_utc)
start_of_c_utc = in_point_utc
start_of_c_time = Time.parse(in_point_utc.to_s)
end
if start_pos_utc < end_of_c_utc
break if start_pos_utc < start_of_c_utc
else
total_seconds += (start_pos_time - start_of_c_time)
end
else
total_seconds += (end_of_c_time - start_of_c_time)
end
end
total_seconds
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment