1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Vinno.IUS.Common.Media.FFmpeg
- {
- public class FFmpegLog: IFFmpegLog
- {
- private readonly StringBuilder _complete = new StringBuilder();
- private TimeSpan? _duration;
- private int _lastReportedProgress;
- private readonly Regex _durationRegex = new Regex(@"Duration: (\d{2}:\d{2}:\d{2}.\d{2})");
- private readonly Regex _timeRegex = new Regex(@"time=(\d{2}:\d{2}:\d{2}.\d{2})");
- public event Action<int> ProgressChanged;
- public string Frame { get; private set; } = string.Empty;
- public string Content { get; private set; } = string.Empty;
- public void WriteArgs(string args)
- {
- _complete.AppendLine("ARGS:");
- _complete.AppendLine("-------------");
- _complete.AppendLine(args);
- _complete.AppendLine();
- _complete.AppendLine("OUTPUT:");
- _complete.AppendLine("-------------");
- }
- public void Write(string text)
- {
- if (text == null)
- return;
- _complete.AppendLine(text);
- if (_duration == null)
- {
- var match = _durationRegex.Match(text);
- if (match.Success)
- {
- _duration = TimeSpan.Parse(match.Groups[1].Value);
- }
- }
- if (text.StartsWith("frame=") || text.StartsWith("size="))
- {
- Frame = text;
- if (_duration != null)
- {
- var match = _timeRegex.Match(text);
- if (match.Success)
- {
- var time = TimeSpan.Parse(match.Groups[1].Value);
- var progress = (int)(time.TotalMilliseconds * 100 / _duration.Value.TotalMilliseconds);
- if (progress > _lastReportedProgress)
- {
- _lastReportedProgress = progress;
- ProgressChanged?.Invoke(progress);
- }
- }
- }
- }
- else Content += text + Environment.NewLine;
- }
- public string GetCompleteLog() => _complete.ToString();
- }
- }
|