Esempio n. 1
0
 private void buttonDownloadFfmpeg_Click(object sender, EventArgs e)
 {
     using (var form = new DownloadFfmpeg())
     {
         if (form.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(form.FFmpegPath))
         {
             Configuration.Settings.General.FFmpegLocation = form.FFmpegPath;
             buttonDownloadFfmpeg.Visible = false;
             Configuration.Settings.Save();
         }
     }
 }
Esempio n. 2
0
 private void buttonDownloadFfmpeg_Click(object sender, EventArgs e)
 {
     using (var form = new DownloadFfmpeg())
     {
         if (form.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(form.FFmpegPath))
         {
             Configuration.Settings.General.FFmpegLocation = form.FFmpegPath;
             buttonDownloadFfmpeg.Visible   = false;
             buttonImportWithFfmpeg.Enabled = true;
             numericUpDownThreshold.Enabled = true;
         }
     }
 }
Esempio n. 3
0
        private void buttonRipWave_Click(object sender, EventArgs e)
        {
            if (listViewInputFiles.Items.Count == 0)
            {
                MessageBox.Show(LanguageSettings.Current.BatchConvert.NothingToConvert);
                return;
            }

            _converting                = true;
            buttonRipWave.Enabled      = false;
            progressBar1.Style         = ProgressBarStyle.Blocks;
            progressBar1.Maximum       = listViewInputFiles.Items.Count;
            progressBar1.Value         = 0;
            progressBar1.Visible       = progressBar1.Maximum > 2;
            buttonInputBrowse.Enabled  = false;
            buttonSearchFolder.Enabled = false;
            _abort = false;
            listViewInputFiles.BeginUpdate();
            foreach (ListViewItem item in listViewInputFiles.Items)
            {
                item.SubItems[3].Text = "-";
            }

            listViewInputFiles.EndUpdate();
            Refresh();
            var index = 0;

            while (index < listViewInputFiles.Items.Count && _abort == false)
            {
                var item = listViewInputFiles.Items[index];

                void UpdateStatus(string status)
                {
                    item.SubItems[3].Text = status;
                    Refresh();
                }

                UpdateStatus(LanguageSettings.Current.AddWaveformBatch.ExtractingAudio);
                var fileName = item.Text;
                try
                {
                    var     targetFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".wav");
                    Process process;
                    try
                    {
                        process        = AddWaveform.GetCommandLineProcess(fileName, -1, targetFile, Configuration.Settings.General.VlcWaveTranscodeSettings, out var encoderName);
                        labelInfo.Text = encoderName;
                    }
                    catch (DllNotFoundException)
                    {
                        var isFfmpegAvailable = !string.IsNullOrEmpty(Configuration.Settings.General.FFmpegLocation) && File.Exists(Configuration.Settings.General.FFmpegLocation);
                        if (isFfmpegAvailable)
                        {
                            Configuration.Settings.General.UseFFmpegForWaveExtraction = true;
                            process        = AddWaveform.GetCommandLineProcess(fileName, -1, targetFile, Configuration.Settings.General.VlcWaveTranscodeSettings, out var encoderName);
                            labelInfo.Text = encoderName;
                        }
                        else
                        {
                            if (MessageBox.Show(LanguageSettings.Current.AddWaveform.FfmpegNotFound, "Subtitle Edit", MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
                            {
                                buttonRipWave.Enabled = true;
                                return;
                            }

                            using (var form = new DownloadFfmpeg())
                            {
                                if (form.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(form.FFmpegPath))
                                {
                                    Configuration.Settings.General.FFmpegLocation             = form.FFmpegPath;
                                    Configuration.Settings.General.UseFFmpegForWaveExtraction = true;
                                    process        = AddWaveform.GetCommandLineProcess(fileName, -1, targetFile, Configuration.Settings.General.VlcWaveTranscodeSettings, out var encoderName);
                                    labelInfo.Text = encoderName;
                                }
                                else
                                {
                                    buttonRipWave.Enabled = true;
                                    return;
                                }
                            }
                        }
                    }

                    process.Start();
                    while (!process.HasExited && !_abort)
                    {
                        Application.DoEvents();
                    }

                    // check for delay in matroska files
                    var audioTrackNames      = new List <string>();
                    var mkvAudioTrackNumbers = new Dictionary <int, int>();
                    if (fileName.ToLowerInvariant().EndsWith(".mkv", StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            using (var matroska = new MatroskaFile(fileName))
                            {
                                if (matroska.IsValid)
                                {
                                    foreach (var track in matroska.GetTracks())
                                    {
                                        if (track.IsAudio)
                                        {
                                            if (track.CodecId != null && track.Language != null)
                                            {
                                                audioTrackNames.Add("#" + track.TrackNumber + ": " + track.CodecId.Replace("\0", string.Empty) + " - " + track.Language.Replace("\0", string.Empty));
                                            }
                                            else
                                            {
                                                audioTrackNames.Add("#" + track.TrackNumber);
                                            }

                                            mkvAudioTrackNumbers.Add(mkvAudioTrackNumbers.Count, track.TrackNumber);
                                        }
                                    }
                                    if (mkvAudioTrackNumbers.Count > 0)
                                    {
                                        _delayInMilliseconds = (int)matroska.GetAudioTrackDelayMilliseconds(mkvAudioTrackNumbers[0]);
                                    }
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            SeLogger.Error(exception, $"Error getting delay from mkv: {fileName}");
                            _delayInMilliseconds = 0;
                        }
                    }

                    UpdateStatus(LanguageSettings.Current.AddWaveformBatch.Calculating);
                    MakeWaveformAndSpectrogram(fileName, targetFile, _delayInMilliseconds);

                    if (checkBoxGenerateSceneChanges.Visible && checkBoxGenerateSceneChanges.Checked)
                    {
                        GenerateSceneChanges(fileName);
                    }

                    // cleanup
                    try
                    {
                        File.Delete(targetFile);
                    }
                    catch
                    {
                        // don't show error about unsuccessful delete
                    }

                    IncrementAndShowProgress();

                    UpdateStatus(LanguageSettings.Current.AddWaveformBatch.Done);
                }
                catch
                {
                    IncrementAndShowProgress();

                    UpdateStatus(LanguageSettings.Current.AddWaveformBatch.Error);
                }
                index++;
            }
            _converting          = false;
            labelProgress.Text   = string.Empty;
            labelInfo.Text       = string.Empty;
            progressBar1.Visible = false;
            TaskbarList.SetProgressState(Owner.Handle, TaskbarButtonProgressFlags.NoProgress);
            buttonRipWave.Enabled      = true;
            buttonInputBrowse.Enabled  = true;
            buttonSearchFolder.Enabled = true;
        }
Esempio n. 4
0
        private void buttonRipWave_Click(object sender, EventArgs e)
        {
            buttonRipWave.Enabled = false;
            _cancel = false;
            string targetFile        = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".wav");
            string targetDriveLetter = null;

            if (Configuration.IsRunningOnWindows)
            {
                var root = Path.GetPathRoot(targetFile);
                if (root.Length > 1 && root[1] == ':')
                {
                    targetDriveLetter = root.Remove(1);
                }
            }

            labelPleaseWait.Visible = true;
            string  encoderName;
            Process process;

            try
            {
                process        = GetCommandLineProcess(SourceVideoFileName, AudioTrackNumber, targetFile, _encodeParameters, out encoderName);
                labelInfo.Text = encoderName;
            }
            catch (DllNotFoundException)
            {
                var isFfmpegAvailable = !string.IsNullOrEmpty(Configuration.Settings.General.FFmpegLocation) && File.Exists(Configuration.Settings.General.FFmpegLocation);
                if (isFfmpegAvailable)
                {
                    Configuration.Settings.General.UseFFmpegForWaveExtraction = true;
                    process        = GetCommandLineProcess(SourceVideoFileName, AudioTrackNumber, targetFile, _encodeParameters, out encoderName);
                    labelInfo.Text = encoderName;
                }
                else
                {
                    if (MessageBox.Show(LanguageSettings.Current.AddWaveform.FfmpegNotFound, "Subtitle Edit", MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
                    {
                        buttonRipWave.Enabled = true;
                        return;
                    }

                    using (var form = new DownloadFfmpeg {
                        AutoClose = true
                    })
                    {
                        if (form.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(form.FFmpegPath))
                        {
                            Configuration.Settings.General.FFmpegLocation             = form.FFmpegPath;
                            Configuration.Settings.General.UseFFmpegForWaveExtraction = true;
                            process = GetCommandLineProcess(SourceVideoFileName, AudioTrackNumber, targetFile, _encodeParameters, out encoderName);
                        }
                        else
                        {
                            buttonRipWave.Enabled = true;
                            return;
                        }
                    }
                }
            }

            process.Start();
            progressBar1.Style   = ProgressBarStyle.Marquee;
            progressBar1.Visible = true;
            double seconds = 0;

            buttonCancel.Visible = true;
            try
            {
                process.PriorityClass = ProcessPriorityClass.Normal;
            }
            catch
            {
                // ignored
            }

            while (!process.HasExited)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
                seconds += 0.1;
                if (seconds < 60)
                {
                    labelProgress.Text = string.Format(LanguageSettings.Current.AddWaveform.ExtractingSeconds, seconds);
                }
                else
                {
                    labelProgress.Text = string.Format(LanguageSettings.Current.AddWaveform.ExtractingMinutes, (int)(seconds / 60), (int)(seconds % 60));
                }

                Refresh();
                if (_cancel)
                {
                    process.Kill();
                    progressBar1.Visible    = false;
                    labelPleaseWait.Visible = false;
                    buttonRipWave.Enabled   = true;
                    buttonCancel.Visible    = false;
                    DialogResult            = DialogResult.Cancel;
                    return;
                }

                if (targetDriveLetter != null && seconds > 1 && Convert.ToInt32(seconds) % 10 == 0)
                {
                    try
                    {
                        var drive = new DriveInfo(targetDriveLetter);
                        if (drive.IsReady)
                        {
                            if (drive.AvailableFreeSpace < 50 * 1000000) // 50 mb
                            {
                                labelInfo.ForeColor = Color.Red;
                                labelInfo.Text      = LanguageSettings.Current.AddWaveform.LowDiskSpace;
                            }
                            else if (labelInfo.ForeColor == Color.Red)
                            {
                                labelInfo.Text = string.Format(LanguageSettings.Current.AddWaveform.FreeDiskSpace, Utilities.FormatBytesToDisplayFileSize(drive.AvailableFreeSpace));
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            buttonCancel.Visible = false;
            progressBar1.Visible = false;
            progressBar1.Style   = ProgressBarStyle.Blocks;
            process.Dispose();

            var targetFileInfo = new FileInfo(targetFile);

            if (!targetFileInfo.Exists)
            {
                if (_encodeParameters != RetryEncodeParameters)
                {
                    _encodeParameters = RetryEncodeParameters;
                    buttonRipWave_Click(null, null);
                    return;
                }

                if (_numberOfAudioTracks == 0)
                {
                    if (MessageBox.Show(LanguageSettings.Current.AddWaveform.NoAudioTracksFoundGenerateEmptyWaveform, LanguageSettings.Current.General.Title, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                    {
                        MakeEmptyWaveFile();
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        DialogResult = DialogResult.Cancel;
                    }

                    return;
                }

                MessageBox.Show(string.Format(LanguageSettings.Current.AddWaveform.WaveFileNotFound, IntPtr.Size * 8, process.StartInfo.FileName, process.StartInfo.Arguments));

                labelPleaseWait.Visible = false;
                labelProgress.Text      = string.Empty;
                buttonRipWave.Enabled   = true;
                return;
            }

            if (targetFileInfo.Length <= 200)
            {
                if (_numberOfAudioTracks == 0)
                {
                    if (MessageBox.Show(LanguageSettings.Current.AddWaveform.NoAudioTracksFoundGenerateEmptyWaveform, LanguageSettings.Current.General.Title, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                    {
                        MakeEmptyWaveFile();
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        DialogResult = DialogResult.Cancel;
                    }

                    return;
                }

                MessageBox.Show(string.Format(LanguageSettings.Current.AddWaveform.WaveFileMalformed, encoderName, process.StartInfo.FileName, process.StartInfo.Arguments));

                labelPleaseWait.Visible = false;
                labelProgress.Text      = string.Empty;
                buttonRipWave.Enabled   = true;
                return;
            }

            try
            {
                ReadWaveFile(targetFile, _delayInMilliseconds);
            }
            catch
            {
                // retry - see https://github.com/SubtitleEdit/subtitleedit/issues/6003
                System.Threading.Thread.Sleep(5000);
                Application.DoEvents();
                ReadWaveFile(targetFile, _delayInMilliseconds);
            }

            labelProgress.Text = string.Empty;
            File.Delete(targetFile);
            DialogResult = DialogResult.OK;
        }