public virtual void Run()
        {
            try {
                logger.IfInfo("Forking: " + Command + " " + Arguments);

                // Fork the process
                TranscodeProcess = new Process();
                TranscodeProcess.StartInfo.FileName               = Command;
                TranscodeProcess.StartInfo.Arguments              = Arguments;
                TranscodeProcess.StartInfo.UseShellExecute        = false;
                TranscodeProcess.StartInfo.RedirectStandardOutput = true;
                TranscodeProcess.StartInfo.RedirectStandardError  = true;
                TranscodeProcess.Start();

                logger.IfInfo("Waiting for '" + Command + "' to finish...");

                // Block until done
                TranscodeProcess.WaitForExit();

                logger.IfInfo("'" + Command + "' finished (exit: " + TranscodeProcess.ExitCode + ")");
            } catch (Exception e) {
                logger.IfInfo("\t" + "Failed to start transcode process " + e);

                // Set the state
                State = TranscodeState.Failed;

                // Inform the delegate
                if ((object)TranscoderDelegate != null)
                {
                    TranscoderDelegate.TranscodeFailed(this);
                }

                return;
            }

            if (TranscodeProcess != null)
            {
                // Check for success
                if (TranscodeProcess.ExitCode == 0)
                {
                    State = TranscodeState.Finished;

                    if ((object)TranscoderDelegate != null)
                    {
                        TranscoderDelegate.TranscodeFinished(this);
                    }
                }
                else
                {
                    State = TranscodeState.Failed;

                    if ((object)TranscoderDelegate != null)
                    {
                        TranscoderDelegate.TranscodeFailed(this);
                    }
                }
            }
        }
        public void CancelTranscode()
        {
            if (TranscodeProcess != null)
            {
                logger.IfInfo("Cancelling transcode for " + Item.FileName);

                // Kill the process
                TranscodeProcess.Kill();
                TranscodeProcess = null;

                // Wait for the thread to die
                TranscodeThread.Join();
                TranscodeThread = null;

                // Set the state
                State = TranscodeState.Canceled;

                // Inform the delegate
                if ((object)TranscoderDelegate != null)
                {
                    TranscoderDelegate.TranscodeFailed(this);
                }
            }
        }
Example #3
0
        public override void Run()
        {
            try {
                string ffmpegArguments = "-loglevel quiet -i \"" + Item.FilePath() + "\" -f wav -";
                logger.IfInfo("Forking the process");
                logger.IfInfo("ffmpeg " + ffmpegArguments);

                // Create the ffmpeg process
                FfmpegProcess = new Process();
                FfmpegProcess.StartInfo.FileName               = "ffmpeg";
                FfmpegProcess.StartInfo.Arguments              = ffmpegArguments;
                FfmpegProcess.StartInfo.UseShellExecute        = false;
                FfmpegProcess.StartInfo.RedirectStandardOutput = true;
                FfmpegProcess.StartInfo.RedirectStandardError  = true;

                // Create the opusenc object
                logger.IfInfo("opusenc " + Arguments);
                TranscodeProcess = new Process();
                TranscodeProcess.StartInfo.FileName               = "opusenc";
                TranscodeProcess.StartInfo.Arguments              = Arguments;
                TranscodeProcess.StartInfo.UseShellExecute        = false;
                TranscodeProcess.StartInfo.RedirectStandardInput  = true;
                TranscodeProcess.StartInfo.RedirectStandardOutput = true;
                TranscodeProcess.StartInfo.RedirectStandardError  = false;

                var buffer = new byte[8192];
                FfmpegProcess.Start();
                TranscodeProcess.Start();

                var input        = new BinaryWriter(TranscodeProcess.StandardInput.BaseStream);
                int totalWritten = 0;

                while (true)
                {
                    int bytesRead = FfmpegProcess.StandardOutput.BaseStream.Read(buffer, 0, 8192);
                    totalWritten += bytesRead;
                    if (bytesRead > 0)
                    {
                        input.Write(buffer, 0, bytesRead);
                    }

                    if (bytesRead == 0 && FfmpegProcess.HasExited)
                    {
                        input.Close();
                        FfmpegProcess.Close();
                        break;
                    }
                }

                logger.IfInfo("Waiting for processes to finish");

                // Block until done
                TranscodeProcess.WaitForExit();

                logger.IfInfo("Process finished");
            } catch (Exception e) {
                logger.IfInfo("\t" + "Failed to start transcode process " + e);

                try {
                    TranscodeProcess.Kill();
                    TranscodeProcess.Close();
                } catch { /* do nothing if killing the process fails */ }

                try {
                    FfmpegProcess.Kill();
                    FfmpegProcess.Close();
                } catch { /* do nothing if killing the process fails */ }

                // Set the state
                State = TranscodeState.Failed;

                // Inform the delegate
                if ((object)TranscoderDelegate != null)
                {
                    TranscoderDelegate.TranscodeFailed(this);
                }

                return;
            }

            if (TranscodeProcess != null)
            {
                int exitValue = TranscodeProcess.ExitCode;
                logger.IfInfo("Exit value " + exitValue);

                if (exitValue == 0)
                {
                    State = TranscodeState.Finished;

                    if ((object)TranscoderDelegate != null)
                    {
                        TranscoderDelegate.TranscodeFinished(this);
                    }
                }
                else
                {
                    State = TranscodeState.Failed;

                    if ((object)TranscoderDelegate != null)
                    {
                        TranscoderDelegate.TranscodeFailed(this);
                    }
                }
            }
        }