Esempio n. 1
0
 public bool Stop()
 {
     if (FFMpegProcess != null && !FFMpegProcess.HasExited && FFMpegProcess.StartInfo.RedirectStandardInput)
     {
         FFMpegProcess.StandardInput.WriteLine("q\n");
         NetStandardCompatibility.Close(FFMpegProcess.StandardInput);
         WaitFFMpegProcessForExit();
         return(true);
     }
     return(false);
 }
Esempio n. 2
0
 public void Invoke(string ffmpegArgs)
 {
     EnsureFFMpegLibs();
     //License.L.Check();
     try
     {
         string fFMpegExePath = GetFFMpegExePath();
         if (!File.Exists(fFMpegExePath))
         {
             throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath);
         }
         ProcessStartInfo processStartInfo = new ProcessStartInfo(fFMpegExePath, ffmpegArgs);
         processStartInfo.CreateNoWindow         = true;
         processStartInfo.UseShellExecute        = false;
         processStartInfo.WorkingDirectory       = Path.GetDirectoryName(FFMpegToolPath);
         processStartInfo.RedirectStandardInput  = true;
         processStartInfo.RedirectStandardOutput = false;
         processStartInfo.RedirectStandardError  = true;
         InitStartInfo(processStartInfo);
         if (FFMpegProcess != null)
         {
             throw new InvalidOperationException("FFMpeg process is already started");
         }
         FFMpegProcess = Process.Start(processStartInfo);
         if (FFMpegProcessPriority != ProcessPriorityClass.Normal)
         {
             FFMpegProcess.PriorityClass = FFMpegProcessPriority;
         }
         string lastErrorLine = string.Empty;
         FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args)
         {
             if (args.Data != null)
             {
                 lastErrorLine = args.Data;
                 FFMpegLogHandler(args.Data);
             }
         };
         FFMpegProcess.BeginErrorReadLine();
         WaitFFMpegProcessForExit();
         if (FFMpegProcess.ExitCode != 0)
         {
             throw new FFMpegException(FFMpegProcess.ExitCode, lastErrorLine);
         }
         NetStandardCompatibility.Close(FFMpegProcess);
         FFMpegProcess = null;
     }
     catch (Exception)
     {
         EnsureFFMpegProcessStopped();
         throw;
     }
 }
Esempio n. 3
0
        protected void CopyToStdIn()
        {
            byte[]  array             = new byte[65536];
            Thread  copyToStdInThread = CopyToStdInThread;
            Process fFMpegProcess     = FFMpegProcess;
            Stream  baseStream        = FFMpegProcess.StandardInput.BaseStream;

            while (true)
            {
                int num;
                try
                {
                    num = Input.Read(array, 0, array.Length);
                }
                catch (Exception ex)
                {
                    OnStreamError(ex, isStdinStdout: false);
                    return;
                }
                if (num <= 0)
                {
                    break;
                }
                if (FFMpegProcess == null || copyToStdInThread != CopyToStdInThread || fFMpegProcess != FFMpegProcess)
                {
                    return;
                }
                try
                {
                    baseStream.Write(array, 0, num);
                    baseStream.Flush();
                }
                catch (Exception ex2)
                {
                    OnStreamError(ex2, isStdinStdout: true);
                    return;
                }
            }
            NetStandardCompatibility.Close(FFMpegProcess.StandardInput);
        }
Esempio n. 4
0
 public void Wait()
 {
     FFMpegProcess.WaitForExit(int.MaxValue);
     if (CopyToStdInThread != null)
     {
         CopyToStdInThread = null;
     }
     if (CopyFromStdOutThread != null)
     {
         CopyFromStdOutThread = null;
     }
     if (FFMpegProcess.ExitCode != 0)
     {
         throw new FFMpegException(FFMpegProcess.ExitCode, lastErrorLine ?? "Unknown error");
     }
     if (lastStreamException != null)
     {
         throw new IOException(lastStreamException.Message, lastStreamException);
     }
     NetStandardCompatibility.Close(FFMpegProcess);
     ffmpegProgress.Complete();
 }
