internal static void TestAudio(string data, FFmpegParameters engine) { var matchMetaAudio = Index[Find.MetaAudio].Match(data); if (!matchMetaAudio.Success) { return; } var fullMetadata = matchMetaAudio.Groups[1].ToString(); var matchAudioFormatHzChannel = Index[Find.AudioFormatHzChannel].Match(fullMetadata).Groups; var matchAudioBitRate = Index[Find.BitRate].Match(fullMetadata).Groups; if (engine.InputFile.MetaData == null) { engine.InputFile.MetaData = new MetaData(); } if (engine.InputFile.MetaData.AudioData == null) { engine.InputFile.MetaData.AudioData = new MetaData.Audio { Format = matchAudioFormatHzChannel[1].ToString(), SampleRate = matchAudioFormatHzChannel[2].ToString(), ChannelOutput = matchAudioFormatHzChannel[3].ToString(), BitRateKbs = !string.IsNullOrWhiteSpace(matchAudioBitRate[1].ToString()) ? Convert.ToInt32(matchAudioBitRate[1].ToString()) : 0 } } ; }
public async Task ExecuteAsync(FFmpegParameters parameters, string ffmpegFilePath, CancellationToken cancellationToken = default(CancellationToken)) { var argumentBuilder = new FFmpegArgumentBuilder(); var arguments = argumentBuilder.Build(parameters); var startInfo = GenerateStartInfo(ffmpegFilePath, arguments); await ExecuteAsync(startInfo, parameters, cancellationToken); }
public void Execute(FFmpegParameters parameters, string ffmpegFilePath) { var argumentBuilder = new FFmpegArgumentBuilder(); var arguments = argumentBuilder.Build(parameters); var startInfo = GenerateStartInfo(ffmpegFilePath, arguments); Execute(startInfo, parameters); }
private async Task ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken = default(CancellationToken)) { var ffmpegProcess = new FFmpegProcess(); ffmpegProcess.Progress += OnProgress; ffmpegProcess.Completed += OnComplete; ffmpegProcess.Error += OnError; ffmpegProcess.Data += OnData; await ffmpegProcess.ExecuteAsync(parameters, FFmpegFilePath, cancellationToken); }
private void Execute(FFmpegParameters parameters) { var ffmpegProcess = new FFmpegProcess(); ffmpegProcess.Progress += OnProgress; ffmpegProcess.Completed += OnComplete; ffmpegProcess.Error += OnError; ffmpegProcess.Data += OnData; ffmpegProcess.Execute(parameters, FFmpegFilePath); }
public MetaData GetMetaData(MediaFile mediaFile) { var parameters = new FFmpegParameters { Task = FFmpegTask.GetMetaData, InputFile = mediaFile }; Execute(parameters); return(parameters.InputFile.MetaData); }
public async Task <MetaData> GetMetaDataAsync(MediaFile mediaFile, CancellationToken cancellationToken = default(CancellationToken)) { var parameters = new FFmpegParameters { Task = FFmpegTask.GetMetaData, InputFile = mediaFile }; await ExecuteAsync(parameters, cancellationToken); return(parameters.InputFile.MetaData); }
public MediaFile Convert(MediaFile input, MediaFile output, ConversionOptions options = null) { var parameters = new FFmpegParameters { Task = FFmpegTask.Convert, InputFile = input, OutputFile = output, ConversionOptions = options }; Execute(parameters); return(parameters.OutputFile); }
public MediaFile GetThumbnail(MediaFile input, MediaFile output, ConversionOptions options) { var parameters = new FFmpegParameters { Task = FFmpegTask.GetThumbnail, InputFile = input, OutputFile = output, ConversionOptions = options }; Execute(parameters); return(parameters.OutputFile); }
public async Task <MediaFile> ConvertAsync(MediaFile input, MediaFile output, ConversionOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var parameters = new FFmpegParameters { Task = FFmpegTask.Convert, InputFile = input, OutputFile = output, ConversionOptions = options }; await ExecuteAsync(parameters, cancellationToken); return(parameters.OutputFile); }
public string Build(FFmpegParameters parameters) { switch (parameters.Task) { case FFmpegTask.Convert: return(Convert(parameters.InputFile, parameters.OutputFile, parameters.ConversionOptions)); case FFmpegTask.GetMetaData: return(GetMetadata(parameters.InputFile)); case FFmpegTask.GetThumbnail: return(GetThumbnail(parameters.InputFile, parameters.OutputFile, parameters.ConversionOptions)); default: throw new ArgumentOutOfRangeException(); } }
private void FFmpegProcessOnErrorDataReceived(DataReceivedEventArgs e, FFmpegParameters parameters, ref Exception exception, List <string> messages) { var totalMediaDuration = new TimeSpan(); if (e.Data == null) { return; } try { messages.Insert(0, e.Data); if (parameters.InputFile != null) { RegexEngine.TestVideo(e.Data, parameters); RegexEngine.TestAudio(e.Data, parameters); var matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(e.Data); if (matchDuration.Success) { if (parameters.InputFile.MetaData == null) { parameters.InputFile.MetaData = new MetaData { FileInfo = parameters.InputFile.FileInfo } } ; TimeSpan.TryParse(matchDuration.Groups[1].Value, out totalMediaDuration); parameters.InputFile.MetaData.Duration = totalMediaDuration; } } if (RegexEngine.IsProgressData(e.Data, out var progressData)) { progressData.TotalDuration = totalMediaDuration; OnProgressChanged(new ConversionProgressEventArgs(progressData, parameters.InputFile, parameters.OutputFile)); } } catch (Exception ex) { exception = ex; } }
private async Task ExecuteAsync(ProcessStartInfo startInfo, FFmpegParameters parameters, CancellationToken cancellationToken = default(CancellationToken)) { var messages = new List <string>(); Exception caughtException = null; using (var ffmpegProcess = Process.Start(startInfo)) { if (ffmpegProcess == null) { throw new InvalidOperationException(Resources.Resources.Exceptions_FFmpeg_Process_Not_Running); } ffmpegProcess.ErrorDataReceived += (sender, e) => OnData(new ConversionDataEventArgs(e.Data, parameters.InputFile, parameters.OutputFile)); ffmpegProcess.ErrorDataReceived += (sender, e) => { FFmpegProcessOnErrorDataReceived(e, parameters, ref caughtException, messages); }; ffmpegProcess.BeginErrorReadLine(); await ffmpegProcess.WaitForExitAsync(cancellationToken); if (ffmpegProcess.ExitCode != 0 || caughtException != null) { try { ffmpegProcess.Kill(); } catch (InvalidOperationException) { // swallow exceptions that are thrown when killing the process, // one possible candidate is the application ending naturally before we get a chance to kill it } catch (Win32Exception) { } var exceptionMessage = GetExceptionMessage(messages); var exception = new FFmpegException(exceptionMessage, caughtException, ffmpegProcess.ExitCode); OnConversionError(new ConversionErrorEventArgs(exception, parameters.InputFile, parameters.OutputFile)); } else { OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFile, parameters.OutputFile)); } } }
internal static void TestVideo(string data, FFmpegParameters engine) { var matchMetaVideo = Index[Find.MetaVideo].Match(data); if (!matchMetaVideo.Success) { return; } var fullMetadata = matchMetaVideo.Groups[1].ToString(); var matchVideoFormatColorSize = Index[Find.VideoFormatColorSize].Match(fullMetadata).Groups; var matchVideoFps = Index[Find.VideoFps].Match(fullMetadata).Groups; var matchVideoBitRate = Index[Find.BitRate].Match(fullMetadata); if (engine.InputFile.MetaData == null) { engine.InputFile.MetaData = new MetaData(); } if (engine.InputFile.MetaData.VideoData == null) { engine.InputFile.MetaData.VideoData = new MetaData.Video { Format = matchVideoFormatColorSize[1].ToString(), ColorModel = matchVideoFormatColorSize[2].ToString(), FrameSize = matchVideoFormatColorSize[3].ToString(), Fps = matchVideoFps[1].Success && !string.IsNullOrEmpty(matchVideoFps[1].ToString()) ? Convert.ToDouble(matchVideoFps[1].ToString(), new CultureInfo("en-US")) : 0, BitRateKbs = matchVideoBitRate.Success ? (int?)Convert.ToInt32(matchVideoBitRate.Groups[1].ToString()) : null } } ; }
private void Execute(FFmpegParameters parameters) { ExecuteAsync(parameters).Wait(); }
private void Execute(ProcessStartInfo startInfo, FFmpegParameters parameters) { ExecuteAsync(startInfo, parameters).Wait(); }
public void Execute(FFmpegParameters parameters, string ffmpegFilePath) { ExecuteAsync(parameters, ffmpegFilePath).Wait(); }