public void GetVideoThumbnail(string inputFile, string outputFile, float?frameTime, ConvertSettings settings) { Media input = new Media { Filename = inputFile }; Media output = new Media { Filename = outputFile, Format = "mjpeg" }; if (settings == null) { settings = new ConvertSettings(); } settings.VideoFrameCount = 1; if (frameTime.HasValue) { settings.Seek = frameTime; } ConvertMedia(input, output, settings); }
public void ConvertMedia(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings) { if (inputFile == null) { throw new ArgumentNullException("inputFile"); } if (outputFile == null) { throw new ArgumentNullException("outputFile"); } if (File.Exists(inputFile) && string.IsNullOrEmpty(Path.GetExtension(inputFile)) && inputFormat == null) { throw new Exception("Input format is required for file without extension"); } if (string.IsNullOrEmpty(Path.GetExtension(outputFile)) && outputFormat == null) { throw new Exception("Output format is required for file without extension"); } Media input = new Media { Filename = inputFile, Format = inputFormat }; Media output = new Media { Filename = outputFile, Format = outputFormat }; ConvertMedia(input, output, settings ?? new ConvertSettings()); }
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); } } }
protected string ComposeFFMpegCommandLineArgs(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings) { StringBuilder stringBuilder = new StringBuilder(); if (settings.AppendSilentAudioStream) { stringBuilder.Append(" -f lavfi -i aevalsrc=0 "); } if (settings.Seek.HasValue) { stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " -ss {0}", settings.Seek); } if (inputFormat != null) { stringBuilder.Append(" -f " + inputFormat); } if (settings.CustomInputArgs != null) { stringBuilder.AppendFormat(" {0} ", settings.CustomInputArgs); } StringBuilder stringBuilder2 = new StringBuilder(); ComposeFFMpegOutputArgs(stringBuilder2, outputFormat, settings); if (settings.AppendSilentAudioStream) { stringBuilder2.Append(" -shortest "); } return(string.Format("-y -loglevel {4} {0} -i {1} {2} {3}", stringBuilder.ToString(), CommandArgParameter(inputFile), stringBuilder2.ToString(), CommandArgParameter(outputFile), LogLevel)); }
private ConvertLiveMediaTask CreateLiveMediaTask(string toolArgs, Stream inputStream, Stream outputStream, ConvertSettings settings) { FFMpegProgress fFMpegProgress = new FFMpegProgress(OnConvertProgress, this.ConvertProgress != null); if (settings != null) { fFMpegProgress.Seek = settings.Seek; fFMpegProgress.MaxDuration = settings.MaxDuration; } return(new ConvertLiveMediaTask(this, toolArgs, inputStream, outputStream, fFMpegProgress)); }
public ConvertLiveMediaTask ConvertLiveMedia(Stream inputStream, string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings) { EnsureFFMpegLibs(); string toolArgs = ComposeFFMpegCommandLineArgs("-", inputFormat, "-", outputFormat, settings); return(CreateLiveMediaTask(toolArgs, inputStream, outputStream, settings)); }
public ConvertLiveMediaTask ConvertLiveMedia(string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings) { return(ConvertLiveMedia((Stream)null, inputFormat, outputStream, outputFormat, settings)); }