Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2016 11:00
  • 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?
Grab an UTC timecode from an offset in seconds on a Unified Streaming Server. Takes it to account encoder reconnects / restarts.
def timecode_from_offset (nokogiri_doc, pos_in_secs, in_point_utc)
utc_result = in_point_utc
video_el ='video').first
c_els = video_el.css('c')
final_c_els = []
total_seconds = pos_in_secs
c_els.each do |c_el|
end_of_c_utc = DateTime.parse(c_el['end'])
if end_of_c_utc > in_point_utc
final_c_els << c_el 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 c_length = end_of_c_time - start_of_c_time if total_seconds > c_length
total_seconds -= c_length
utc_result = start_of_c_utc + Rational(total_seconds, 86400)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment