Simplify regex

This commit is contained in:
Jaex 2019-11-23 11:23:07 +03:00
parent a1418be676
commit 7faad83eeb

View file

@ -35,6 +35,9 @@ namespace ShareX.MediaLib
{
public class FFmpegCLIManager : ExternalCLIManager
{
public delegate void EncodeProgressChangedEventHandler(float percentage);
public event EncodeProgressChangedEventHandler EncodeProgressChanged;
public string FFmpegPath { get; private set; }
public StringBuilder Output { get; private set; }
public bool ShowError { get; set; }
@ -43,9 +46,6 @@ public class FFmpegCLIManager : ExternalCLIManager
public TimeSpan EncodeTime { get; set; }
public float EncodePercentage { get; set; }
public delegate void EncodeProgressChangedEventHandler(float percentage);
public event EncodeProgressChangedEventHandler EncodeProgressChanged;
public FFmpegCLIManager(string ffmpegPath)
{
FFmpegPath = ffmpegPath;
@ -55,63 +55,6 @@ public FFmpegCLIManager(string ffmpegPath)
Helpers.CreateDirectoryFromFilePath(FFmpegPath);
}
private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e)
{
lock (this)
{
string data = e.Data;
if (!string.IsNullOrEmpty(data))
{
Output.AppendLine(data);
if (TrackEncodeProgress)
{
if (VideoDuration.Ticks == 0)
{
// Duration: 00:00:15.32, start: 0.000000, bitrate: 1095 kb/s
Match matchInput = Regex.Match(data,
@"\s*Duration: (?<Duration>\d{2}:\d{2}:\d{2}\.\d{2}),\s*start: (?<Start>\d+\.\d+),\s*bitrate: (?<Bitrate>\d+) kb/s", RegexOptions.CultureInvariant);
if (matchInput.Success)
{
TimeSpan duration;
if (TimeSpan.TryParse(matchInput.Groups["Duration"].Value, out duration))
{
VideoDuration = duration;
}
}
}
else
{
//frame= 942 fps=187 q=35.0 size= 3072kB time=00:00:38.10 bitrate= 660.5kbits/s speed=7.55x
Match matchInput = Regex.Match(data,
@"frame=\s*(?<Frame>\d+)\s*fps=\s*(?<FPS>\d+).+time=\s*(?<Time>\d{2}:\d{2}:\d{2}\.\d{2})\s*bitrate=", RegexOptions.CultureInvariant);
if (matchInput.Success)
{
TimeSpan time;
if (TimeSpan.TryParse(matchInput.Groups["Time"].Value, out time))
{
EncodeTime = time;
EncodePercentage = ((float)EncodeTime.Ticks / VideoDuration.Ticks) * 100;
OnEncodeProgressChanged(EncodePercentage);
}
}
}
}
}
}
}
protected void OnEncodeProgressChanged(float percentage)
{
EncodeProgressChanged?.Invoke(percentage);
}
public bool Run(string args)
{
return Run(FFmpegPath, args);
@ -129,6 +72,66 @@ private bool Run(string path, string args)
return result;
}
private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e)
{
lock (this)
{
string data = e.Data;
if (!string.IsNullOrEmpty(data))
{
Output.AppendLine(data);
if (TrackEncodeProgress)
{
UpdateEncodeProgress(data);
}
}
}
}
private void UpdateEncodeProgress(string data)
{
if (VideoDuration.Ticks == 0)
{
// Duration: 00:00:15.32, start: 0.000000, bitrate: 1095 kb/s
Match match = Regex.Match(data, @"Duration:\s*(\d+:\d+:\d+\.\d+),\s*start:", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (match.Success)
{
TimeSpan duration;
if (TimeSpan.TryParse(match.Groups[1].Value, out duration))
{
VideoDuration = duration;
}
}
}
else
{
//frame= 942 fps=187 q=35.0 size= 3072kB time=00:00:38.10 bitrate= 660.5kbits/s speed=7.55x
Match match = Regex.Match(data, @"time=\s*(\d+:\d+:\d+\.\d+)\s*bitrate=", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (match.Success)
{
TimeSpan time;
if (TimeSpan.TryParse(match.Groups[1].Value, out time))
{
EncodeTime = time;
EncodePercentage = ((float)EncodeTime.Ticks / VideoDuration.Ticks) * 100;
OnEncodeProgressChanged(EncodePercentage);
}
}
}
}
protected void OnEncodeProgressChanged(float percentage)
{
EncodeProgressChanged?.Invoke(percentage);
}
public VideoInfo GetVideoInfo(string videoPath)
{
VideoInfo videoInfo = new VideoInfo();
@ -138,7 +141,8 @@ public VideoInfo GetVideoInfo(string videoPath)
{
string output = Output.ToString();
Match matchInput = Regex.Match(output, @"Duration: (?<Duration>\d{2}:\d{2}:\d{2}\.\d{2}),.+?start: (?<Start>\d+\.\d+),.+?bitrate: (?<Bitrate>\d+) kb/s", RegexOptions.CultureInvariant);
Match matchInput = Regex.Match(output, @"Duration: (?<Duration>\d{2}:\d{2}:\d{2}\.\d{2}),.+?start: (?<Start>\d+\.\d+),.+?bitrate: (?<Bitrate>\d+) kb/s",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (matchInput.Success)
{
@ -152,7 +156,7 @@ public VideoInfo GetVideoInfo(string videoPath)
}
Match matchVideoStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Video: (?<Codec>.+?) \(.+?,.+?, (?<Width>\d+)x(?<Height>\d+).+?, (?<FPS>\d+(?:\.\d+)?) fps",
RegexOptions.CultureInvariant);
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (matchVideoStream.Success)
{
@ -161,7 +165,8 @@ public VideoInfo GetVideoInfo(string videoPath)
videoInfo.VideoFPS = double.Parse(matchVideoStream.Groups["FPS"].Value, CultureInfo.InvariantCulture);
}
Match matchAudioStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Audio: (?<Codec>.+?)(?: \(|,)", RegexOptions.CultureInvariant);
Match matchAudioStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Audio: (?<Codec>.+?)(?: \(|,)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (matchAudioStream.Success)
{