Exemplo n.º 1
0
        private void AlignStaticStartParameters(Representation video)
        {
            var audioStartSegment = Segments.StartSegmentId();

            if (audioStartSegment == null)
            {
                throw new ArgumentNullException("audio StartSegmentId is null");
            }
            var audioTimeRange = Segments.SegmentTimeRange(audioStartSegment);

            if (audioTimeRange == null)
            {
                throw new ArgumentNullException("audio SegmentTimeRange is null");
            }

            var videoStartSegment = video.Segments.StartSegmentId();

            if (videoStartSegment == null)
            {
                throw new ArgumentNullException("video StartSegmentId is null");
            }
            var videoTimeRange = video.Segments.SegmentTimeRange(videoStartSegment);

            if (videoTimeRange == null)
            {
                throw new ArgumentNullException("vdeo SegmentTimeRange is null");
            }
            var trimOffset = audioTimeRange.Start < videoTimeRange.Start ? audioTimeRange.Start : videoTimeRange.Start;

            //Set aligned start data
            AlignedStartSegmentID       = audioStartSegment;
            AlignedTrimOffset           = trimOffset;
            video.AlignedStartSegmentID = videoStartSegment;
            video.AlignedTrimOffset     = trimOffset;
        }
Exemplo n.º 2
0
        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;
        }