From 6904edf68c359cff56b5e6cae9c1d05e2173cd18 Mon Sep 17 00:00:00 2001 From: Nils Fürniß Date: Wed, 9 Mar 2022 20:47:03 +0100 Subject: add extracting attachments from external subs --- .../Attachments/AttachmentExtractor.cs | 78 +++++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs index 06d20d90e..eb58dd9e2 100644 --- a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs +++ b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs @@ -89,29 +89,63 @@ namespace MediaBrowser.MediaEncoding.Attachments string outputPath, CancellationToken cancellationToken) { - var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); + if (!Directory.Exists(outputPath)) + { + var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); - await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); - try - { - if (!Directory.Exists(outputPath)) + try { await ExtractAllAttachmentsInternal( _mediaEncoder.GetInputArgument(inputFile, mediaSource), outputPath, + false, cancellationToken).ConfigureAwait(false); } + finally + { + semaphore.Release(); + } } - finally + } + + public async Task ExtractAllAttachmentsExternal( + string inputArgument, + string id, + string outputPath, + CancellationToken cancellationToken) + { + if (!File.Exists(Path.Join(outputPath, id))) { - semaphore.Release(); + var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + await ExtractAllAttachmentsInternal( + inputArgument, + outputPath, + true, + cancellationToken).ConfigureAwait(false); + } + finally + { + if (Directory.Exists(outputPath)) + { + File.Create(Path.Join(outputPath, id)); + } + + semaphore.Release(); + } } } private async Task ExtractAllAttachmentsInternal( string inputPath, string outputPath, + bool isExternal, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(inputPath)) @@ -124,11 +158,14 @@ namespace MediaBrowser.MediaEncoding.Attachments throw new ArgumentNullException(nameof(outputPath)); } - Directory.CreateDirectory(outputPath); + if (!Directory.Exists(outputPath)) + { + Directory.CreateDirectory(outputPath); + } var processArgs = string.Format( CultureInfo.InvariantCulture, - "-dump_attachment:t \"\" -i {0} -t 0 -f null null", + "-dump_attachment:t \"\" -y -i {0} -t 0 -f null null", inputPath); int exitCode; @@ -174,19 +211,24 @@ namespace MediaBrowser.MediaEncoding.Attachments if (exitCode != 0) { - failed = true; - - _logger.LogWarning("Deleting extracted attachments {Path} due to failure: {ExitCode}", outputPath, exitCode); - try + if (isExternal && exitCode == 1) { - if (Directory.Exists(outputPath)) + // ffmpeg returns exitCode 1 because there is no video or audio stream + // this can be ignored + } + else + { + failed = true; + + _logger.LogWarning("Deleting extracted attachments {Path} due to failure: {ExitCode}", outputPath, exitCode); + try { Directory.Delete(outputPath); } - } - catch (IOException ex) - { - _logger.LogError(ex, "Error deleting extracted attachments {Path}", outputPath); + catch (IOException ex) + { + _logger.LogError(ex, "Error deleting extracted attachments {Path}", outputPath); + } } } else if (!Directory.Exists(outputPath)) -- cgit v1.2.3 From b4bb82b6d7537dde1ee868b1a1cf9d3652f83866 Mon Sep 17 00:00:00 2001 From: Nils Fürniß Date: Sat, 12 Mar 2022 21:46:29 +0100 Subject: Improve code --- .../Attachments/AttachmentExtractor.cs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs index eb58dd9e2..00aafa126 100644 --- a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs +++ b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs @@ -89,12 +89,12 @@ namespace MediaBrowser.MediaEncoding.Attachments string outputPath, CancellationToken cancellationToken) { - if (!Directory.Exists(outputPath)) - { - var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); + var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); - await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + if (!Directory.Exists(outputPath)) + { try { await ExtractAllAttachmentsInternal( @@ -116,12 +116,12 @@ namespace MediaBrowser.MediaEncoding.Attachments string outputPath, CancellationToken cancellationToken) { - if (!File.Exists(Path.Join(outputPath, id))) - { - var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); + var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1)); - await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + if (!File.Exists(Path.Join(outputPath, id))) + { try { await ExtractAllAttachmentsInternal( @@ -158,10 +158,7 @@ namespace MediaBrowser.MediaEncoding.Attachments throw new ArgumentNullException(nameof(outputPath)); } - if (!Directory.Exists(outputPath)) - { - Directory.CreateDirectory(outputPath); - } + Directory.CreateDirectory(outputPath); var processArgs = string.Format( CultureInfo.InvariantCulture, -- cgit v1.2.3 From d7d36a102aedf1801c8f76a0ead983c5a5a87ccd Mon Sep 17 00:00:00 2001 From: Nils Fürniß Date: Sun, 13 Mar 2022 11:20:09 +0100 Subject: Fix releasing lock --- .../Attachments/AttachmentExtractor.cs | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs index 00aafa126..142571e8f 100644 --- a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs +++ b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs @@ -93,9 +93,9 @@ namespace MediaBrowser.MediaEncoding.Attachments await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); - if (!Directory.Exists(outputPath)) + try { - try + if (!Directory.Exists(outputPath)) { await ExtractAllAttachmentsInternal( _mediaEncoder.GetInputArgument(inputFile, mediaSource), @@ -103,10 +103,10 @@ namespace MediaBrowser.MediaEncoding.Attachments false, cancellationToken).ConfigureAwait(false); } - finally - { - semaphore.Release(); - } + } + finally + { + semaphore.Release(); } } @@ -120,26 +120,26 @@ namespace MediaBrowser.MediaEncoding.Attachments await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); - if (!File.Exists(Path.Join(outputPath, id))) + try { - try + if (!File.Exists(Path.Join(outputPath, id))) { await ExtractAllAttachmentsInternal( inputArgument, outputPath, true, cancellationToken).ConfigureAwait(false); - } - finally - { + if (Directory.Exists(outputPath)) { File.Create(Path.Join(outputPath, id)); } - - semaphore.Release(); } } + finally + { + semaphore.Release(); + } } private async Task ExtractAllAttachmentsInternal( -- cgit v1.2.3