Skip to content

Commit

Permalink
Merge pull request #48 from Greek6/master
Browse files Browse the repository at this point in the history
Fix for the ConvertProgressEvent
  • Loading branch information
AydinAdn authored Sep 16, 2016
2 parents 0fead8a + 699cb4a commit 246571a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
8 changes: 4 additions & 4 deletions MediaToolkit src/MediaToolkit/ConvertProgressEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ConvertProgressEventArgs : EventArgs
/// <param name="fps">The frames converted per second</param>
/// <param name="sizeKb">The current size in Kb of the converted media</param>
/// <param name="bitrate">The bit rate of the converted media</param>
public ConvertProgressEventArgs(TimeSpan processed, TimeSpan totalDuration, long frame, double fps, int sizeKb,
public ConvertProgressEventArgs(TimeSpan processed, TimeSpan totalDuration, long? frame, double? fps, int? sizeKb,
double? bitrate)
{
TotalDuration = totalDuration;
Expand All @@ -24,9 +24,9 @@ public ConvertProgressEventArgs(TimeSpan processed, TimeSpan totalDuration, long
Bitrate = bitrate;
}

public long Frame { get; private set; }
public double Fps { get; private set; }
public int SizeKb { get; private set; }
public long? Frame { get; private set; }
public double? Fps { get; private set; }
public int? SizeKb { get; private set; }
public TimeSpan ProcessedDuration { get; private set; }
public double? Bitrate { get; private set; }
public TimeSpan TotalDuration { get; internal set; }
Expand Down
51 changes: 44 additions & 7 deletions MediaToolkit src/MediaToolkit/Util/RegexEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class RegexEngine
{Find.Duration, new Regex(@"Duration: ([^,]*), ")},
{Find.ConvertProgressFrame, new Regex(@"frame=\s*([0-9]*)")},
{Find.ConvertProgressFps, new Regex(@"fps=\s*([0-9]*\.?[0-9]*?)")},
{Find.ConvertProgressSize, new Regex(@" size=\s*([0-9]*)kB")},
{Find.ConvertProgressSize, new Regex(@"size=\s*([0-9]*)kB")},
{Find.ConvertProgressFinished, new Regex(@"Lsize=\s*([0-9]*)kB")},
{Find.ConvertProgressTime, new Regex(@"time=\s*([^ ]*)")},
{Find.ConvertProgressBitrate, new Regex(@"bitrate=\s*([0-9]*\.?[0-9]*?)kbits/s")},
Expand Down Expand Up @@ -53,22 +53,59 @@ internal static bool IsProgressData(string data, out ConvertProgressEventArgs pr
Match matchTime = Index[Find.ConvertProgressTime].Match(data);
Match matchBitrate = Index[Find.ConvertProgressBitrate].Match(data);

if (!matchFrame.Success || !matchFps.Success || !matchSize.Success || !matchTime.Success ||
!matchBitrate.Success) return false;
if (!matchSize.Success || !matchTime.Success || !matchBitrate.Success)
return false;

TimeSpan processedDuration;
TimeSpan.TryParse(matchTime.Groups[1].Value, out processedDuration);

long frame = Convert.ToInt64(matchFrame.Groups[1].Value, CultureInfo.InvariantCulture);
double fps = Convert.ToDouble(matchFps.Groups[1].Value, CultureInfo.InvariantCulture);
int sizeKb = Convert.ToInt32(matchSize.Groups[1].Value, CultureInfo.InvariantCulture);
double bitrate = Convert.ToDouble(matchBitrate.Groups[1].Value, CultureInfo.InvariantCulture);
long? frame = GetLongValue(matchFrame);
double? fps = GetDoubleValue(matchFps);
int? sizeKb = GetIntValue(matchSize);
double? bitrate = GetDoubleValue(matchBitrate);

progressEventArgs = new ConvertProgressEventArgs(processedDuration, TimeSpan.Zero, frame, fps, sizeKb, bitrate);

return true;
}

private static long? GetLongValue(Match match)
{
try
{
return Convert.ToInt64(match.Groups[1].Value, CultureInfo.InvariantCulture);
}
catch
{
return null;
}
}

private static double? GetDoubleValue(Match match)
{
try
{
return Convert.ToDouble(match.Groups[1].Value, CultureInfo.InvariantCulture);
}
catch
{
return null;
}
}

private static int? GetIntValue(Match match)
{
try
{
return Convert.ToInt32(match.Groups[1].Value, CultureInfo.InvariantCulture);
}
catch
{
return null;
}
}


/// <summary>
/// <para> ---- </para>
/// <para>Establishes whether the data indicates the conversion is complete</para>
Expand Down

0 comments on commit 246571a

Please sign in to comment.