Exemplo n.º 1
0
        /// <summary>Auto syncs a range of subtitles given their first and last correct frames.</summary>
        /// <remarks>The subtitles are first shifted to the first subtitle's correct frame, and then proportionally
        /// adjusted using the last subtitle's correct frame.</remarks>
        /// <param name="subtitles">The subtitles to sync.</param>
        /// <param name="startIndex">The subtitle index to start the adjustment with.</param>
        /// <param name="startFrame">The correct start frame for the first subtitle.</param>
        /// <param name="endIndex">The subtitle index to end the adjustment with.</param>
        /// <param name="endFrame">The correct start frame for the last subtitle.</param>
        /// <param name="syncLast">Whether to sync the last subtitle.</param>
        /// <returns>Whether the subtitles could be adjusted.</returns>
        public static bool Sync(Subtitles subtitles, int startIndex, int startFrame, int endIndex, int endFrame, bool syncLast)
        {
            if (!AreSyncArgsValid(subtitles, startIndex, startFrame, endIndex, endFrame))
            {
                return(false);
            }

            /* Perform initial calculations */
            int      syncEndIndex  = (syncLast ? endIndex : endIndex - 1);
            Subtitle startSubtitle = subtitles.Collection.Get(startIndex);
            int      shift         = (int)(startFrame - startSubtitle.Frames.PreciseStart);
            Subtitle endSubtitle   = subtitles.Collection.Get(endIndex);
            double   factor        = (endFrame - startFrame) / (endSubtitle.Frames.PreciseStart - startFrame);

            /* Shift subtitles to the start point */
            if (shift != 0)
            {
                ShiftOperator shiftOp = new ShiftOperator(subtitles);
                shiftOp.Shift(shift, startIndex, syncEndIndex);
            }

            /* Auto adjust timings with proportion */
            for (int index = startIndex; index <= syncEndIndex; index++)
            {
                Subtitle subtitle = subtitles.Collection.Get(index);
                subtitle.Frames.Scale(factor, startFrame);
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>Auto syncs a range of subtitles given their first and last correct times.</summary>
        /// <remarks>The subtitles are first shifted to the first subtitle's correct time, and then proportionally
        /// adjusted using the last subtitle's correct time.</remarks>
        /// <param name="subtitles">The subtitles to sync.</param>
        /// <param name="startIndex">The subtitle index to start the adjustment with.</param>
        /// <param name="startTime">The correct start time for the first subtitle.</param>
        /// <param name="endIndex">The subtitle index to end the adjustment with.</param>
        /// <param name="endTime">The correct start time for the last subtitle.</param>
        /// <param name="syncLast">Whether to sync the last subtitle.</param>
        /// <returns>Whether the subtitles could be adjusted.</returns>
        public static bool Sync(Subtitles subtitles, int startIndex, TimeSpan startTime, int endIndex, TimeSpan endTime, bool syncLast)
        {
            if (!AreSyncArgsValid(subtitles, startIndex, startTime, endIndex, endTime))
            {
                return(false);
            }

            /* Perform initial calculations */
            int      syncEndIndex  = (syncLast ? endIndex : endIndex - 1);
            Subtitle startSubtitle = subtitles.Collection.Get(startIndex);
            TimeSpan shift         = startTime - startSubtitle.Times.PreciseStart;
            Subtitle endSubtitle   = subtitles.Collection.Get(endIndex);
            double   factor        = (endTime - startTime).TotalMilliseconds / (endSubtitle.Times.PreciseStart - startSubtitle.Times.PreciseStart).TotalMilliseconds;

            /* Shift subtitles to the start point */
            if (shift != TimeSpan.Zero)
            {
                ShiftOperator shiftOp = new ShiftOperator(subtitles);
                shiftOp.Shift(shift, startIndex, syncEndIndex);
            }

            /* Sync timings with proportion */
            for (int index = startIndex; index <= syncEndIndex; index++)
            {
                Subtitle subtitle = subtitles.Collection.Get(index);
                subtitle.Times.Scale(factor, startTime);
            }
            return(true);
        }