internal void ConvertMedia(Media input, Media output, ConvertSettings settings) { this.EnsureFFMpegLibs(); string filename = input.Filename; if (filename == null) { filename = Path.GetTempFileName(); using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { this.CopyStream(input.DataStream, stream, 0x40000); } } string path = output.Filename; if (path == null) { path = Path.GetTempFileName(); } if (((output.Format == "flv") || (Path.GetExtension(path).ToLower() == ".flv")) && !settings.AudioSampleRate.HasValue) { settings.AudioSampleRate = 0xac44; } try { string fFMpegExePath = this.GetFFMpegExePath(); if (!File.Exists(fFMpegExePath)) { throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath); } string arguments = this.ComposeFFMpegCommandLineArgs(filename, input.Format, path, output.Format, settings); ProcessStartInfo startInfo = new ProcessStartInfo(fFMpegExePath, arguments) { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = Path.GetDirectoryName(this.FFMpegToolPath), RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true }; this.InitStartInfo(startInfo); if (this.FFMpegProcess != null) { throw new InvalidOperationException("FFMpeg process is already started"); } this.FFMpegProcess = Process.Start(startInfo); if (this.FFMpegProcessPriority != ProcessPriorityClass.Normal) { this.FFMpegProcess.PriorityClass = this.FFMpegProcessPriority; } string lastErrorLine = string.Empty; FFMpegProgress ffmpegProgress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null); if (settings != null) { ffmpegProgress.Seek = settings.Seek; ffmpegProgress.MaxDuration = settings.MaxDuration; } this.FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args) { if (args.Data != null) { lastErrorLine = args.Data; ffmpegProgress.ParseLine(args.Data); this.FFMpegLogHandler(args.Data); } }; this.FFMpegProcess.OutputDataReceived += delegate(object o, DataReceivedEventArgs args) { }; this.FFMpegProcess.BeginOutputReadLine(); this.FFMpegProcess.BeginErrorReadLine(); this.WaitFFMpegProcessForExit(); if (this.FFMpegProcess.ExitCode != 0) { throw new FFMpegException(this.FFMpegProcess.ExitCode, lastErrorLine); } this.FFMpegProcess.Close(); this.FFMpegProcess = null; ffmpegProgress.Complete(); if (output.Filename == null) { using (FileStream stream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)) { this.CopyStream(stream2, output.DataStream, 0x40000); } } } catch (Exception) { this.EnsureFFMpegProcessStopped(); throw; } finally { if (((filename != null) && (input.Filename == null)) && File.Exists(filename)) { File.Delete(filename); } if (((path != null) && (output.Filename == null)) && File.Exists(path)) { File.Delete(path); } } }
public void ConcatMedia(string[] inputFiles, string outputFile, string outputFormat, ConcatSettings settings) { this.EnsureFFMpegLibs(); string fFMpegExePath = this.GetFFMpegExePath(); if (!File.Exists(fFMpegExePath)) { throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath); } StringBuilder builder = new StringBuilder(); foreach (string str2 in inputFiles) { if (!File.Exists(str2)) { throw new FileNotFoundException("Cannot find input video file: " + str2); } builder.AppendFormat(" -i {0} ", this.CommandArgParameter(str2)); } StringBuilder outputArgs = new StringBuilder(); this.ComposeFFMpegOutputArgs(outputArgs, outputFormat, settings); outputArgs.Append(" -filter_complex \""); outputArgs.AppendFormat("concat=n={0}", inputFiles.Length); if (settings.ConcatVideoStream) { outputArgs.Append(":v=1"); } if (settings.ConcatAudioStream) { outputArgs.Append(":a=1"); } if (settings.ConcatVideoStream) { outputArgs.Append(" [v]"); } if (settings.ConcatAudioStream) { outputArgs.Append(" [a]"); } outputArgs.Append("\" "); if (settings.ConcatVideoStream) { outputArgs.Append(" -map \"[v]\" "); } if (settings.ConcatAudioStream) { outputArgs.Append(" -map \"[a]\" "); } string arguments = string.Format("-y -loglevel {3} {0} {1} {2}", new object[] { builder.ToString(), outputArgs, this.CommandArgParameter(outputFile), this.LogLevel }); try { ProcessStartInfo startInfo = new ProcessStartInfo(fFMpegExePath, arguments) { WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = Path.GetDirectoryName(this.FFMpegToolPath), RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true }; this.InitStartInfo(startInfo); if (this.FFMpegProcess != null) { throw new InvalidOperationException("FFMpeg process is already started"); } this.FFMpegProcess = Process.Start(startInfo); if (this.FFMpegProcessPriority != ProcessPriorityClass.Normal) { this.FFMpegProcess.PriorityClass = this.FFMpegProcessPriority; } string lastErrorLine = string.Empty; FFMpegProgress ffmpegProgress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null); if (settings != null) { ffmpegProgress.MaxDuration = settings.MaxDuration; } this.FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args) { if (args.Data != null) { lastErrorLine = args.Data; ffmpegProgress.ParseLine(args.Data); this.FFMpegLogHandler(args.Data); } }; this.FFMpegProcess.OutputDataReceived += delegate(object o, DataReceivedEventArgs args) { }; this.FFMpegProcess.BeginOutputReadLine(); this.FFMpegProcess.BeginErrorReadLine(); this.WaitFFMpegProcessForExit(); if (this.FFMpegProcess.ExitCode != 0) { throw new FFMpegException(this.FFMpegProcess.ExitCode, lastErrorLine); } this.FFMpegProcess.Close(); this.FFMpegProcess = null; ffmpegProgress.Complete(); } catch (Exception) { this.EnsureFFMpegProcessStopped(); throw; } }