Esempio n. 5
0
 public void Stop(bool forceFFMpegQuit)
 {
     if (CopyToStdInThread != null)
     {
         CopyToStdInThread = null;
     }
     if (forceFFMpegQuit)
     {
         if (Input == null && WriteBytesCount == 0L)
         {
             FFMpegProcess.StandardInput.WriteLine("q\n");
             NetStandardCompatibility.Close(FFMpegProcess.StandardInput);
         }
         else
         {
             Abort();
         }
     }
     else
     {
         NetStandardCompatibility.Close(FFMpegProcess.StandardInput.BaseStream);
     }
     Wait();
 }
Esempio n. 6
0
        internal void ConvertMedia(Media input, Media output, ConvertSettings settings)
        {
            EnsureFFMpegLibs();
            //License.L.Check();
            string text = input.Filename;

            if (text == null)
            {
                text = Path.GetTempFileName();
                using (FileStream outputStream = new FileStream(text, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    CopyStream(input.DataStream, outputStream, 262144);
                }
            }
            string text2 = output.Filename;

            if (text2 == null)
            {
                text2 = Path.GetTempFileName();
            }
            if ((output.Format == "flv" || Path.GetExtension(text2).ToLower() == ".flv") && !settings.AudioSampleRate.HasValue)
            {
                settings.AudioSampleRate = 44100;
            }
            try
            {
                string fFMpegExePath = GetFFMpegExePath();
                if (!File.Exists(fFMpegExePath))
                {
                    throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath);
                }
                string           arguments        = ComposeFFMpegCommandLineArgs(text, input.Format, text2, output.Format, settings);
                ProcessStartInfo processStartInfo = new ProcessStartInfo(fFMpegExePath, arguments);
                processStartInfo.CreateNoWindow         = true;
                processStartInfo.UseShellExecute        = false;
                processStartInfo.WorkingDirectory       = Path.GetDirectoryName(FFMpegToolPath);
                processStartInfo.RedirectStandardInput  = true;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.RedirectStandardError  = true;
                InitStartInfo(processStartInfo);
                if (FFMpegProcess != null)
                {
                    throw new InvalidOperationException("FFMpeg process is already started");
                }
                FFMpegProcess = Process.Start(processStartInfo);
                if (FFMpegProcessPriority != ProcessPriorityClass.Normal)
                {
                    FFMpegProcess.PriorityClass = FFMpegProcessPriority;
                }
                string         lastErrorLine  = string.Empty;
                FFMpegProgress ffmpegProgress = new FFMpegProgress(OnConvertProgress, this.ConvertProgress != null);
                if (settings != null)
                {
                    ffmpegProgress.Seek        = settings.Seek;
                    ffmpegProgress.MaxDuration = settings.MaxDuration;
                }
                FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args)
                {
                    if (args.Data != null)
                    {
                        lastErrorLine = args.Data;
                        ffmpegProgress.ParseLine(args.Data);
                        FFMpegLogHandler(args.Data);
                    }
                };
                FFMpegProcess.OutputDataReceived += delegate
                {
                };
                FFMpegProcess.BeginOutputReadLine();
                FFMpegProcess.BeginErrorReadLine();
                WaitFFMpegProcessForExit();
                if (FFMpegProcess.ExitCode != 0)
                {
                    throw new FFMpegException(FFMpegProcess.ExitCode, lastErrorLine);
                }
                NetStandardCompatibility.Close(FFMpegProcess);
                FFMpegProcess = null;
                ffmpegProgress.Complete();
                if (output.Filename == null)
                {
                    using (FileStream inputStream = new FileStream(text2, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        CopyStream(inputStream, output.DataStream, 262144);
                    }
                }
            }
            catch (Exception)
            {
                EnsureFFMpegProcessStopped();
                throw;
            }
            finally
            {
                if (text != null && input.Filename == null && File.Exists(text))
                {
                    File.Delete(text);
                }
                if (text2 != null && output.Filename == null && File.Exists(text2))
                {
                    File.Delete(text2);
                }
            }
        }
