Exemple #1
0
        private void resetTranscoderFile(TranscoderFile file)
        {
            if (InvokeRequired)
            {
                Invoke(new ResetTranscoderFileCallback(resetTranscoderFile), file);
                return;
            }

            var index = TranscoderFiles.IndexOf(file);

            TranscoderFiles.ResetItem(index);
        }
Exemple #2
0
        void AddFiles(IEnumerable <TranscoderFile> files)
        {
            if (InvokeRequired)
            {
                Invoke(new AddFilesCallback(AddFiles), files);
                return;
            }

            foreach (var file in files)
            {
                TranscoderFiles.Add(file);
            }
        }
Exemple #3
0
        async void goButton_Click(object sender, EventArgs e)
        {
            if (Running)
            {
                if (TokenSource.IsCancellationRequested)
                {
                    return;
                }

                TokenSource.Token.Register(() => {
                    setRunning(false);
                });

                TokenSource.Cancel();
                return;
            }

            if (String.IsNullOrWhiteSpace(outputTextbox.Text))
            {
                MessageBox.Show("You must set the Output folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (bitrateNumericUpDown.Value < 64 || bitrateNumericUpDown.Value > 320)
            {
                MessageBox.Show("You must set a bitrate from 64-320.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            for (int i = 0; i < TranscoderFiles.Count; i++)
            {
                var file = TranscoderFiles[i];
                if (file.Done)
                {
                    file.Done = false;
                    TranscoderFiles.ResetItem(i);
                }
            }

            setRunning(true);

            var bitrate     = Convert.ToInt32(bitrateNumericUpDown.Value);
            var encoderType = encoderComboBox.SelectedItem as TranscoderFile.Type;

            TokenSource = new CancellationTokenSource();

            await Task.Run(async() => {
                int i = 0;
                while (i < TranscoderFiles.Count)
                {
                    var file = TranscoderFiles[i];

                    using (var decoder = new Process())
                        using (var encoder = new Process()) {
                            decoder.StartInfo = new ProcessStartInfo()
                            {
                                FileName               = Path.Combine(Environment.CurrentDirectory, Encoder.FFMPEG.FilePath),
                                Arguments              = String.Format("-i \"{0}\" -vn -f wav -", file.FilePath),
                                WindowStyle            = ProcessWindowStyle.Hidden,
                                CreateNoWindow         = true,
                                UseShellExecute        = false,
                                RedirectStandardInput  = true,
                                RedirectStandardOutput = true,
                                RedirectStandardError  = true
                            };
                            decoder.EnableRaisingEvents = true;
                            decoder.ErrorDataReceived  += delegate(object processSender, DataReceivedEventArgs processEventArgs) {
                                if (processEventArgs.Data != null)
                                {
                                    file.Log.AppendLine(String.Format("{0}", processEventArgs.Data));
                                }
                            };

                            encoder.StartInfo = new ProcessStartInfo()
                            {
                                FileName               = Path.Combine(Environment.CurrentDirectory, encoderType.Encoder.FilePath),
                                Arguments              = file.BuildCommandLineArgs(encoderType, bitrate, outputTextbox.Text),
                                WindowStyle            = ProcessWindowStyle.Hidden,
                                CreateNoWindow         = true,
                                UseShellExecute        = false,
                                RedirectStandardInput  = true,
                                RedirectStandardOutput = true,
                                RedirectStandardError  = true
                            };
                            encoder.EnableRaisingEvents = true;
                            encoder.OutputDataReceived += delegate(object processSender, DataReceivedEventArgs processEventArgs) {
                                file.Log.AppendLine(String.Format("{0}", processEventArgs.Data));
                                updateStatus(processEventArgs.Data);
                            };
                            encoder.ErrorDataReceived += delegate(object processSender, DataReceivedEventArgs processEventArgs) {
                                if (processEventArgs.Data != null && !processEventArgs.Data.StartsWith("["))
                                {
                                    file.Log.AppendLine(String.Format("{0}", processEventArgs.Data));
                                }
                                updateStatus(processEventArgs.Data);
                            };
                            encoder.Exited += delegate(object processSender, EventArgs processEventArgs) {
                                file.Done = true;
                                resetTranscoderFile(file);
                            };

                            selectDataGridViewRow(i);

                            var destinationFolder = file.OutputFolderPath(outputTextbox.Text);

                            if (!Directory.Exists(destinationFolder))
                            {
                                try {
                                    Directory.CreateDirectory(destinationFolder);
                                } catch (Exception ex) {
                                    file.Log.AppendLine(ex.Message);
                                    updateStatus(ex.Message);

                                    if (TokenSource.IsCancellationRequested)
                                    {
                                        updateStatus("Stopped");
                                        return;
                                    }

                                    i++;
                                    continue;
                                }
                            }

                            encoder.Start();
                            encoder.BeginOutputReadLine();
                            encoder.BeginErrorReadLine();

                            if (file.RequiresDecoding)
                            {
                                file.Log.AppendLine(String.Format("{0} {1}", decoder.StartInfo.FileName, decoder.StartInfo.Arguments));
                                file.Log.AppendLine(String.Format("{0} {1}", encoder.StartInfo.FileName, encoder.StartInfo.Arguments));
                                decoder.Start();
                                decoder.BeginErrorReadLine();

                                try {
                                    await decoder.StandardOutput.BaseStream.CopyToAsync(encoder.StandardInput.BaseStream, 4096, TokenSource.Token);
                                    await encoder.StandardInput.BaseStream.FlushAsync(TokenSource.Token);
                                } catch (TaskCanceledException) { }

                                encoder.StandardInput.Close();
                            }
                            else
                            {
                                file.Log.AppendLine(String.Format("{0} {1}", encoder.StartInfo.FileName, encoder.StartInfo.Arguments));
                            }

                            while (!TokenSource.IsCancellationRequested && !encoder.HasExited)
                            {
                                encoder.WaitForExit(500);
                            }

                            if (file.RequiresDecoding && !decoder.HasExited)
                            {
                                decoder.Kill();
                            }

                            if (!encoder.HasExited)
                            {
                                encoder.Kill();
                            }

                            if (encoder.ExitCode == 0 || file.RequiresDecoding)
                            {
                                i++;                         // goto next file
                            }
                            else
                            {
                                file.RequiresDecoding = true;                         // try again with decoding
                            }

                            if (TokenSource.IsCancellationRequested)
                            {
                                updateStatus("Stopped");
                                return;
                            }
                        }
                }

                updateStatus("Ready");
            }, TokenSource.Token);

            setRunning(false);
        }