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:
@@ -25,7 +25,7 @@ public sealed class AudioExtractor : IAudioExtractor
|
||||
_videoService = videoService ?? throw new ArgumentNullException(nameof(videoService), $"{nameof(videoService)} cannot be null");
|
||||
}
|
||||
|
||||
public async Task<Stream> ExtractMp3FromMp4Async(Stream video)
|
||||
public async Task<Stream> ExtractMp3FromMp4Async(Stream video, Stream audio)
|
||||
{
|
||||
if (video is null)
|
||||
{
|
||||
@@ -42,25 +42,29 @@ public sealed class AudioExtractor : IAudioExtractor
|
||||
throw new ArgumentException("Video stream must be seekable", nameof(video));
|
||||
}
|
||||
|
||||
if (audio is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(audio), "Audio stream cannot be null");
|
||||
}
|
||||
|
||||
if (audio.CanWrite is false)
|
||||
{
|
||||
throw new ArgumentException("Audio stream must be writable", nameof(audio));
|
||||
}
|
||||
|
||||
var originalPosition = video.Position;
|
||||
|
||||
try
|
||||
{
|
||||
var mp3Stream = new MemoryStream();
|
||||
using var mp4Stream = new MemoryStream();
|
||||
|
||||
await video.CopyToAsync(mp4Stream).ConfigureAwait(false);
|
||||
mp4Stream.Position = 0;
|
||||
|
||||
var wasExtracted = await _videoService.ExtractAudioFromVideoAsync(mp4Stream, mp3Stream).ConfigureAwait(false);
|
||||
var wasExtracted = await _videoService.ExtractAudioFromVideoAsync(video, audio).ConfigureAwait(false);
|
||||
|
||||
if (wasExtracted is false)
|
||||
{
|
||||
throw new FailedAudioExtractionException("Failed to extract audio from the video stream.");
|
||||
}
|
||||
|
||||
mp3Stream.Position = 0;
|
||||
return mp3Stream;
|
||||
audio.Position = 0;
|
||||
return audio;
|
||||
}
|
||||
catch (Exception e) when (e is not FailedAudioExtractionException)
|
||||
{
|
||||
|
||||
@@ -9,11 +9,14 @@ public interface IAudioExtractor
|
||||
/// Extracts MP3 audio from an MP4 video stream.
|
||||
/// </summary>
|
||||
/// <param name="video">The input video stream.</param>
|
||||
/// <param name="audio">The output audio stream where the extracted MP3 will be written.</param>
|
||||
/// <returns>A stream containing the extracted MP3 audio.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the video stream is null.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown when the video stream is not readable.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown when the video stream is not seekable.</exception>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the audio stream is null.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown when the audio stream is not writable.</exception>
|
||||
/// <exception cref="FailedAudioExtractionException">Thrown when the audio extraction fails.</exception>
|
||||
/// <remarks>The method will preserve the passed video stream's data and position.</remarks>
|
||||
Task<Stream> ExtractMp3FromMp4Async(Stream video);
|
||||
Task<Stream> ExtractMp3FromMp4Async(Stream video, Stream audio);
|
||||
}
|
||||
@@ -12,14 +12,17 @@ internal sealed class FFMpegService : IVideoService
|
||||
{
|
||||
public async Task<bool> ExtractAudioFromVideoAsync(Stream video, Stream audio)
|
||||
{
|
||||
return await FFMpegArguments
|
||||
.FromPipeInput(new StreamPipeSource(video))
|
||||
.OutputToPipe(
|
||||
new StreamPipeSink(audio),
|
||||
static o => o.DisableChannel(Channel.Video).ForceFormat("mp3")
|
||||
)
|
||||
.ProcessAsynchronously()
|
||||
.ConfigureAwait(false);
|
||||
var inputArguments = video is FileStream videoFileStream
|
||||
? FFMpegArguments.FromFileInput(videoFileStream.Name)
|
||||
: FFMpegArguments.FromPipeInput(new StreamPipeSource(video));
|
||||
|
||||
Action<FFMpegArgumentOptions> arguments = static o => o.DisableChannel(Channel.Video).ForceFormat("mp3");
|
||||
|
||||
var processor = audio is FileStream audioFileStream
|
||||
? inputArguments.OutputToFile(audioFileStream.Name, addArguments: arguments)
|
||||
: inputArguments.OutputToPipe(new StreamPipeSink(audio), addArguments: arguments);
|
||||
|
||||
return await processor.ProcessAsynchronously().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task CreateClipFromVideoAsync(
|
||||
|
||||
Reference in New Issue
Block a user