Skip to content

Commit

Permalink
Improved unit tests fir FFmpegRunner to not take over display, be dep…
Browse files Browse the repository at this point in the history
…endent on an installed media player, leave junk on the screen, and leave orphaned temp files.
  • Loading branch information
tombogle committed Nov 8, 2024
1 parent 46fa5f0 commit afd7bcb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
47 changes: 45 additions & 2 deletions SIL.Media.Tests/FFmpegRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.IO;
using FFMpegCore;
using NUnit.Framework;
using SIL.IO;
using SIL.Media.Tests.Properties;
Expand Down Expand Up @@ -79,7 +81,27 @@ public void MakeLowQualityCompressedAudio_CreatesFile()
var outputPath = originalAudioPath.Replace("mp3", "low.mp3");
FFmpegRunner.MakeLowQualityCompressedAudio(originalAudioPath, outputPath, new ConsoleProgress());
Assert.IsTrue(File.Exists(outputPath));
System.Diagnostics.Process.Start(outputPath);

var mediaInfoOrig = FFProbe.Analyse(file.Path);
var mediaInfo = FFProbe.Analyse(outputPath);

// Validate resolution and bit rate
Assert.That(mediaInfo.PrimaryVideoStream, Is.Null);
Assert.That(mediaInfo.PrimaryAudioStream, Is.Not.Null);
Assert.That(mediaInfo.AudioStreams.Count, Is.EqualTo(1));
Assert.That(mediaInfo.PrimaryAudioStream.Channels, Is.EqualTo(1));
Assert.That(mediaInfo.Format.BitRate, Is.LessThan(mediaInfoOrig.Format.BitRate));
Assert.That(mediaInfo.PrimaryAudioStream.SampleRateHz, Is.EqualTo(8000));
try
{
// When running the by-hand test, the default media player might leave this
// locked, so this cleanup will fail.
RobustFile.Delete(outputPath);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}

Expand All @@ -92,7 +114,28 @@ public void MakeLowQualitySmallVideo_CreatesFile()
var outputPath = file.Path.Replace("wmv", "low.wmv");
FFmpegRunner.MakeLowQualitySmallVideo(file.Path, outputPath, 0, new ConsoleProgress());
Assert.IsTrue(File.Exists(outputPath));
System.Diagnostics.Process.Start(outputPath);

var mediaInfoOrig = FFProbe.Analyse(file.Path);
var mediaInfo = FFProbe.Analyse(outputPath);

// Validate resolution and bit rate
Assert.That(mediaInfo.PrimaryVideoStream, Is.Not.Null);
Assert.That(mediaInfo.VideoStreams.Count, Is.EqualTo(1));
Assert.That(mediaInfo.PrimaryAudioStream, Is.Not.Null);
Assert.That(mediaInfo.AudioStreams.Count, Is.EqualTo(1));
Assert.That(mediaInfo.PrimaryVideoStream.Width, Is.EqualTo(160));
Assert.That(mediaInfo.PrimaryVideoStream.Height, Is.EqualTo(120));
Assert.That(mediaInfo.Format.BitRate, Is.LessThan(mediaInfoOrig.Format.BitRate));
try
{
// When running the by-hand test, the default media player might leave this
// locked, so this cleanup will fail.
RobustFile.Delete(outputPath);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions SIL.Media/FFmpegRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static ExecutionResult ExtractMp3Audio(string inputPath, string outputPat
var arguments = $"-i \"{inputPath}\" -vn {mp3LameCodecArg} -ac {channels} \"{outputPath}\"";
var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small")
&& File.Exists(outputPath))
{
Expand Down Expand Up @@ -203,7 +203,7 @@ public static ExecutionResult ExtractOggAudio(string inputPath, string outputPat
var arguments = $"-i \"{inputPath}\" -vn -acodec vorbis -ac {channels} \"{outputPath}\"";
var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small")
&& File.Exists(outputPath))
{
Expand Down Expand Up @@ -294,7 +294,7 @@ private static ExecutionResult ExtractAudio(string inputPath, string outputPath,

var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small")
&& File.Exists(outputPath))
{
Expand Down Expand Up @@ -328,7 +328,7 @@ public static ExecutionResult ChangeNumberOfAudioChannels(string inputPath,

var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small") && File.Exists(outputPath))
{
var doctoredResult = new ExecutionResult
Expand Down Expand Up @@ -361,7 +361,7 @@ public static ExecutionResult MakeLowQualityCompressedAudio(string inputPath, st

var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small")
&& File.Exists(outputPath))
{
Expand Down Expand Up @@ -404,7 +404,7 @@ public static ExecutionResult MakeLowQualitySmallVideo(string inputPath, string

var result = RunFFmpeg(arguments, progress);

//hide a meaningless error produced by some versions of liblame
// Hide a meaningless error produced by some versions of liblame
if (result.StandardError.Contains("lame: output buffer too small")
&& File.Exists(outputPath))
{
Expand Down

0 comments on commit afd7bcb

Please sign in to comment.