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); } FFMpegProcess.Close(); ffmpegProgress.Complete(); }
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.WindowStyle = ProcessWindowStyle.Hidden; 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); } FFMpegProcess.Close(); 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); } } }
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.WindowStyle = ProcessWindowStyle.Hidden; 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); } FFMpegProcess.Close(); FFMpegProcess = null; ffmpegProgress.Complete(); } catch (Exception) { EnsureFFMpegProcessStopped(); throw; } }