Пример #1
0
 private SubtitleFormat FinalizeFormat(string fileName, bool batchMode, double?sourceFrameRate, List <string> lines, SubtitleFormat subtitleFormat, bool loadSubtitle)
 {
     Header = null;
     subtitleFormat.BatchMode            = batchMode;
     subtitleFormat.BatchSourceFrameRate = sourceFrameRate;
     if (loadSubtitle)
     {
         subtitleFormat.LoadSubtitle(this, lines, fileName);
     }
     OriginalFormat = subtitleFormat;
     return(subtitleFormat);
 }
Пример #2
0
 private SubtitleFormat FinalizeFormat(string fileName, bool batchMode, double?sourceFrameRate, List <string> lines, SubtitleFormat subtitleFormat, bool loadSubtitle)
 {
     Header = null;
     subtitleFormat.BatchMode            = batchMode;
     subtitleFormat.BatchSourceFrameRate = sourceFrameRate;
     if (loadSubtitle)
     {
         subtitleFormat.LoadSubtitle(this, lines, fileName);
     }
     OriginalFormat            = subtitleFormat;
     WasLoadedWithFrameNumbers = OriginalFormat.IsFrameBased;
     if (WasLoadedWithFrameNumbers)
     {
         CalculateTimeCodesFromFrameNumbers(Configuration.Settings.General.CurrentFrameRate);
     }
     return(subtitleFormat);
 }
Пример #3
0
 public SubtitleFormat ReloadLoadSubtitle(List <string> lines, string fileName, SubtitleFormat format)
 {
     Paragraphs.Clear();
     if (format != null && format.IsMine(lines, fileName))
     {
         format.LoadSubtitle(this, lines, fileName);
         _format = format;
         return(format);
     }
     foreach (SubtitleFormat subtitleFormat in SubtitleFormat.AllSubtitleFormats)
     {
         if (subtitleFormat.IsMine(lines, fileName))
         {
             subtitleFormat.LoadSubtitle(this, lines, fileName);
             _format = subtitleFormat;
             return(subtitleFormat);
         }
     }
     return(null);
 }
Пример #4
0
        private void SortAndLoad()
        {
            JoinedFormat = new SubRip(); // default subtitle format
            string         header     = null;
            SubtitleFormat lastFormat = null;
            var            subtitles  = new List <Subtitle>();

            for (int k = 0; k < _fileNamesToJoin.Count; k++)
            {
                string fileName = _fileNamesToJoin[k];
                try
                {
                    var            sub    = new Subtitle();
                    SubtitleFormat format = null;
                    var            lines  = FileUtil.ReadAllLinesShared(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName));
                    if (lastFormat != null && lastFormat.IsMine(lines, fileName))
                    {
                        format = lastFormat;
                        format.LoadSubtitle(sub, lines, fileName);
                    }

                    format = sub.LoadSubtitle(fileName, out _, null);

                    if (format == null)
                    {
                        if (lines.Count > 0 && lines.Count < 10 && lines[0].Trim() == "WEBVTT")
                        {
                            format = new WebVTT(); // empty WebVTT
                        }
                    }

                    if (format == null)
                    {
                        foreach (var binaryFormat in SubtitleFormat.GetBinaryFormats(true))
                        {
                            if (binaryFormat.IsMine(null, fileName))
                            {
                                binaryFormat.LoadSubtitle(sub, null, fileName);
                                format = binaryFormat;
                                break;
                            }
                        }
                    }

                    if (format == null)
                    {
                        foreach (var f in SubtitleFormat.GetTextOtherFormats())
                        {
                            if (f.IsMine(lines, fileName))
                            {
                                f.LoadSubtitle(sub, lines, fileName);
                                format = f;
                                break;
                            }
                        }
                    }

                    if (format == null)
                    {
                        for (int j = k; j < _fileNamesToJoin.Count; j++)
                        {
                            _fileNamesToJoin.RemoveAt(j);
                        }
                        MessageBox.Show("Unkown subtitle format: " + fileName);
                        break;
                    }
                    if (sub.Header != null)
                    {
                        header = sub.Header;
                    }

                    if (lastFormat == null || lastFormat.FriendlyName == format.FriendlyName)
                    {
                        lastFormat = format;
                    }
                    else
                    {
                        lastFormat = new SubRip(); // default subtitle format
                    }

                    subtitles.Add(sub);
                }
                catch (Exception exception)
                {
                    for (int j = k; j < _fileNamesToJoin.Count; j++)
                    {
                        _fileNamesToJoin.RemoveAt(j);
                    }
                    MessageBox.Show(exception.Message);
                    return;
                }
            }
            JoinedFormat = lastFormat;

            for (int outer = 0; outer < subtitles.Count; outer++)
            {
                for (int inner = 1; inner < subtitles.Count; inner++)
                {
                    var a = subtitles[inner - 1];
                    var b = subtitles[inner];
                    if (a.Paragraphs.Count > 0 && b.Paragraphs.Count > 0 && a.Paragraphs[0].StartTime.TotalMilliseconds > b.Paragraphs[0].StartTime.TotalMilliseconds)
                    {
                        string t1 = _fileNamesToJoin[inner - 1];
                        _fileNamesToJoin[inner - 1] = _fileNamesToJoin[inner];
                        _fileNamesToJoin[inner]     = t1;

                        var t2 = subtitles[inner - 1];
                        subtitles[inner - 1] = subtitles[inner];
                        subtitles[inner]     = t2;
                    }
                }
            }

            listViewParts.BeginUpdate();
            listViewParts.Items.Clear();
            int i = 0;

            foreach (string fileName in _fileNamesToJoin)
            {
                var sub = subtitles[i];
                var lvi = new ListViewItem($"{sub.Paragraphs.Count:#,###,###}");
                if (sub.Paragraphs.Count > 0)
                {
                    lvi.SubItems.Add(sub.Paragraphs[0].StartTime.ToString());
                    lvi.SubItems.Add(sub.Paragraphs[sub.Paragraphs.Count - 1].StartTime.ToString());
                }
                else
                {
                    lvi.SubItems.Add("-");
                    lvi.SubItems.Add("-");
                }
                lvi.SubItems.Add(fileName);
                listViewParts.Items.Add(lvi);
                i++;
            }
            listViewParts.EndUpdate();

            JoinedSubtitle = new Subtitle();
            if (JoinedFormat != null && JoinedFormat.FriendlyName != SubRip.NameOfFormat)
            {
                JoinedSubtitle.Header = header;
            }

            var addTime = radioButtonJoinAddTime.Checked;

            foreach (var sub in subtitles)
            {
                double addMs = 0;
                if (addTime && JoinedSubtitle.Paragraphs.Count > 0)
                {
                    addMs = JoinedSubtitle.Paragraphs.Last().EndTime.TotalMilliseconds + Convert.ToDouble(numericUpDownAddMs.Value);
                }
                foreach (var p in sub.Paragraphs)
                {
                    p.StartTime.TotalMilliseconds += addMs;
                    p.EndTime.TotalMilliseconds   += addMs;
                    JoinedSubtitle.Paragraphs.Add(p);
                }
            }
            JoinedSubtitle.Renumber();
            labelTotalLines.Text = string.Format(Configuration.Settings.Language.JoinSubtitles.TotalNumberOfLinesX, JoinedSubtitle.Paragraphs.Count);
        }
