fix: add validation that stream is seekable

This commit is contained in:
Stevan Freeborn
2025-10-10 16:34:43 -05:00
parent e611522c7b
commit 401f03e2f0
2 changed files with 15 additions and 9 deletions
@@ -32,14 +32,9 @@ public sealed class AudioExtractor : IAudioExtractor
throw new ArgumentNullException(nameof(video), "Video stream cannot be null");
}
if (video.CanRead is false)
if (IsVideoStreamUsable(video) is false)
{
throw new ArgumentException("Video stream must be readable", nameof(video));
}
if (video.CanSeek is false)
{
throw new ArgumentException("Video stream must be seekable", nameof(video));
throw new ArgumentException("Stream must be non-null, readable, and seekable", nameof(video));
}
if (audio is null)
@@ -47,9 +42,9 @@ public sealed class AudioExtractor : IAudioExtractor
throw new ArgumentNullException(nameof(audio), "Audio stream cannot be null");
}
if (audio.CanWrite is false)
if (IsAudioStreamUsable(audio) is false)
{
throw new ArgumentException("Audio stream must be writable", nameof(audio));
throw new ArgumentException("Stream must be non-null, writable, and seekable", nameof(audio));
}
var originalPosition = video.Position;
@@ -75,4 +70,14 @@ public sealed class AudioExtractor : IAudioExtractor
video.Position = originalPosition;
}
}
private static bool IsVideoStreamUsable(Stream stream)
{
return stream.CanRead && stream.CanSeek;
}
private static bool IsAudioStreamUsable(Stream stream)
{
return stream.CanWrite && stream.CanSeek;
}
}
@@ -16,6 +16,7 @@ public interface IAudioExtractor
/// <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="ArgumentException">Thrown when the audio stream is not seekable.</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, Stream audio);