diff options
| author | Nils Fürniß <contact@nalsai.de> | 2022-03-09 20:47:03 +0100 |
|---|---|---|
| committer | Nils Fürniß <contact@nalsai.de> | 2022-03-22 19:17:48 +0100 |
| commit | 6904edf68c359cff56b5e6cae9c1d05e2173cd18 (patch) | |
| tree | 82fd38a8c70c256cb14d76e4d3ff0628dfa773c2 /MediaBrowser.MediaEncoding | |
| parent | c512e783b34fe67c523e1f71a930c23219669f14 (diff) | |
add extracting attachments from external subs
Diffstat (limited to 'MediaBrowser.MediaEncoding')
| -rw-r--r-- | MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs | 78 |
1 files changed, 60 insertions, 18 deletions
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)) |
