Merge pull request #17 from StevanFreeborn/stevanfreeborn/feat/deal-with-larger-streams
feat: deal with larger streams
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
*.mp3 binary
|
||||||
|
*.mp4 binary
|
||||||
Vendored
+27
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch StreamShorts.Console",
|
||||||
|
"program": "${workspaceFolder}/src/StreamShorts.Console/bin/Debug/net9.0/win-x64/StreamShorts.Console.dll",
|
||||||
|
"args": [
|
||||||
|
"${input:filePath}"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/src/StreamShorts.Console",
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
"justMyCode": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"id": "filePath",
|
||||||
|
"type": "promptString",
|
||||||
|
"description": "Enter the path to the video file",
|
||||||
|
"default": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Vendored
+17
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/StreamShorts.sln",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -69,19 +69,28 @@ internal sealed class DefaultCommand(
|
|||||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||||
{
|
{
|
||||||
_console.MarkupLine($"[blue]Processing stream:[/] {settings.Stream.EscapeMarkup()}");
|
_console.MarkupLine($"[blue]Processing stream:[/] {settings.Stream.EscapeMarkup()}");
|
||||||
var videoStream = _fileSystem.File.OpenRead(settings.Stream);
|
var videoStream = (FileStream)_fileSystem.File.OpenRead(settings.Stream);
|
||||||
|
|
||||||
Stream? audioStream = null;
|
var now = _timeProvider.GetUtcNow();
|
||||||
|
var inputFileName = _fileSystem.Path.GetFileNameWithoutExtension(settings.Stream);
|
||||||
|
var baseDirectory = ValidateAndGetBaseOutputDirectory();
|
||||||
|
var artifactsOutputDirectory = $"{now:yyyy_MM_dd_HH_mm_ss}_{inputFileName}";
|
||||||
|
var outputDirectoryPath = _fileSystem.Path.Combine(baseDirectory, artifactsOutputDirectory);
|
||||||
|
|
||||||
|
_fileSystem.Directory.CreateDirectory(outputDirectoryPath);
|
||||||
|
|
||||||
|
using var audioStream = new FileStream(
|
||||||
|
_fileSystem.Path.Combine(outputDirectoryPath, $"{inputFileName}.mp3"),
|
||||||
|
FileMode.Create,
|
||||||
|
FileAccess.ReadWrite,
|
||||||
|
FileShare.ReadWrite,
|
||||||
|
4096,
|
||||||
|
FileOptions.Asynchronous
|
||||||
|
);
|
||||||
|
|
||||||
await _console.Status()
|
await _console.Status()
|
||||||
.Spinner(Spinner.Known.Dots)
|
.Spinner(Spinner.Known.Dots)
|
||||||
.StartAsync("Extracting audio...", async _ => audioStream = await _audioExtractor.ExtractMp3FromMp4Async(videoStream));
|
.StartAsync("Extracting audio...", async _ => await _audioExtractor.ExtractMp3FromMp4Async(videoStream, audioStream));
|
||||||
|
|
||||||
if (audioStream is null)
|
|
||||||
{
|
|
||||||
_console.MarkupLine("[red]Failed[/] to extract audio from the stream.");
|
|
||||||
return (int)ExitCode.FailedToExtractAudio;
|
|
||||||
}
|
|
||||||
|
|
||||||
_console.MarkupLine($"[blue]Audio extracted[/] [green]successfully![/]");
|
_console.MarkupLine($"[blue]Audio extracted[/] [green]successfully![/]");
|
||||||
|
|
||||||
@@ -101,14 +110,6 @@ internal sealed class DefaultCommand(
|
|||||||
|
|
||||||
_console.MarkupLine($"[blue]Transcription completed[/] [green]successfully![/]");
|
_console.MarkupLine($"[blue]Transcription completed[/] [green]successfully![/]");
|
||||||
|
|
||||||
var now = _timeProvider.GetUtcNow();
|
|
||||||
var inputFileName = _fileSystem.Path.GetFileNameWithoutExtension(settings.Stream);
|
|
||||||
var baseDirectory = ValidateAndGetBaseOutputDirectory();
|
|
||||||
var artifactsOutputDirectory = $"{now:yyyy_MM_dd_HH_mm_ss}_{inputFileName}";
|
|
||||||
var outputDirectoryPath = _fileSystem.Path.Combine(baseDirectory, artifactsOutputDirectory);
|
|
||||||
|
|
||||||
_fileSystem.Directory.CreateDirectory(outputDirectoryPath);
|
|
||||||
|
|
||||||
await _fileSystem.File.WriteAllTextAsync(
|
await _fileSystem.File.WriteAllTextAsync(
|
||||||
_fileSystem.Path.Combine(outputDirectoryPath, "transcription.txt"),
|
_fileSystem.Path.Combine(outputDirectoryPath, "transcription.txt"),
|
||||||
string.Join(Environment.NewLine, transcriptionSegments)
|
string.Join(Environment.NewLine, transcriptionSegments)
|
||||||
@@ -192,7 +193,6 @@ internal sealed class DefaultCommand(
|
|||||||
|
|
||||||
private enum ExitCode
|
private enum ExitCode
|
||||||
{
|
{
|
||||||
FailedToExtractAudio,
|
|
||||||
FailedToAnalyzeTranscript,
|
FailedToAnalyzeTranscript,
|
||||||
SuccessFullyProcessedStream,
|
SuccessFullyProcessedStream,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,42 +25,41 @@ public sealed class AudioExtractor : IAudioExtractor
|
|||||||
_videoService = videoService ?? throw new ArgumentNullException(nameof(videoService), $"{nameof(videoService)} cannot be null");
|
_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)
|
if (video is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(video), "Video stream cannot be null");
|
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));
|
throw new ArgumentException("Stream must be readable and seekable", nameof(video));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (video.CanSeek is false)
|
if (audio is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Video stream must be seekable", nameof(video));
|
throw new ArgumentNullException(nameof(audio), "Audio stream cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsAudioStreamUsable(audio) is false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Stream must be writable and seekable", nameof(audio));
|
||||||
}
|
}
|
||||||
|
|
||||||
var originalPosition = video.Position;
|
var originalPosition = video.Position;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mp3Stream = new MemoryStream();
|
var wasExtracted = await _videoService.ExtractAudioFromVideoAsync(video, audio).ConfigureAwait(false);
|
||||||
using var mp4Stream = new MemoryStream();
|
|
||||||
|
|
||||||
await video.CopyToAsync(mp4Stream).ConfigureAwait(false);
|
|
||||||
mp4Stream.Position = 0;
|
|
||||||
|
|
||||||
var wasExtracted = await _videoService.ExtractAudioFromVideoAsync(mp4Stream, mp3Stream).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (wasExtracted is false)
|
if (wasExtracted is false)
|
||||||
{
|
{
|
||||||
throw new FailedAudioExtractionException("Failed to extract audio from the video stream.");
|
throw new FailedAudioExtractionException("Failed to extract audio from the video stream.");
|
||||||
}
|
}
|
||||||
|
|
||||||
mp3Stream.Position = 0;
|
audio.Position = 0;
|
||||||
return mp3Stream;
|
return audio;
|
||||||
}
|
}
|
||||||
catch (Exception e) when (e is not FailedAudioExtractionException)
|
catch (Exception e) when (e is not FailedAudioExtractionException)
|
||||||
{
|
{
|
||||||
@@ -71,4 +70,14 @@ public sealed class AudioExtractor : IAudioExtractor
|
|||||||
video.Position = originalPosition;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,15 @@ public interface IAudioExtractor
|
|||||||
/// Extracts MP3 audio from an MP4 video stream.
|
/// Extracts MP3 audio from an MP4 video stream.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="video">The input video stream.</param>
|
/// <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>
|
/// <returns>A stream containing the extracted MP3 audio.</returns>
|
||||||
/// <exception cref="ArgumentNullException">Thrown when the video stream is null.</exception>
|
/// <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 readable.</exception>
|
||||||
/// <exception cref="ArgumentException">Thrown when the video stream is not seekable.</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="ArgumentException">Thrown when the audio stream is not seekable.</exception>
|
||||||
/// <exception cref="FailedAudioExtractionException">Thrown when the audio extraction fails.</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>
|
/// <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)
|
public async Task<bool> ExtractAudioFromVideoAsync(Stream video, Stream audio)
|
||||||
{
|
{
|
||||||
return await FFMpegArguments
|
var inputArguments = video is FileStream videoFileStream
|
||||||
.FromPipeInput(new StreamPipeSource(video))
|
? FFMpegArguments.FromFileInput(videoFileStream.Name)
|
||||||
.OutputToPipe(
|
: FFMpegArguments.FromPipeInput(new StreamPipeSource(video));
|
||||||
new StreamPipeSink(audio),
|
|
||||||
static o => o.DisableChannel(Channel.Video).ForceFormat("mp3")
|
Action<FFMpegArgumentOptions> arguments = static o => o.DisableChannel(Channel.Video).ForceFormat("mp3");
|
||||||
)
|
|
||||||
.ProcessAsynchronously()
|
var processor = audio is FileStream audioFileStream
|
||||||
.ConfigureAwait(false);
|
? inputArguments.OutputToFile(audioFileStream.Name, addArguments: arguments)
|
||||||
|
: inputArguments.OutputToPipe(new StreamPipeSink(audio), addArguments: arguments);
|
||||||
|
|
||||||
|
return await processor.ProcessAsynchronously().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateClipFromVideoAsync(
|
public async Task CreateClipFromVideoAsync(
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
using NAudio.Wave;
|
using NAudio.Wave;
|
||||||
|
|
||||||
namespace StreamShorts.Library.Media;
|
namespace StreamShorts.Library.Media;
|
||||||
|
|||||||
@@ -9,13 +9,15 @@ public class AudioExtractorTests
|
|||||||
{
|
{
|
||||||
using var testVideo = TestData.GetTestVideo();
|
using var testVideo = TestData.GetTestVideo();
|
||||||
using var extractedAudio = TestData.GetExtractedAudio();
|
using var extractedAudio = TestData.GetExtractedAudio();
|
||||||
|
using var audioStream = new MemoryStream();
|
||||||
|
|
||||||
var result = await _sut.ExtractMp3FromMp4Async(testVideo);
|
var result = await _sut.ExtractMp3FromMp4Async(testVideo, audioStream);
|
||||||
|
|
||||||
var audioBytes = await ConvertStreamToBytesAsync(extractedAudio);
|
var audioBytes = await ConvertStreamToBytesAsync(extractedAudio);
|
||||||
var resultBytes = await ConvertStreamToBytesAsync(result);
|
var resultBytes = await ConvertStreamToBytesAsync(result);
|
||||||
|
|
||||||
resultBytes.Should().Equal(audioBytes);
|
resultBytes.Should().Equal(audioBytes);
|
||||||
|
result.Should().BeSameAs(audioStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<byte[]> ConvertStreamToBytesAsync(Stream stream)
|
private static async Task<byte[]> ConvertStreamToBytesAsync(Stream stream)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class AudioExtractorTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task ExtractMp3FromMp4Async_WhenVideoIsNull_ItShouldThrow()
|
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>();
|
await action.Should().ThrowAsync<ArgumentNullException>();
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ public class AudioExtractorTests
|
|||||||
var mockStream = new Mock<Stream>();
|
var mockStream = new Mock<Stream>();
|
||||||
mockStream.Setup(s => s.CanRead).Returns(false);
|
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>();
|
await action.Should().ThrowAsync<ArgumentException>();
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ public class AudioExtractorTests
|
|||||||
mockStream.Setup(s => s.CanRead).Returns(true);
|
mockStream.Setup(s => s.CanRead).Returns(true);
|
||||||
mockStream.Setup(s => s.CanSeek).Returns(false);
|
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>();
|
await action.Should().ThrowAsync<ArgumentException>();
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ public class AudioExtractorTests
|
|||||||
)
|
)
|
||||||
.ThrowsAsync(new Exception());
|
.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>();
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ public class AudioExtractorTests
|
|||||||
)
|
)
|
||||||
.ReturnsAsync(false);
|
.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>();
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ public class AudioExtractorTests
|
|||||||
)
|
)
|
||||||
.ReturnsAsync(true);
|
.ReturnsAsync(true);
|
||||||
|
|
||||||
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object);
|
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
||||||
|
|
||||||
result.Should().BeAssignableTo<Stream>();
|
result.Should().BeAssignableTo<Stream>();
|
||||||
result.Should().BeOfType<MemoryStream>();
|
result.Should().BeOfType<MemoryStream>();
|
||||||
@@ -137,7 +137,7 @@ public class AudioExtractorTests
|
|||||||
)
|
)
|
||||||
.ReturnsAsync(true);
|
.ReturnsAsync(true);
|
||||||
|
|
||||||
await _sut.ExtractMp3FromMp4Async(stream);
|
await _sut.ExtractMp3FromMp4Async(stream, new MemoryStream());
|
||||||
|
|
||||||
stream.Position.Should().Be(positionToRead);
|
stream.Position.Should().Be(positionToRead);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user