Exemplo n.º 1
0
        private void SetPosition_Shown(object sender, EventArgs e)
        {
            ShowStyleAlignment();
            ShowCurrentPosition();

            var playResX = AdvancedSubStationAlpha.GetTagFromHeader("PlayResX", "[Script Info]", _subtitleWithNewHeader.Header);
            var playResY = AdvancedSubStationAlpha.GetTagFromHeader("PlayResY", "[Script Info]", _subtitleWithNewHeader.Header);

            if (string.IsNullOrEmpty(playResX) || string.IsNullOrEmpty(playResY))
            {
                var dialogResult = MessageBox.Show(LanguageSettings.Current.AssaSetPosition.ResolutionMissing, "Subtitle Edit", MessageBoxButtons.YesNoCancel);
                if (dialogResult == DialogResult.OK || dialogResult == DialogResult.Yes)
                {
                    if (string.IsNullOrEmpty(_subtitleWithNewHeader.Header))
                    {
                        _subtitleWithNewHeader.Header = AdvancedSubStationAlpha.DefaultHeader;
                    }

                    _subtitleWithNewHeader.Header = AdvancedSubStationAlpha.AddTagToHeader("PlayResX", "PlayResX: " + _videoInfo.Width.ToString(CultureInfo.InvariantCulture), "[Script Info]", _subtitleWithNewHeader.Header);
                    _subtitleWithNewHeader.Header = AdvancedSubStationAlpha.AddTagToHeader("PlayResY", "PlayResY: " + _videoInfo.Height.ToString(CultureInfo.InvariantCulture), "[Script Info]", _subtitleWithNewHeader.Header);
                }
            }

            GeneratePreviewViaMpv();
            _loading = false;
        }
Exemplo n.º 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++)
            {
                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);
        }