示例#1
0
        private void AppendTextVisuallyToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (!IsSubtitleLoaded)
            {
                DisplaySubtitleNotLoadedMessage();
                return;
            }

            ReloadFromSourceView();

            if (MessageBox.Show(_language.SubtitleAppendPrompt, _language.SubtitleAppendPromptTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                openFileDialog1.Title = _language.OpenSubtitleToAppend;
                openFileDialog1.FileName = string.Empty;
                openFileDialog1.Filter = Utilities.GetOpenDialogFilter();
                if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    bool success = false;
                    string fileName = openFileDialog1.FileName;
                    if (File.Exists(fileName))
                    {
                        var subtitleToAppend = new Subtitle();
                        SubtitleFormat format = null;

                        // do not allow blu-ray/vobsub
                        string extension = Path.GetExtension(fileName).ToLowerInvariant();
                        if (extension == ".sub" && (IsVobSubFile(fileName, false) || FileUtil.IsSpDvdSup(fileName)))
                        {
                            format = null;
                        }
                        else if (extension == ".sup" && FileUtil.IsBluRaySup(fileName))
                        {
                            format = null;
                        }
                        else
                        {
                            Encoding encoding;
                            format = subtitleToAppend.LoadSubtitle(fileName, out encoding, null);
                            if (GetCurrentSubtitleFormat().IsFrameBased)
                                subtitleToAppend.CalculateTimeCodesFromFrameNumbers(CurrentFrameRate);
                            else
                                subtitleToAppend.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate);
                        }

                        if (format != null && subtitleToAppend.Paragraphs.Count > 1)
                        {
                            using (var visualSync = new VisualSync())
                            {
                                visualSync.Initialize(toolStripButtonVisualSync.Image as Bitmap, subtitleToAppend, _fileName, _language.AppendViaVisualSyncTitle, CurrentFrameRate);
                                visualSync.ShowDialog(this);
                                if (visualSync.OkPressed)
                                {
                                    if (MessageBox.Show(_language.AppendSynchronizedSubtitlePrompt, _language.SubtitleAppendPromptTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        int start = _subtitle.Paragraphs.Count + 1;
                                        var fr = CurrentFrameRate;
                                        MakeHistoryForUndo(_language.BeforeAppend);
                                        foreach (var p in visualSync.Paragraphs)
                                        {
                                            if (format.IsFrameBased)
                                                p.CalculateFrameNumbersFromTimeCodes(fr);
                                            _subtitle.Paragraphs.Add(new Paragraph(p));
                                        }
                                        if (format.GetType() == typeof(AdvancedSubStationAlpha) && GetCurrentSubtitleFormat().GetType() == typeof(AdvancedSubStationAlpha))
                                        {
                                            var currentStyles = new List<string>();
                                            if (_subtitle.Header != null)
                                                currentStyles = AdvancedSubStationAlpha.GetStylesFromHeader(_subtitle.Header);
                                            foreach (var styleName in AdvancedSubStationAlpha.GetStylesFromHeader(subtitleToAppend.Header))
                                            {
                                                bool alreadyExists = false;
                                                foreach (var currentStyleName in currentStyles)
                                                {
                                                    if (currentStyleName.Trim().Equals(styleName.Trim(), StringComparison.OrdinalIgnoreCase))
                                                        alreadyExists = true;
                                                }
                                                if (!alreadyExists)
                                                {
                                                    var newStyle = AdvancedSubStationAlpha.GetSsaStyle(styleName, subtitleToAppend.Header);
                                                    _subtitle.Header = AdvancedSubStationAlpha.AddSsaStyle(newStyle, _subtitle.Header);
                                                }
                                            }
                                        }

                                        _subtitle.Renumber();

                                        ShowSource();
                                        SubtitleListview1.Fill(_subtitle, _subtitleAlternate);

                                        // select appended lines
                                        for (int i = start; i < _subtitle.Paragraphs.Count; i++)
                                            SubtitleListview1.Items[i].Selected = true;
                                        SubtitleListview1.EnsureVisible(start);

                                        ShowStatus(string.Format(_language.SubtitleAppendedX, fileName));
                                        success = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!success)
                        ShowStatus(_language.SubtitleNotAppended);
                }
            }
        }
示例#2
0
        private void ShowVisualSync(bool onlySelectedLines)
        {
            if (!IsSubtitleLoaded)
            {
                DisplaySubtitleNotLoadedMessage();
                return;
            }

            using (var visualSync = new VisualSync())
            {
                visualSync.VideoFileName = _videoFileName;
                visualSync.AudioTrackNumber = _videoAudioTrackNumber;

                SaveSubtitleListviewIndices();
                if (onlySelectedLines)
                {
                    var selectedLines = new Subtitle { WasLoadedWithFrameNumbers = _subtitle.WasLoadedWithFrameNumbers };
                    foreach (int index in SubtitleListview1.SelectedIndices)
                        selectedLines.Paragraphs.Add(_subtitle.Paragraphs[index]);
                    visualSync.Initialize(toolStripButtonVisualSync.Image as Bitmap, selectedLines, _fileName, _language.VisualSyncSelectedLines, CurrentFrameRate);
                }
                else
                {
                    visualSync.Initialize(toolStripButtonVisualSync.Image as Bitmap, _subtitle, _fileName, _language.VisualSyncTitle, CurrentFrameRate);
                }

                _endSeconds = -1;
                mediaPlayer.Pause();
                if (visualSync.ShowDialog(this) == DialogResult.OK)
                {
                    MakeHistoryForUndo(_language.BeforeVisualSync);

                    if (onlySelectedLines)
                    { // we only update selected lines
                        int i = 0;
                        foreach (int index in SubtitleListview1.SelectedIndices)
                        {
                            _subtitle.Paragraphs[index] = visualSync.Paragraphs[i];
                            i++;
                        }
                        ShowStatus(_language.VisualSyncPerformedOnSelectedLines);
                    }
                    else
                    {
                        _subtitle.Paragraphs.Clear();
                        foreach (var p in visualSync.Paragraphs)
                            _subtitle.Paragraphs.Add(new Paragraph(p));
                        ShowStatus(_language.VisualSyncPerformed);
                    }
                    if (visualSync.FrameRateChanged)
                        toolStripComboBoxFrameRate.Text = string.Format("{0:0.###}", visualSync.FrameRate);
                    if (IsFramesRelevant && CurrentFrameRate > 0)
                    {
                        _subtitle.CalculateFrameNumbersFromTimeCodesNoCheck(CurrentFrameRate);
                        if (tabControlSubtitle.SelectedIndex == TabControlSourceView)
                            ShowSource();
                    }
                    ShowSource();
                    SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
                    RestoreSubtitleListviewIndices();
                    if (onlySelectedLines && SubtitleListview1.SelectedItems.Count > 0)
                    {
                        SubtitleListview1.EnsureVisible(SubtitleListview1.SelectedItems[SubtitleListview1.SelectedItems.Count - 1].Index);
                    }
                }
                _videoFileName = visualSync.VideoFileName;
            }
        }