Пример #1
0
        internal static void TestAudio(string data, FFmpegTask task)
        {
            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 (task.MetaData == null)
            {
                task.MetaData = new MetaData();
            }

            if (task.MetaData.AudioData == null)
            {
                task.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
                };
            }
        }
Пример #2
0
        internal static void TestVideo(string data, FFmpegTask task)
        {
            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 (task.MetaData == null)
            {
                task.MetaData = new MetaData();
            }

            if (task.MetaData.VideoData == null)
            {
                task.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
                };
            }
        }
Пример #3
0
        //private void OnException(List<string> messages, FFmpegParameters parameters, int exitCode, Exception caughtException)
        //{
        //    var exceptionMessage = GetExceptionMessage(messages);
        //    var exception = new FFmpegException(exceptionMessage, caughtException, exitCode);
        //    OnConversionError(new ConversionErrorEventArgs(exception, parameters.InputFile, parameters.OutputFile));
        //}

        //private string GetExceptionMessage(List<string> messages)
        //    => messages.Count > 1
        //        ? messages[1] + messages[0]
        //        : string.Join(string.Empty, messages);

        private void FFmpegProcessOnErrorDataReceived(DataReceivedEventArgs e, FFmpegTask ffmpegTask, ref Exception exception, List <string> messages)
        {
            var totalMediaDuration = new TimeSpan();

            if (e.Data == null)
            {
                return;
            }

            try
            {
                messages.Insert(0, e.Data);

                if (ffmpegTask.MetaData == null)
                {
                    ffmpegTask.MetaData = new MetaData();
                }

                RegexEngine.TestVideo(e.Data, ffmpegTask);
                RegexEngine.TestAudio(e.Data, ffmpegTask);

                var matchDuration = RegexEngine._index[RegexEngine.Find.Duration].Match(e.Data);
                if (matchDuration.Success)
                {
                    if (ffmpegTask.MetaData == null)
                    {
                        ffmpegTask.MetaData = new MetaData();
                    }

                    RegexEngine.TimeSpanLargeTryParse(matchDuration.Groups[1].Value, out totalMediaDuration);
                    ffmpegTask.MetaData.Duration = totalMediaDuration;
                }

                if (RegexEngine.IsProgressData(e.Data, out var progressData))
                {
                    //if (parameters.InputFile != null)
                    //{
                    progressData.TotalDuration = ffmpegTask.MetaData?.Duration ?? totalMediaDuration;
                    //}

                    //OnProgressChanged(new ConversionProgressEventArgs(progressData, parameters.InputFile, parameters.OutputFile));
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }
Пример #4
0
        public async Task ExecuteAsync(FFmpegTask ffmpegTask, FFmpegEnviroment enviroment, CancellationToken cancellationToken = default)
        {
            var startInfo = new ProcessStartInfo
            {
                // -y overwrite output files
                Arguments              = "-y " + ffmpegTask.GetCommandString(),
                FileName               = enviroment.FFmpegPath,
                CreateNoWindow         = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false,
                WindowStyle            = ProcessWindowStyle.Hidden
            };

            await ExecuteAsync(startInfo, ffmpegTask, cancellationToken);
        }
Пример #5
0
        private async Task ExecuteAsync(ProcessStartInfo startInfo, FFmpegTask ffmpegTask, CancellationToken cancellationToken = default)
        {
            var       messages        = new List <string>();
            Exception caughtException = null;

            using (var ffmpegProcess = new Process()
            {
                StartInfo = startInfo
            })
            {
                //ffmpegProcess.ErrorDataReceived += (sender, e) => OnData(new ConversionDataEventArgs(e.Data, parameters.InputFile, parameters.OutputFile));
                ffmpegProcess.ErrorDataReceived += (sender, e) => FFmpegProcessOnErrorDataReceived(e, ffmpegTask, ref caughtException, messages);

                Task <int> task = null;
                try
                {
                    task = ffmpegProcess.WaitForExitAsync(null, cancellationToken);
                    await task;
                }
                catch (Exception)
                {
                    // An exception occurs if the user cancels the operation by calling Cancel on the CancellationToken.
                    // Exc.Message will be "A task was canceled." (in English).
                    // task.IsCanceled will be true.
                    if (task.IsCanceled)
                    {
                        throw new TaskCanceledException(task);
                    }
                    // I don't think this can occur, but if some other exception, rethrow it.
                    throw;
                }

                //if (caughtException != null || ffmpegProcess.ExitCode != 0)
                //{
                //    OnException(messages, parameters, ffmpegProcess.ExitCode, caughtException);
                //}
                //else
                //{
                //    OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFile, parameters.OutputFile));
                //}
            }
        }