Esempio n. 7
0
        public void ConcatMedia(string[] inputFiles, string outputFile, string outputFormat, ConcatSettings settings)
        {
            EnsureFFMpegLibs();
            string fFMpegExePath = GetFFMpegExePath();

            //License.L.Check();
            if (!File.Exists(fFMpegExePath))
            {
                throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath);
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (string text in inputFiles)
            {
                if (!File.Exists(text))
                {
                    throw new FileNotFoundException("Cannot find input video file: " + text);
                }
                stringBuilder.AppendFormat(" -i {0} ", CommandArgParameter(text));
            }
            StringBuilder stringBuilder2 = new StringBuilder();

            ComposeFFMpegOutputArgs(stringBuilder2, outputFormat, settings);
            stringBuilder2.Append(" -filter_complex \"");
            stringBuilder2.AppendFormat("concat=n={0}", inputFiles.Length);
            if (settings.ConcatVideoStream)
            {
                stringBuilder2.Append(":v=1");
            }
            if (settings.ConcatAudioStream)
            {
                stringBuilder2.Append(":a=1");
            }
            if (settings.ConcatVideoStream)
            {
                stringBuilder2.Append(" [v]");
            }
            if (settings.ConcatAudioStream)
            {
                stringBuilder2.Append(" [a]");
            }
            stringBuilder2.Append("\" ");
            if (settings.ConcatVideoStream)
            {
                stringBuilder2.Append(" -map \"[v]\" ");
            }
            if (settings.ConcatAudioStream)
            {
                stringBuilder2.Append(" -map \"[a]\" ");
            }
            string arguments = string.Format("-y -loglevel {3} {0} {1} {2}", stringBuilder.ToString(), stringBuilder2, CommandArgParameter(outputFile), LogLevel);

            try
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo(fFMpegExePath, arguments);
                processStartInfo.UseShellExecute        = false;
                processStartInfo.CreateNoWindow         = true;
                processStartInfo.WorkingDirectory       = Path.GetDirectoryName(FFMpegToolPath);
                processStartInfo.RedirectStandardInput  = true;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.RedirectStandardError  = true;
                InitStartInfo(processStartInfo);
                if (FFMpegProcess != null)
                {
                    throw new InvalidOperationException("FFMpeg process is already started");
                }
                FFMpegProcess = Process.Start(processStartInfo);
                if (FFMpegProcessPriority != ProcessPriorityClass.Normal)
                {
                    FFMpegProcess.PriorityClass = FFMpegProcessPriority;
                }
                string         lastErrorLine  = string.Empty;
                FFMpegProgress ffmpegProgress = new FFMpegProgress(OnConvertProgress, this.ConvertProgress != null);
                if (settings != null)
                {
                    ffmpegProgress.MaxDuration = settings.MaxDuration;
                }
                FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args)
                {
                    if (args.Data != null)
                    {
                        lastErrorLine = args.Data;
                        ffmpegProgress.ParseLine(args.Data);
                        FFMpegLogHandler(args.Data);
                    }
                };
                FFMpegProcess.OutputDataReceived += delegate
                {
                };
                FFMpegProcess.BeginOutputReadLine();
                FFMpegProcess.BeginErrorReadLine();
                WaitFFMpegProcessForExit();
                if (FFMpegProcess.ExitCode != 0)
                {
                    throw new FFMpegException(FFMpegProcess.ExitCode, lastErrorLine);
                }
                NetStandardCompatibility.Close(FFMpegProcess);
                FFMpegProcess = null;
                ffmpegProgress.Complete();
            }
            catch (Exception)
            {
                EnsureFFMpegProcessStopped();
                throw;
            }
        }