Created
February 7, 2016 10:59
-
-
Save cairey/dad05435084a8375e882 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 C# version.
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
public double GetOffsetFromTimecode(Guid eventId, DateTime startPos, DateTime inPoint) | |
{ | |
startPos = startPos.ToUniversalTime(); | |
inPoint = inPoint.ToUniversalTime(); | |
var ismUrl = _configuration.GenerateIsmStreamUrl(eventId); | |
var streamUrl = ismUrl + "/archive"; | |
var doc = XDocument.Load(streamUrl); | |
var videoEl = doc.Descendants().First(x => x.Name.LocalName == "video"); | |
var cEls = videoEl.Descendants().Where(x => x.Name.LocalName == "c"); | |
var finalCEls = new List(); | |
var totalSeconds = TimeSpan.FromSeconds(0); | |
foreach(var cEl in cEls) | |
{ | |
var endOfC = DateTime.Parse(cEl.Attribute("end").Value).ToUniversalTime(); | |
if(endOfC > inPoint) | |
{ | |
finalCEls.Add(cEl); | |
} | |
} | |
foreach(var cEl in finalCEls) | |
{ | |
var startOfC = DateTime.Parse(cEl.Attribute("start").Value).ToUniversalTime(); | |
var endOfC = DateTime.Parse(cEl.Attribute("end").Value).ToUniversalTime(); | |
//override start if they fall into middle part | |
if (inPoint >= startOfC && inPoint <= endOfC) | |
{ | |
startOfC = inPoint; | |
} | |
if (startPos < endOfC) | |
{ | |
if (startPos < startOfC) break; | |
totalSeconds += (startPos.TimeOfDay - startOfC.TimeOfDay); | |
} | |
else | |
{ | |
totalSeconds += (endOfC.TimeOfDay - startOfC.TimeOfDay); | |
} | |
} | |
return totalSeconds.TotalSeconds; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment