private void AlignDynamicStartParameters(Representation video) { var alignments = new List <AlignmentEntry>(); var videoStartSegment = video.Segments.StartSegmentId(); var videoSegmentId = video.Segments.PreviousSegmentId(videoStartSegment); if (!videoSegmentId.HasValue) { videoSegmentId = videoStartSegment; } // Scan 3 video segments starting from videoSegmentId. for (int s = 0; s < 3; s++) { var videoTimeRange = video.Segments.SegmentTimeRange(videoSegmentId); if (videoTimeRange == null) { continue; } // Split duration into 3 elements to cover 4 time points in segment duration // Start, Start+1/3*Duration, Start+2/3*Duration, Start+3/3*Duration // var tickOffset = videoTimeRange.Duration.Ticks / 3; var tickStart = videoTimeRange.Start.Ticks; for (int t = 0; t < 4; t++) { // Get audio SegmentId & Start time for a given time point var timePoint = TimeSpan.FromTicks(tickStart + (t * tickOffset)); var audioSegmentId = Segments.SegmentId(timePoint); var audioTimeRange = Segments.SegmentTimeRange(audioSegmentId); if (audioTimeRange == null) { continue; } alignments.Add(new AlignmentEntry(videoSegmentId, videoTimeRange.Start, audioSegmentId, audioTimeRange.Start)); } // Get Next video segment videoSegmentId = video.Segments.NextSegmentId(videoSegmentId); } var audioStartSegment = videoStartSegment = null; TimeSpan?trimmOffset = null; // handle the unexpected if (alignments.Count > 0) { var bestAlignment = (from AlignmentEntry in alignments orderby AlignmentEntry.AudioVideoStartDiff select AlignmentEntry).First(); videoStartSegment = bestAlignment.VideoSegmentID; audioStartSegment = bestAlignment.AudioSegmentID; var videoStart = bestAlignment.VideoSegmentStart; var audioStart = bestAlignment.AudioSegmentStart; trimmOffset = videoStart < audioStart ? videoStart : audioStart; } //Set aligned start data AlignedStartSegmentID = audioStartSegment; AlignedTrimOffset = trimmOffset; video.AlignedStartSegmentID = videoStartSegment; video.AlignedTrimOffset = trimmOffset; }