Skip to content

Instantly share code, notes, and snippets.

@cairey
Created February 7, 2016 10:59
Show Gist options
  • Save cairey/dad05435084a8375e882 to your computer and use it in GitHub Desktop.
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.
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