jellyfin/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs

282 lines
9.8 KiB
C#
Raw Normal View History

2019-10-18 11:52:32 +00:00
using System;
using System.Diagnostics;
2019-10-18 11:52:32 +00:00
using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.MediaEncoding.Attachments
{
2019-12-26 22:09:00 +00:00
public class AttachmentExtractor : IAttachmentExtractor, IDisposable
2019-10-18 11:52:32 +00:00
{
private readonly ILogger _logger;
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
private readonly IMediaEncoder _mediaEncoder;
private readonly IMediaSourceManager _mediaSourceManager;
2019-12-26 22:09:00 +00:00
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks =
new ConcurrentDictionary<string, SemaphoreSlim>();
private bool _disposed = false;
2019-10-18 11:52:32 +00:00
public AttachmentExtractor(
2019-11-07 16:55:39 +00:00
ILogger<AttachmentExtractor> logger,
2019-10-18 11:52:32 +00:00
IApplicationPaths appPaths,
IFileSystem fileSystem,
IMediaEncoder mediaEncoder,
IMediaSourceManager mediaSourceManager)
2019-10-18 11:52:32 +00:00
{
2019-11-07 16:55:39 +00:00
_logger = logger;
2019-10-18 11:52:32 +00:00
_appPaths = appPaths;
_fileSystem = fileSystem;
_mediaEncoder = mediaEncoder;
_mediaSourceManager = mediaSourceManager;
}
2019-12-26 22:09:00 +00:00
/// <inheritdoc />
2019-10-18 11:52:32 +00:00
public async Task<(MediaAttachment attachment, Stream stream)> GetAttachment(BaseItem item, string mediaSourceId, int attachmentStreamIndex, CancellationToken cancellationToken)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
2019-10-18 11:52:32 +00:00
if (string.IsNullOrWhiteSpace(mediaSourceId))
{
throw new ArgumentNullException(nameof(mediaSourceId));
}
2020-01-10 23:08:47 +00:00
var mediaSources = await _mediaSourceManager.GetPlaybackMediaSources(item, null, true, false, cancellationToken).ConfigureAwait(false);
var mediaSource = mediaSources
.FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
if (mediaSource == null)
{
throw new ResourceNotFoundException($"MediaSource {mediaSourceId} not found");
}
2019-12-26 22:09:00 +00:00
var mediaAttachment = mediaSource.MediaAttachments
.FirstOrDefault(i => i.Index == attachmentStreamIndex);
if (mediaAttachment == null)
{
throw new ResourceNotFoundException($"MediaSource {mediaSourceId} has no attachment with stream index {attachmentStreamIndex}");
}
2019-12-26 22:09:00 +00:00
2019-10-18 11:52:32 +00:00
var attachmentStream = await GetAttachmentStream(mediaSource, mediaAttachment, cancellationToken)
.ConfigureAwait(false);
return (mediaAttachment, attachmentStream);
}
private async Task<Stream> GetAttachmentStream(
MediaSourceInfo mediaSource,
MediaAttachment mediaAttachment,
CancellationToken cancellationToken)
{
2019-12-26 22:09:00 +00:00
var attachmentPath = await GetReadableFile(mediaSource.Path, mediaSource.Path, mediaSource.Protocol, mediaAttachment, cancellationToken).ConfigureAwait(false);
return File.OpenRead(attachmentPath);
2019-10-18 11:52:32 +00:00
}
2019-12-26 22:09:00 +00:00
private async Task<string> GetReadableFile(
2019-10-18 11:52:32 +00:00
string mediaPath,
2019-12-26 22:09:00 +00:00
string inputFile,
2019-10-18 11:52:32 +00:00
MediaProtocol protocol,
MediaAttachment mediaAttachment,
CancellationToken cancellationToken)
{
var outputPath = GetAttachmentCachePath(mediaPath, protocol, mediaAttachment.Index);
2019-12-26 22:09:00 +00:00
await ExtractAttachment(inputFile, protocol, mediaAttachment.Index, outputPath, cancellationToken)
2019-10-18 11:52:32 +00:00
.ConfigureAwait(false);
return outputPath;
2019-10-18 11:52:32 +00:00
}
private async Task ExtractAttachment(
2019-12-26 22:09:00 +00:00
string inputFile,
2019-10-18 11:52:32 +00:00
MediaProtocol protocol,
int attachmentStreamIndex,
string outputPath,
CancellationToken cancellationToken)
{
2019-12-26 22:09:00 +00:00
var semaphore = _semaphoreLocks.GetOrAdd(outputPath, key => new SemaphoreSlim(1, 1));
2019-10-18 11:52:32 +00:00
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
if (!File.Exists(outputPath))
{
2019-12-26 22:09:00 +00:00
await ExtractAttachmentInternal(
_mediaEncoder.GetInputArgument(new[] { inputFile }, protocol),
attachmentStreamIndex,
outputPath,
cancellationToken).ConfigureAwait(false);
2019-10-18 11:52:32 +00:00
}
}
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(
CultureInfo.InvariantCulture,
"-dump_attachment:{1} {2} -i {0} -t 0 -f null null",
inputPath,
attachmentStreamIndex,
outputPath);
var startInfo = new ProcessStartInfo
2019-10-18 11:52:32 +00:00
{
Arguments = processArgs,
FileName = _mediaEncoder.EncoderPath,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
2019-10-18 11:52:32 +00:00
ErrorDialog = false
};
var process = new Process
{
StartInfo = startInfo
};
2019-10-18 11:52:32 +00:00
_logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments);
2019-12-26 22:09:00 +00:00
process.Start();
2019-10-18 11:52:32 +00:00
var processTcs = new TaskCompletionSource<bool>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => processTcs.TrySetResult(true);
var unregister = cancellationToken.Register(() => processTcs.TrySetResult(process.HasExited));
var ranToCompletion = await processTcs.Task.ConfigureAwait(false);
unregister.Dispose();
2019-10-18 11:52:32 +00:00
if (!ranToCompletion)
{
try
{
_logger.LogWarning("Killing ffmpeg attachment extraction process");
process.Kill();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error killing attachment extraction process");
}
}
2019-12-26 22:09:00 +00:00
2019-10-18 11:52:32 +00:00
var exitCode = ranToCompletion ? process.ExitCode : -1;
process.Dispose();
2020-01-10 23:08:47 +00:00
2019-10-18 11:52:32 +00:00
var failed = false;
if (exitCode != 0)
2019-10-18 11:52:32 +00:00
{
failed = true;
2019-11-04 16:16:57 +00:00
_logger.LogWarning("Deleting extracted attachment {Path} due to failure: {ExitCode}", outputPath, exitCode);
2019-10-18 11:52:32 +00:00
try
{
if (File.Exists(outputPath))
{
_fileSystem.DeleteFile(outputPath);
}
2019-10-18 11:52:32 +00:00
}
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 InvalidOperationException(msg);
2019-10-18 11:52:32 +00:00
}
else
{
2019-11-04 16:39:40 +00:00
_logger.LogInformation("ffmpeg attachment extraction completed for {Path} to {Path}", inputPath, outputPath);
2019-10-18 11:52:32 +00:00
}
}
private string GetAttachmentCachePath(string mediaPath, MediaProtocol protocol, int attachmentStreamIndex)
{
string filename;
2019-10-18 11:52:32 +00:00
if (protocol == MediaProtocol.File)
{
var date = _fileSystem.GetLastWriteTimeUtc(mediaPath);
2019-11-04 16:45:31 +00:00
filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D");
2019-10-18 11:52:32 +00:00
}
else
{
2019-11-04 16:45:31 +00:00
filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D");
2019-10-18 11:52:32 +00:00
}
2019-12-26 22:09:00 +00:00
2019-11-04 16:45:31 +00:00
var prefix = filename.Substring(0, 1);
2019-12-26 22:09:00 +00:00
return Path.Combine(_appPaths.DataPath, "attachments", prefix, filename);
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
2019-10-18 11:52:32 +00:00
}
2019-12-26 22:09:00 +00:00
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
}
_disposed = true;
}
2019-10-18 11:52:32 +00:00
}
}