Fix progress and add unit test

Former-commit-id: 55a7e74817
This commit is contained in:
Malte Rosenbjerg 2020-03-02 22:50:04 +01:00
parent 85b75dfa75
commit 418b0e57c7
2 changed files with 32 additions and 18 deletions

View file

@ -351,5 +351,32 @@ public void Video_Duration() {
output.Delete();
}
}
[TestMethod]
public void Video_UpdatesProgress() {
var output = Input.OutputLocation(VideoType.Mp4);
var percentageDone = 0.0;
void OnProgess(double percentage) => percentageDone = percentage;
Encoder.OnProgress += OnProgess;
var arguments = new ArgumentContainer
{
new InputArgument(VideoLibrary.LocalVideo),
new DurationArgument(TimeSpan.FromSeconds(8)),
new OutputArgument(output)
};
try {
Encoder.Convert(arguments);
Encoder.OnProgress -= OnProgess;
Assert.IsTrue(File.Exists(output.FullName));
Assert.AreNotEqual(0.0, percentageDone);
} finally {
if (File.Exists(output.FullName))
output.Delete();
}
}
}
}

View file

@ -479,8 +479,6 @@ public void Stop()
private readonly string _ffmpegPath;
private TimeSpan _totalTime;
private volatile StringBuilder _errorOutput = new StringBuilder();
private bool RunProcess(ArgumentContainer container, FileInfo output)
{
_instance?.Dispose();
@ -492,7 +490,7 @@ private bool RunProcess(ArgumentContainer container, FileInfo output)
if (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0)
{
throw new FFMpegException(FFMpegExceptionType.Process, _errorOutput);
throw new FFMpegException(FFMpegExceptionType.Process, string.Join("\n", _instance.ErrorData));
}
return exitCode == 0;
@ -509,31 +507,20 @@ private void Cleanup(IEnumerable<string> pathList)
}
}
private static readonly Regex ProgressRegex = new Regex(@"\w\w:\w\w:\w\w", RegexOptions.Compiled);
private static readonly Regex ProgressRegex = new Regex(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled);
private Instance _instance;
private void OutputData(object sender, (DataType Type, string Data) msg)
{
var (type, data) = msg;
if (data == null) return;
if (type == DataType.Error)
{
_errorOutput.AppendLine(data);
return;
}
#if DEBUG
Trace.WriteLine(data);
Trace.WriteLine(msg.Data);
#endif
if (OnProgress == null) return;
if (!data.Contains("frame")) return;
var match = ProgressRegex.Match(data);
var match = ProgressRegex.Match(msg.Data);
if (!match.Success) return;
var processed = TimeSpan.Parse(match.Value, CultureInfo.InvariantCulture);
var processed = TimeSpan.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
var percentage = Math.Round(processed.TotalSeconds / _totalTime.TotalSeconds * 100, 2);
OnProgress(percentage);
}