Update audio extraction to use file-based streaming instead of in-memory buffers to support processing video files larger than 2GB. **Changes:** - Modified `IAudioExtractor.ExtractMp3FromMp4Async()` to accept an output audio stream parameter instead of creating a MemoryStream internally - Updated `AudioExtractor` to write directly to the provided audio stream, eliminating the need to load entire video into memory - Enhanced `FFMpegService` to detect FileStream inputs and use direct file-to-file processing when possible, falling back to pipe-based streaming for other stream types - Refactored `DefaultCommand` to create output directory and audio file stream before extraction, enabling direct file-based audio extraction - Updated all unit and integration tests to accommodate the new audio stream parameter This change prevents `OutOfMemoryException` errors when processing large video files by streaming data directly to disk rather than buffering in memory. Additionally, it removes the possibility of overflowing the 2GB size limit that `MemoryStream` has, which would result in `IOException` errors when trying to write or copy to it.
144 lines
4.1 KiB
C#
144 lines
4.1 KiB
C#
using System.Text;
|
|
|
|
using StreamShorts.Library.Media;
|
|
|
|
namespace StreamShorts.Library.Tests.Unit.Media.Audio;
|
|
|
|
public class AudioExtractorTests
|
|
{
|
|
private readonly Mock<IVideoService> _mockFfmpegService = new();
|
|
private readonly AudioExtractor _sut;
|
|
|
|
public AudioExtractorTests()
|
|
{
|
|
_sut = new(_mockFfmpegService.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WhenCalledWithNullFfmpegService_ItShouldThrow()
|
|
{
|
|
var action = () => new AudioExtractor(null!);
|
|
|
|
action.Should().Throw<ArgumentNullException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNull_ItShouldThrow()
|
|
{
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(null!, new MemoryStream());
|
|
|
|
await action.Should().ThrowAsync<ArgumentNullException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNotReadable_ItShouldThrow()
|
|
{
|
|
var mockStream = new Mock<Stream>();
|
|
mockStream.Setup(s => s.CanRead).Returns(false);
|
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
|
|
|
await action.Should().ThrowAsync<ArgumentException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNotSeekable_ItShouldThrow()
|
|
{
|
|
var mockStream = new Mock<Stream>();
|
|
mockStream.Setup(s => s.CanRead).Returns(true);
|
|
mockStream.Setup(s => s.CanSeek).Returns(false);
|
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
|
|
|
await action.Should().ThrowAsync<ArgumentException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceThrows_ItShouldThrow()
|
|
{
|
|
var mockStream = new Mock<Stream>();
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
_mockFfmpegService.
|
|
Setup(
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
It.IsAny<Stream>(),
|
|
It.IsAny<Stream>()
|
|
)
|
|
)
|
|
.ThrowsAsync(new Exception());
|
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
|
|
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceFailsExtraction_ItShouldThrow()
|
|
{
|
|
var mockStream = new Mock<Stream>();
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
_mockFfmpegService.
|
|
Setup(
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
It.IsAny<Stream>(),
|
|
It.IsAny<Stream>()
|
|
)
|
|
)
|
|
.ReturnsAsync(false);
|
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
|
|
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceSucceedsAtExtractingAudio_ItShouldReturnStream()
|
|
{
|
|
var mockStream = new Mock<Stream>();
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
_mockFfmpegService.
|
|
Setup(
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
It.IsAny<Stream>(),
|
|
It.IsAny<Stream>()
|
|
)
|
|
)
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
|
|
|
result.Should().BeAssignableTo<Stream>();
|
|
result.Should().BeOfType<MemoryStream>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExtractMp3FromMp4Async_WhenCalled_ItShouldNotMutatePositionOfPassedStream()
|
|
{
|
|
var text = "hello world";
|
|
var textByte = Encoding.UTF8.GetBytes(text);
|
|
var stream = new MemoryStream(textByte);
|
|
|
|
var positionToRead = 5;
|
|
var buffer = new byte[5];
|
|
await stream.ReadAsync(buffer.AsMemory(0, positionToRead), TestContext.Current.CancellationToken);
|
|
|
|
_mockFfmpegService.
|
|
Setup(
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
It.IsAny<Stream>(),
|
|
It.IsAny<Stream>()
|
|
)
|
|
)
|
|
.ReturnsAsync(true);
|
|
|
|
await _sut.ExtractMp3FromMp4Async(stream, new MemoryStream());
|
|
|
|
stream.Position.Should().Be(positionToRead);
|
|
}
|
|
} |