Пример #1
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();
                    var format = sub.LoadSubtitle(fileName, out _, null);

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

                    if (format == null)
                    {
                        var encoding = LanguageAutoDetect.GetEncodingFromFile(fileName);
                        var lines    = FileUtil.ReadAllTextShared(fileName, encoding).SplitToLines();
                        foreach (var f in SubtitleFormat.GetTextOtherFormats())
                        {
                            if (f.IsMine(lines, fileName))
                            {
                                _fileNamesToJoin.Add(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);
                        return;
                    }
                    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;
            }

            foreach (var sub in subtitles)
            {
                foreach (var p in sub.Paragraphs)
                {
                    JoinedSubtitle.Paragraphs.Add(p);
                }
            }
            JoinedSubtitle.Renumber();
            labelTotalLines.Text = string.Format(Configuration.Settings.Language.JoinSubtitles.TotalNumberOfLinesX, JoinedSubtitle.Paragraphs.Count);
        }
Пример #2
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;
                    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)
                    {
                        Revert(k, Configuration.Settings.Language.UnknownSubtitle.Title + Environment.NewLine + fileName);
                        break;
                    }

                    if (sub.Header != null)
                    {
                        header = sub.Header;
                    }

                    lastFormat = lastFormat == null || lastFormat.FriendlyName == format.FriendlyName ? format : new SubRip();

                    subtitles.Add(sub);
                }
                catch (Exception exception)
                {
                    Revert(k, exception.Message);
                    return;
                }
            }
            JoinedFormat = lastFormat;


            if (!radioButtonJoinAddTime.Checked)
            {
                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);
        }
        private void AddInputFile(string fileName)
        {
            foreach (ListViewItem lvi in listViewInputFiles.Items)
            {
                if (lvi.Text.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
            }

            var fi   = new FileInfo(fileName);
            var ext  = fi.Extension.ToLowerInvariant();
            var item = new ListViewItem(fileName);

            item.SubItems.Add(Utilities.FormatBytesToDisplayFileSize(fi.Length));
            var sub = new Subtitle();

            if (fi.Length < 500_000)
            {
                if (!FileUtil.IsBluRaySup(fileName) && !FileUtil.IsVobSub(fileName) &&
                    !((ext == ".mkv" || ext == ".mks") && FileUtil.IsMatroskaFile(fileName)))
                {
                    var format = sub.LoadSubtitle(fileName, out _, null);

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

                    if (format == null)
                    {
                        var encoding = LanguageAutoDetect.GetEncodingFromFile(fileName);
                        var lines    = FileUtil.ReadAllTextShared(fileName, encoding).SplitToLines();
                        foreach (var f in SubtitleFormat.GetTextOtherFormats())
                        {
                            if (f.IsMine(lines, fileName))
                            {
                                f.LoadSubtitle(sub, lines, fileName);
                                format = f;
                                break;
                            }
                        }
                    }

                    if (format != null)
                    {
                        item.SubItems.Add(format.Name);
                        listViewInputFiles.Items.Add(item);
                        _subtitleList.Add(sub);
                    }
                }
            }
        }
Пример #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++)
            {
                var fileName = _fileNamesToJoin[k];
                try
                {
                    var            sub    = new Subtitle();
                    SubtitleFormat format = null;

                    if (fileName.EndsWith(".ismt", StringComparison.InvariantCultureIgnoreCase) ||
                        fileName.EndsWith(".mp4", StringComparison.InvariantCultureIgnoreCase) ||
                        fileName.EndsWith(".m4v", StringComparison.InvariantCultureIgnoreCase) ||
                        fileName.EndsWith(".3gp", StringComparison.InvariantCultureIgnoreCase))
                    {
                        format = new IsmtDfxp();
                        if (format.IsMine(null, fileName))
                        {
                            var s = new Subtitle();
                            format.LoadSubtitle(s, null, fileName);
                            if (s.Paragraphs.Count > 0)
                            {
                                lastFormat = format;
                            }
                        }
                    }

                    var lines = FileUtil.ReadAllLinesShared(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName));
                    if (lastFormat != null && lastFormat.IsMine(lines, fileName))
                    {
                        format = lastFormat;
                        format.LoadSubtitle(sub, lines, fileName);
                    }

                    if (sub.Paragraphs.Count == 0 || format == null)
                    {
                        format = sub.LoadSubtitle(fileName, out _, null);
                    }

                    if (format == null && 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)
                    {
                        Revert(k, LanguageSettings.Current.UnknownSubtitle.Title + Environment.NewLine + fileName);
                        break;
                    }

                    if (sub.Header != null)
                    {
                        if (format.Name == AdvancedSubStationAlpha.NameOfFormat && header != null)
                        {
                            var oldPlayResX = AdvancedSubStationAlpha.GetTagFromHeader("PlayResX", "[Script Info]", header);
                            var oldPlayResY = AdvancedSubStationAlpha.GetTagFromHeader("PlayResY", "[Script Info]", header);
                            var newPlayResX = AdvancedSubStationAlpha.GetTagFromHeader("PlayResX", "[Script Info]", sub.Header);
                            var newPlayResY = AdvancedSubStationAlpha.GetTagFromHeader("PlayResY", "[Script Info]", sub.Header);

                            var stylesInHeader = AdvancedSubStationAlpha.GetStylesFromHeader(header);
                            var styles         = new List <SsaStyle>();
                            foreach (var styleName in stylesInHeader)
                            {
                                styles.Add(AdvancedSubStationAlpha.GetSsaStyle(styleName, header));
                            }

                            foreach (var newStyle in AdvancedSubStationAlpha.GetStylesFromHeader(sub.Header))
                            {
                                if (stylesInHeader.Any(p => p == newStyle))
                                {
                                    var styleToBeRenamed = AdvancedSubStationAlpha.GetSsaStyle(newStyle, sub.Header);
                                    var newName          = styleToBeRenamed.Name + "_" + Guid.NewGuid();
                                    foreach (var p in sub.Paragraphs.Where(p => p.Extra == styleToBeRenamed.Name))
                                    {
                                        p.Extra = newName;
                                    }

                                    styleToBeRenamed.Name = newName;
                                    styles.Add(styleToBeRenamed);
                                }
                                else
                                {
                                    styles.Add(AdvancedSubStationAlpha.GetSsaStyle(newStyle, sub.Header));
                                }
                            }

                            header = AdvancedSubStationAlpha.GetHeaderAndStylesFromAdvancedSubStationAlpha(header, styles);
                            if (!string.IsNullOrEmpty(oldPlayResX) && string.IsNullOrEmpty(newPlayResX))
                            {
                                header = AdvancedSubStationAlpha.AddTagToHeader("PlayResX", oldPlayResX, "[Script Info]", header);
                            }
                            if (!string.IsNullOrEmpty(oldPlayResY) && string.IsNullOrEmpty(newPlayResY))
                            {
                                header = AdvancedSubStationAlpha.AddTagToHeader("PlayResY", oldPlayResY, "[Script Info]", header);
                            }
                        }
                        else
                        {
                            header = sub.Header;
                        }
                    }

                    lastFormat = lastFormat == null || lastFormat.FriendlyName == format.FriendlyName ? format : new SubRip();

                    subtitles.Add(sub);
                }
                catch (Exception exception)
                {
                    Revert(k, exception.Message);
                    return;
                }
            }
            JoinedFormat = lastFormat;


            if (!radioButtonJoinAddTime.Checked)
            {
                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)
                        {
                            var 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 (var 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(LanguageSettings.Current.JoinSubtitles.TotalNumberOfLinesX, JoinedSubtitle.Paragraphs.Count);
        }
Пример #5
0
        private void importTimeCodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_subtitle.Paragraphs.Count < 1)
            {
                return;
            }

            openFileDialog1.Title    = Configuration.Settings.Language.General.OpenSubtitle;
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.Filter   = UiUtil.SubtitleExtensionFilter.Value;
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                var            timeCodeSubtitle = new Subtitle();
                SubtitleFormat format           = null;

                if (openFileDialog1.FileName.EndsWith(".sup", StringComparison.OrdinalIgnoreCase) &&
                    FileUtil.IsBluRaySup(openFileDialog1.FileName))
                {
                    var log       = new StringBuilder();
                    var subtitles = BluRaySupParser.ParseBluRaySup(openFileDialog1.FileName, log);
                    if (subtitles.Count > 0)
                    {
                        foreach (var sup in subtitles)
                        {
                            timeCodeSubtitle.Paragraphs.Add(new Paragraph(sup.StartTimeCode, sup.EndTimeCode, string.Empty));
                        }

                        format = new SubRip(); // just to set format to something
                    }
                }

                if (format == null)
                {
                    format = timeCodeSubtitle.LoadSubtitle(openFileDialog1.FileName, out var encoding, null);
                }

                if (format == null)
                {
                    var formats = SubtitleFormat.GetBinaryFormats(true).Union(SubtitleFormat.GetTextOtherFormats()).Union(new SubtitleFormat[]
                    {
                        new TimeCodesOnly1(),
                        new TimeCodesOnly2()
                    }).ToArray();
                    format = SubtitleFormat.LoadSubtitleFromFile(formats, openFileDialog1.FileName, timeCodeSubtitle);
                }

                if (format == null)
                {
                    return;
                }

                if (timeCodeSubtitle.Paragraphs.Count != _subtitle.Paragraphs.Count)
                {
                    var text = string.Format(Configuration.Settings.Language.Main.ImportTimeCodesDifferentNumberOfLinesWarning, timeCodeSubtitle.Paragraphs.Count, _subtitle.Paragraphs.Count);
                    if (MessageBox.Show(this, text, Text, MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
                    {
                        return;
                    }
                }

                int count = 0;
                for (int i = 0; i < timeCodeSubtitle.Paragraphs.Count; i++)
                {
                    var existing = _subtitle.GetParagraphOrDefault(i);

                    var newTimeCode = timeCodeSubtitle.GetParagraphOrDefault(i);
                    if (existing == null || newTimeCode == null)
                    {
                        break;
                    }

                    existing.StartTime.TotalMilliseconds = newTimeCode.StartTime.TotalMilliseconds;
                    existing.EndTime.TotalMilliseconds   = newTimeCode.EndTime.TotalMilliseconds;
                    count++;
                }

                MessageBox.Show(string.Format(Configuration.Settings.Language.Main.TimeCodeImportedFromXY, Path.GetFileName(openFileDialog1.FileName), count));
                var idx = subtitleListView1.SelectedItems[0].Index;
                subtitleListView1.Fill(_subtitle);
                subtitleListView1.SelectIndexAndEnsureVisible(_subtitle.GetParagraphOrDefault(idx));
            }
        }