feat: enable processing of large video files over 2GB

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.
This commit is contained in:
Stevan Freeborn
2025-10-10 16:23:02 -05:00
parent 6dcffa6b75
commit e611522c7b
6 changed files with 57 additions and 38 deletions
@@ -25,7 +25,7 @@ public class AudioExtractorTests
[Fact]
public async Task ExtractMp3FromMp4Async_WhenVideoIsNull_ItShouldThrow()
{
var action = async () => await _sut.ExtractMp3FromMp4Async(null!);
var action = async () => await _sut.ExtractMp3FromMp4Async(null!, new MemoryStream());
await action.Should().ThrowAsync<ArgumentNullException>();
}
@@ -36,7 +36,7 @@ public class AudioExtractorTests
var mockStream = new Mock<Stream>();
mockStream.Setup(s => s.CanRead).Returns(false);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
await action.Should().ThrowAsync<ArgumentException>();
}
@@ -48,7 +48,7 @@ public class AudioExtractorTests
mockStream.Setup(s => s.CanRead).Returns(true);
mockStream.Setup(s => s.CanSeek).Returns(false);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
await action.Should().ThrowAsync<ArgumentException>();
}
@@ -69,7 +69,7 @@ public class AudioExtractorTests
)
.ThrowsAsync(new Exception());
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
await action.Should().ThrowAsync<FailedAudioExtractionException>();
}
@@ -90,7 +90,7 @@ public class AudioExtractorTests
)
.ReturnsAsync(false);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object);
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
await action.Should().ThrowAsync<FailedAudioExtractionException>();
}
@@ -111,7 +111,7 @@ public class AudioExtractorTests
)
.ReturnsAsync(true);
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object);
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
result.Should().BeAssignableTo<Stream>();
result.Should().BeOfType<MemoryStream>();
@@ -137,7 +137,7 @@ public class AudioExtractorTests
)
.ReturnsAsync(true);
await _sut.ExtractMp3FromMp4Async(stream);
await _sut.ExtractMp3FromMp4Async(stream, new MemoryStream());
stream.Position.Should().Be(positionToRead);
}