123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using System;
- using System.Collections.Concurrent;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using MediaBrowser.Common.Configuration;
- using MediaBrowser.Common.Extensions;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.MediaEncoding;
- using MediaBrowser.Model.Diagnostics;
- using MediaBrowser.Model.Dto;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.IO;
- using MediaBrowser.Model.MediaInfo;
- using MediaBrowser.Model.Serialization;
- using Microsoft.Extensions.Logging;
- using UtfUnknown;
- namespace MediaBrowser.MediaEncoding.Attachments
- {
- public class AttachmentExtractor : IAttachmentExtractor
- {
- private readonly ILibraryManager _libraryManager;
- private readonly ILogger _logger;
- private readonly IApplicationPaths _appPaths;
- private readonly IFileSystem _fileSystem;
- private readonly IMediaEncoder _mediaEncoder;
- private readonly IMediaSourceManager _mediaSourceManager;
- private readonly IProcessFactory _processFactory;
- public AttachmentExtractor(
- ILibraryManager libraryManager,
- ILoggerFactory loggerFactory,
- IApplicationPaths appPaths,
- IFileSystem fileSystem,
- IMediaEncoder mediaEncoder,
- IMediaSourceManager mediaSourceManager,
- IProcessFactory processFactory)
- {
- _libraryManager = libraryManager;
- _logger = loggerFactory.CreateLogger(nameof(AttachmentExtractor));
- _appPaths = appPaths;
- _fileSystem = fileSystem;
- _mediaEncoder = mediaEncoder;
- _mediaSourceManager = mediaSourceManager;
- _processFactory = processFactory;
- }
- private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments");
- public async Task<(MediaAttachment attachment, Stream stream)> GetAttachment(BaseItem item, string mediaSourceId, int attachmentStreamIndex, CancellationToken cancellationToken)
- {
- if (item == null)
- {
- throw new ArgumentNullException(nameof(item));
- }
- if (string.IsNullOrWhiteSpace(mediaSourceId))
- {
- throw new ArgumentNullException(nameof(mediaSourceId));
- }
- var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, null, true, false, cancellationToken).ConfigureAwait(false);
- MediaSourceInfo mediaSource;
- MediaAttachment mediaAttachment;
- try
- {
- mediaSource = mediaSources
- .First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
- }
- catch (Exception ex) when (ex is ArgumentNullException || ex is InvalidOperationException)
- {
- throw new ResourceNotFoundException($"MediaSource {mediaSourceId} not found");
- }
- try
- {
- mediaAttachment = mediaSource.MediaAttachments
- .First(i => i.Index == attachmentStreamIndex);
- }
- catch (Exception ex) when (ex is ArgumentNullException || ex is InvalidOperationException)
- {
- throw new ResourceNotFoundException($"MediaSource {mediaSourceId} has no attachment with stream index {attachmentStreamIndex}");
- }
- var attachmentStream = await GetAttachmentStream(mediaSource, mediaAttachment, cancellationToken)
- .ConfigureAwait(false);
- return (mediaAttachment, attachmentStream);
- }
- private async Task<Stream> GetAttachmentStream(
- MediaSourceInfo mediaSource,
- MediaAttachment mediaAttachment,
- CancellationToken cancellationToken)
- {
- var inputFiles = new[] {mediaSource.Path};
- var attachmentPath = await GetReadableFile(mediaSource.Path, inputFiles, mediaSource.Protocol, mediaAttachment, cancellationToken).ConfigureAwait(false);
- var stream = await GetAttachmentStream(attachmentPath, cancellationToken).ConfigureAwait(false);
- return stream;
- }
- private async Task<Stream> GetAttachmentStream(
- string path,
- CancellationToken cancellationToken)
- {
- return File.OpenRead(path);
- }
- private async Task<String> GetReadableFile(
- string mediaPath,
- string[] inputFiles,
- MediaProtocol protocol,
- MediaAttachment mediaAttachment,
- CancellationToken cancellationToken)
- {
- var outputPath = GetAttachmentCachePath(mediaPath, protocol, mediaAttachment.Index);
- await ExtractAttachment(inputFiles, protocol, mediaAttachment.Index, outputPath, cancellationToken)
- .ConfigureAwait(false);
- return outputPath;
- }
- private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks =
- new ConcurrentDictionary<string, SemaphoreSlim>();
- private SemaphoreSlim GetLock(string filename)
- {
- return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
- }
- private async Task ExtractAttachment(
- string[] inputFiles,
- MediaProtocol protocol,
- int attachmentStreamIndex,
- string outputPath,
- CancellationToken cancellationToken)
- {
- var semaphore = GetLock(outputPath);
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
- try
- {
- if (!File.Exists(outputPath))
- {
- await ExtractAttachmentInternal(_mediaEncoder.GetInputArgument(inputFiles, protocol), attachmentStreamIndex, outputPath, cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- semaphore.Release();
- }
- }
- private async Task ExtractAttachmentInternal(
- string inputPath,
- int attachmentStreamIndex,
- string outputPath,
- CancellationToken cancellationToken)
- {
- if (string.IsNullOrEmpty(inputPath))
- {
- throw new ArgumentNullException(nameof(inputPath));
- }
- if (string.IsNullOrEmpty(outputPath))
- {
- throw new ArgumentNullException(nameof(outputPath));
- }
- Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
- var processArgs = string.Format("-dump_attachment:{1} {2} -i {0} -t 0 -f null null", inputPath, attachmentStreamIndex, outputPath);
- var process = _processFactory.Create(new ProcessOptions
- {
- CreateNoWindow = true,
- UseShellExecute = false,
- EnableRaisingEvents = true,
- FileName = _mediaEncoder.EncoderPath,
- Arguments = processArgs,
- IsHidden = true,
- ErrorDialog = false
- });
- _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments);
- try
- {
- process.Start();
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Error starting ffmpeg");
- throw;
- }
- var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false);
- if (!ranToCompletion)
- {
- try
- {
- _logger.LogWarning("Killing ffmpeg attachment extraction process");
- process.Kill();
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Error killing attachment extraction process");
- }
- }
- var exitCode = ranToCompletion ? process.ExitCode : -1;
- process.Dispose();
-
- var failed = false;
- if (exitCode != 0)
- {
- failed = true;
- _logger.LogWarning("Deleting extracted attachment {Path} due to failure: {ExitCode}", outputPath, exitCode);
- try
- {
- _fileSystem.DeleteFile(outputPath);
- }
- catch (FileNotFoundException)
- {
- }
- catch (IOException ex)
- {
- _logger.LogError(ex, "Error deleting extracted attachment {Path}", outputPath);
- }
- }
- else if (!File.Exists(outputPath))
- {
- failed = true;
- }
- if (failed)
- {
- var msg = $"ffmpeg attachment extraction failed for {inputPath} to {outputPath}";
- _logger.LogError(msg);
- throw new Exception(msg);
- }
- else
- {
- _logger.LogInformation("ffmpeg attachment extraction completed for {Path} to {Path}", inputPath, outputPath);
- }
- }
- private string GetAttachmentCachePath(string mediaPath, MediaProtocol protocol, int attachmentStreamIndex)
- {
- String filename;
- if (protocol == MediaProtocol.File)
- {
- var date = _fileSystem.GetLastWriteTimeUtc(mediaPath);
- filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D");
- }
- else
- {
- filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D");
- }
- var prefix = filename.Substring(0, 1);
- return Path.Combine(AttachmentCachePath, prefix, filename);
- }
- }
- }
|