Пример #5
0
        internal static Subtitle LoadMatroskaSSA(MatroskaTrackInfo matroskaSubtitleInfo, string fileName, SubtitleFormat format, List<MatroskaSubtitle> sub)
        {
            var subtitle = new Subtitle { Header = matroskaSubtitleInfo.CodecPrivate };
            var lines = subtitle.Header.Trim().SplitToLines().ToList();

            var footer = new StringBuilder();
            var comments = new Subtitle();
            if (!string.IsNullOrEmpty(matroskaSubtitleInfo.CodecPrivate))
            {
                bool footerOn = false;
                foreach (string line in lines)
                {
                    if (footerOn)
                    {
                        footer.AppendLine(line);
                    }
                    else if (line.Trim() == "[Events]")
                    {
                    }
                    else if (line.Trim() == "[Fonts]" || line.Trim() == "[Graphics]")
                    {
                        footerOn = true;
                        footer.AppendLine();
                        footer.AppendLine();
                        footer.AppendLine(line);
                    }
                    else if (line.StartsWith("Comment:", StringComparison.Ordinal))
                    {
                        var arr = line.Split(',');
                        if (arr.Length > 3)
                        {
                            arr = arr[1].Split(new[] { ':', '.' });
                            if (arr.Length == 4)
                            {
                                int hour;
                                int min;
                                int sec;
                                int ms;
                                if (int.TryParse(arr[0], out hour) && int.TryParse(arr[1], out min) && int.TryParse(arr[2], out sec) && int.TryParse(arr[3], out ms))
                                {
                                    comments.Paragraphs.Add(new Paragraph(new TimeCode(hour, min, sec, ms * 10), new TimeCode(0, 0, 0, 0), line));
                                }
                            }
                        }
                    }
                }
            }

            const string headerFormat = "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text";
            if (!subtitle.Header.Contains("[Events]"))
            {
                subtitle.Header = subtitle.Header.Trim() + Environment.NewLine + Environment.NewLine + "[Events]" + Environment.NewLine + headerFormat + Environment.NewLine;
            }
            else
            {
                subtitle.Header = subtitle.Header.Remove(subtitle.Header.IndexOf("[Events]", StringComparison.Ordinal));
                subtitle.Header = subtitle.Header.Trim() + Environment.NewLine + Environment.NewLine + "[Events]" + Environment.NewLine + headerFormat + Environment.NewLine;
            }

            lines = subtitle.Header.Trim().SplitToLines().ToList();

            const string timeCodeFormat = "{0}:{1:00}:{2:00}.{3:00}"; // h:mm:ss.cc
            foreach (var mp in sub)
            {
                var p = new Paragraph(string.Empty, mp.Start, mp.End);
                string start = string.Format(timeCodeFormat, p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10);
                string end = string.Format(timeCodeFormat, p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds / 10);

                // MKS contains this: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text
                lines.AddRange(from cp in comments.Paragraphs where cp.StartTime.TotalMilliseconds <= p.StartTime.TotalMilliseconds select cp.Text);

                for (int commentIndex = comments.Paragraphs.Count - 1; commentIndex >= 0; commentIndex--)
                {
                    var cp = comments.Paragraphs[commentIndex];
                    if (cp.StartTime.TotalMilliseconds <= p.StartTime.TotalMilliseconds)
                    {
                        comments.Paragraphs.RemoveAt(commentIndex);
                    }
                }

                string text = mp.Text.Replace(Environment.NewLine, "\\N");
                int idx = text.IndexOf(',') + 1;
                if (idx > 0 && idx < text.Length)
                {
                    text = text.Remove(0, idx); // remove ReadOrder
                    idx = text.IndexOf(',');
                    text = text.Insert(idx, "," + start + "," + end);
                    lines.Add("Dialogue: " + text);
                }
            }

            lines.AddRange(comments.Paragraphs.Select(cp => cp.Text));

            lines.AddRange(footer.ToString().SplitToLines());

            format.LoadSubtitle(subtitle, lines, fileName);
            return subtitle;
        }