Skip to content

Instantly share code, notes, and snippets.

@cairey
Created February 7, 2016 11:00
Show Gist options
  • Save cairey/975ea7ab2ccdab48af95 to your computer and use it in GitHub Desktop.
Save cairey/975ea7ab2ccdab48af95 to your computer and use it in GitHub Desktop.
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 = nokogiri_doc.search('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
else
utc_result = start_of_c_utc + Rational(total_seconds, 86400)
break
end
end
utc_result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment