private ConvertLiveMediaTask CreateLiveMediaTask(string toolArgs, Stream inputStream, Stream outputStream, ConvertSettings settings) { FFMpegProgress progress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null); if (settings != null) { progress.Seek = settings.Seek; progress.MaxDuration = settings.MaxDuration; } return(new ConvertLiveMediaTask(this, toolArgs, inputStream, outputStream, progress)); }
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 ofDir = Path.GetDirectoryName(outputFile); if (ofDir == "\\") { ofDir = ".\\"; } string ifDir = Path.GetDirectoryName(inputFiles[0]); if (ifDir == "\\") { ifDir = ".\\"; } string ifList = ofDir + "\\" + Path.GetFileNameWithoutExtension(outputFile) + "_fcc.txt"; if (File.Exists(ifList)) { File.Delete(ifList); } StreamWriter sw = new StreamWriter(ifList, false, Encoding.GetEncoding(1251)); 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)); sw.WriteLine("file " + str2.Replace("\\", "/")); } sw.Close(); StringBuilder outputArgs = new StringBuilder(); outputArgs.Append("-i " + ifList); this.ComposeFFMpegOutputArgs(outputArgs, outputFormat, settings); if (settings.ConcatVideoStream == false) { outputArgs.Append("-vn"); } if (settings.ConcatAudioStream == false) { outputArgs.Append("-an"); } string arguments = string.Format("-loglevel {0} -safe 0 -f concat {1} -y {2}", this.LogLevel, outputArgs.ToString(), outputFile); 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; } File.Delete(ifList); }