Skip to content

Commit

Permalink
better RegEx
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime4000 committed Jul 1, 2024
1 parent 240c9b7 commit 54d1536
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
4 changes: 2 additions & 2 deletions IFME/MediaEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ internal static void Audio(MediaQueue queue, string tempDir)

if (!ac.Mode[md].MultiChannelSupport) // Mode didn't support MultiChannel, for example eSBR on exhale. Down-mixing to stereo
{
frmMain.PrintLog($"[INFO] {codec.Name}, {codec.Audio.Mode[md].Name} doesn't support Multi Channel...");
frmMain.PrintLog($"[WARN] {codec.Name}, {codec.Audio.Mode[md].Name} doesn't support Multi Channel...");

if (item.Info.Channel >= 2)
ch = $"-ac 2";
}

if (!ac.Mode[md].MonoSupport) // Some audio encode mode doesn't support Mono, so, need to up-mixing to stereo
{
frmMain.PrintLog($"[INFO] {codec.Name}, {codec.Audio.Mode[md].Name} doesn't support Mono Channel...");
frmMain.PrintLog($"[WARN] {codec.Name}, {codec.Audio.Mode[md].Name} doesn't support Mono Channel...");

if (item.Info.Channel == 1)
ch = $"-ac 2";
Expand Down
48 changes: 31 additions & 17 deletions IFME/ProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,42 @@ private void Proc_DataReceived(object sender, DataReceivedEventArgs e)

if (!string.IsNullOrEmpty(e.Data))
{
var tf = @"(?<=encoded\s) ?\d+(?=> frames in \d+.\d+)?"; //x265 encoded total frame
var tfm = Regex.Matches(e.Data, tf, RegexOptions.IgnoreCase);
if (tfm.Count > 0)
var tf = @"(?<=encoded\s) ?\d+(?=> frames in \d+.\d+)?"; //x265 encoded total frame
var tfm = Regex.Matches(e.Data, tf, RegexOptions.IgnoreCase);
if (tfm.Count > 0)
{
if (tfm.Count > 0)
{
if (!int.TryParse(tfm[0].Value, out int rfc))
MediaEncoding.RealFrameCount = rfc;
}
if (tfm.Count > 0)
{
if (!int.TryParse(tfm[0].Value, out int rfc))
MediaEncoding.RealFrameCount = rfc;
}

return;
return;
}

var p = @"(frame[ ]{1,}\d+)|(\d+.\d+[ ]{1,}kbps)|(\d+.\d+[ ]{1,}fps)";
var x = Regex.Matches(e.Data, p, RegexOptions.IgnoreCase);
if (x.Count >= 3)
{
int.TryParse(x[0].ToString().Substring(6), out int cf);
var patterns = new[]
{
@"vvenc \[info\]: stats: frame=\s*(\d+) .* avg_fps=\s*([\d\.]+) .* avg_bitrate=\s*([\d\.]+) kbps", // Fraunhofer VVC
@"\[\d+\.\d+%\] (\d+)/\d+ frames, ([\d\.]+) fps, ([\d\.]+) kb/s", // x264 & x265
@"(\d+) frames: ([\d\.]+) fps, ([\d\.]+) kb/s", // Rigaya NVEnc
@"frame=\s*(\d+) fps=\s*([\d\.]+) .* bitrate=\s*([\d\.]+)kbits/s", // FFmpeg
@"Encoding frame\s*(\d+)\s* ([\d\.]+) kbps\s* ([\d\.]+) fps" // SVT-AV1
};

foreach (var pattern in patterns)
{
var match = Regex.Match(e.Data, pattern);
if (match.Success)
{
int.TryParse(match.Groups[1].Value, out int cf);
double.TryParse(match.Groups[2].Value, out double speed);
double.TryParse(match.Groups[3].Value, out double bitrate);

frmMain.PrintProgress($"[{(float)cf / MediaEncoding.RealFrameCount * 100:0.0} %] Frame: {cf}, Bitrate: {x[1]}, Speed: {x[2]}");
return;
}
frmMain.PrintProgress($"[{(float)cf / MediaEncoding.RealFrameCount * 100:0.0} %] Frame: {cf}, Bitrate: {bitrate} kb/s, Speed: {speed} fps");

return;
}
}

var regexPattern = @"( \d+ bits )|( \d+ seconds)|(\d+/\d{3})|(size=[ ]{1,}\d+)|(frame[ ]{1,}\d+)|(\d+.\d+[ ]{1,}kb/s)|(\d+.\d+[ ]{1,}fps)|(\d+[ ]{1,}frames:\s\d+.\d+[ ]{1,}fps,\s\d+.\d+[ ]{1,}kb/s,\sGPU\s\d+%,\sVE\s\d+%)";
Match m = Regex.Match(e.Data, regexPattern, RegexOptions.IgnoreCase);
Expand Down

0 comments on commit 54d1536

Please sign in to comment.