Esempio n. 1
0
        private void btnChangeAssTiming_Click(object sender, EventArgs e)
        {
            try
            {
                String newStartStr = AcControls.AcInputBox.AcInputBox.Show("Give the new start of the first subtitle:", "New start...", "");
                String newEndStr   = AcControls.AcInputBox.AcInputBox.Show("Give the new start of the last subtitle:", "New end...", "");

                AssFile assF = new AssFile(txtAssFile.Text);
                assF.SectionEvents.SortEventLines(AssEventsSortType.ByStartTime, AcToolsLibrary.Core.Subtitles.ASS.SortOrder.Ascending);
                Int64 newStart = assF.SectionEvents.EventLines[0].GetTimeInMs(newStartStr);
                Int64 newEnd   = assF.SectionEvents.EventLines[0].GetTimeInMs(newEndStr) +
                                 assF.SectionEvents.EventLines[assF.SectionEvents.EventLines.Count - 1].Duration;
                Double ratio = Convert.ToDouble((newEnd - newStart)) /
                               Convert.ToDouble((assF.SectionEvents.EventLines[assF.SectionEvents.EventLines.Count - 1].EndValue
                                                 - assF.SectionEvents.EventLines[0].StartValue));

                Int64 lastOldEnd = 0;
                for (Int32 i = 0; i < assF.SectionEvents.EventLines.Count; i++)
                {
                    AssLineEvent assL = assF.SectionEvents.EventLines[i];
                    // if its the first line
                    if (i == 0)
                    {
                        Int64 oldDuration = assL.Duration;
                        Int64 newDuration = Convert.ToInt64(oldDuration * ratio);
                        lastOldEnd      = assL.EndValue;
                        assL.StartValue = newStart;
                        assL.EndValue   = assL.StartValue + newDuration;
                    }
                    else
                    {
                        Int64 oldDuration    = assL.Duration;
                        Int64 newDuration    = Convert.ToInt64(oldDuration * ratio);
                        Int64 oldGapDuration = assL.StartValue - lastOldEnd;
                        Int64 newGapDuration = Convert.ToInt64(oldGapDuration * ratio);
                        lastOldEnd      = assL.EndValue;
                        assL.StartValue = assF.SectionEvents.EventLines[i - 1].EndValue + newGapDuration;
                        assL.EndValue   = assL.StartValue + newDuration;
                    }
                }
                assF.SaveAs(assF.Filename + ".new.ass");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
Esempio n. 2
0
 private void btnFixAss2_Click(object sender, EventArgs e)
 {
     try
     {
         AssFile assF = new AssFile(txtAssFile.Text);
         assF.SectionEvents.SortEventLines(AssEventsSortType.ByStartTime, AcToolsLibrary.Core.Subtitles.ASS.SortOrder.Ascending);
         List <Int32> lineIndecesToBeRemoved = new List <int>();
         for (int i = 0; i < assF.SectionEvents.EventLines.Count; i++)
         {
             if (lineIndecesToBeRemoved.Contains(i))
             {
                 continue;
             }
             AssLineEvent myLine = assF.SectionEvents.EventLines[i];
             for (int j = i + 1; j < assF.SectionEvents.EventLines.Count; j++)
             {
                 // check only 50 lines ahead
                 if (j - i > 50)
                 {
                     break;
                 }
                 AssLineEvent lineToTest = assF.SectionEvents.EventLines[j];
                 if (myLine.Text.Trim() == lineToTest.Text.Trim())
                 {
                     lineIndecesToBeRemoved.Add(j);
                     myLine.EndValue = lineToTest.EndValue;
                 }
             }
         }
         // processing ended, time to remove lines
         List <Int32> sortedList = lineIndecesToBeRemoved.OrderByDescending(t => t).ToList();
         foreach (var i in sortedList)
         {
             assF.SectionEvents.EventLines.RemoveAt(i);
         }
         assF.SectionEvents.SortEventLines(AssEventsSortType.ByStartTime, AcToolsLibrary.Core.Subtitles.ASS.SortOrder.Ascending);
         assF.SaveAs(assF.Filename + ".fixed.ass");
     }
     catch (Exception ex)
     {
         ShowExceptionMessage(ex);
     }
 }