| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using AsyncKeyedLock; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Common.Extensions; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.IO; |
| | | 15 | | using MediaBrowser.Controller.Library; |
| | | 16 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 17 | | using MediaBrowser.MediaEncoding.Encoder; |
| | | 18 | | using MediaBrowser.Model.Dto; |
| | | 19 | | using MediaBrowser.Model.Entities; |
| | | 20 | | using MediaBrowser.Model.IO; |
| | | 21 | | using Microsoft.Extensions.Logging; |
| | | 22 | | |
| | | 23 | | namespace MediaBrowser.MediaEncoding.Attachments |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc cref="IAttachmentExtractor"/> |
| | | 26 | | public sealed class AttachmentExtractor : IAttachmentExtractor, IDisposable |
| | | 27 | | { |
| | | 28 | | private readonly ILogger<AttachmentExtractor> _logger; |
| | | 29 | | private readonly IFileSystem _fileSystem; |
| | | 30 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 31 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 32 | | private readonly IPathManager _pathManager; |
| | | 33 | | |
| | 2 | 34 | | private readonly AsyncKeyedLocker<string> _semaphoreLocks = new(o => |
| | 2 | 35 | | { |
| | 2 | 36 | | o.PoolSize = 20; |
| | 2 | 37 | | o.PoolInitialFill = 1; |
| | 2 | 38 | | }); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="AttachmentExtractor"/> class. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="logger">The <see cref="ILogger{AttachmentExtractor}"/>.</param> |
| | | 44 | | /// <param name="fileSystem">The <see cref="IFileSystem"/>.</param> |
| | | 45 | | /// <param name="mediaEncoder">The <see cref="IMediaEncoder"/>.</param> |
| | | 46 | | /// <param name="mediaSourceManager">The <see cref="IMediaSourceManager"/>.</param> |
| | | 47 | | /// <param name="pathManager">The <see cref="IPathManager"/>.</param> |
| | | 48 | | public AttachmentExtractor( |
| | | 49 | | ILogger<AttachmentExtractor> logger, |
| | | 50 | | IFileSystem fileSystem, |
| | | 51 | | IMediaEncoder mediaEncoder, |
| | | 52 | | IMediaSourceManager mediaSourceManager, |
| | | 53 | | IPathManager pathManager) |
| | | 54 | | { |
| | 2 | 55 | | _logger = logger; |
| | 2 | 56 | | _fileSystem = fileSystem; |
| | 2 | 57 | | _mediaEncoder = mediaEncoder; |
| | 2 | 58 | | _mediaSourceManager = mediaSourceManager; |
| | 2 | 59 | | _pathManager = pathManager; |
| | 2 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public async Task<(MediaAttachment Attachment, Stream Stream)> GetAttachment(BaseItem item, string mediaSourceId |
| | | 64 | | { |
| | 0 | 65 | | ArgumentNullException.ThrowIfNull(item); |
| | | 66 | | |
| | 0 | 67 | | if (string.IsNullOrWhiteSpace(mediaSourceId)) |
| | | 68 | | { |
| | 0 | 69 | | throw new ArgumentNullException(nameof(mediaSourceId)); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | var mediaSources = await _mediaSourceManager.GetPlaybackMediaSources(item, null, true, false, cancellationTo |
| | 0 | 73 | | var mediaSource = mediaSources |
| | 0 | 74 | | .FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)); |
| | 0 | 75 | | if (mediaSource is null) |
| | | 76 | | { |
| | 0 | 77 | | throw new ResourceNotFoundException($"MediaSource {mediaSourceId} not found"); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | var mediaAttachment = mediaSource.MediaAttachments |
| | 0 | 81 | | .FirstOrDefault(i => i.Index == attachmentStreamIndex); |
| | 0 | 82 | | if (mediaAttachment is null) |
| | | 83 | | { |
| | 0 | 84 | | throw new ResourceNotFoundException($"MediaSource {mediaSourceId} has no attachment with stream index {a |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if (string.Equals(mediaAttachment.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)) |
| | | 88 | | { |
| | 0 | 89 | | throw new ResourceNotFoundException($"Attachment with stream index {attachmentStreamIndex} can't be extr |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | var attachmentStream = await GetAttachmentStream(mediaSource, mediaAttachment, cancellationToken) |
| | 0 | 93 | | .ConfigureAwait(false); |
| | | 94 | | |
| | 0 | 95 | | return (mediaAttachment, attachmentStream); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | public async Task ExtractAllAttachments( |
| | | 100 | | string inputFile, |
| | | 101 | | MediaSourceInfo mediaSource, |
| | | 102 | | CancellationToken cancellationToken) |
| | | 103 | | { |
| | 0 | 104 | | var shouldExtractOneByOne = mediaSource.MediaAttachments.Any(a => !string.IsNullOrEmpty(a.FileName) |
| | 0 | 105 | | && !string.Equals(PathHelper.GetSafeLeafFi |
| | 0 | 106 | | if (shouldExtractOneByOne && !inputFile.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)) |
| | | 107 | | { |
| | 0 | 108 | | await ExtractAllAttachmentsIndividuallyInternal( |
| | 0 | 109 | | inputFile, |
| | 0 | 110 | | mediaSource, |
| | 0 | 111 | | cancellationToken).ConfigureAwait(false); |
| | | 112 | | } |
| | | 113 | | else |
| | | 114 | | { |
| | 0 | 115 | | await ExtractAllAttachmentsInternal( |
| | 0 | 116 | | inputFile, |
| | 0 | 117 | | mediaSource, |
| | 0 | 118 | | cancellationToken).ConfigureAwait(false); |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private async Task ExtractAllAttachmentsIndividuallyInternal( |
| | | 123 | | string inputFile, |
| | | 124 | | MediaSourceInfo mediaSource, |
| | | 125 | | CancellationToken cancellationToken) |
| | | 126 | | { |
| | 0 | 127 | | var inputPath = _mediaEncoder.GetInputArgument(inputFile, mediaSource); |
| | | 128 | | |
| | 0 | 129 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | | 130 | | |
| | 0 | 131 | | var outputFolder = _pathManager.GetAttachmentFolderPath(mediaSource.Id); |
| | 0 | 132 | | if (outputFolder is null) |
| | | 133 | | { |
| | 0 | 134 | | _logger.LogDebug("Skipping attachment extraction for input {InputFile}: MediaSource Id is not a GUID.", |
| | 0 | 135 | | return; |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | using (await _semaphoreLocks.LockAsync(outputFolder, cancellationToken).ConfigureAwait(false)) |
| | | 139 | | { |
| | 0 | 140 | | Directory.CreateDirectory(outputFolder); |
| | | 141 | | |
| | 0 | 142 | | var dumpArgs = new StringBuilder(); |
| | 0 | 143 | | var missingPaths = new List<string>(); |
| | 0 | 144 | | foreach (var attachment in mediaSource.MediaAttachments) |
| | | 145 | | { |
| | 0 | 146 | | if (string.Equals(attachment.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)) |
| | | 147 | | { |
| | | 148 | | continue; |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | var indexName = attachment.Index.ToString(CultureInfo.InvariantCulture); |
| | 0 | 152 | | var attachmentPath = _pathManager.GetAttachmentPath(mediaSource.Id, attachment.FileName ?? indexName |
| | 0 | 153 | | ?? _pathManager.GetAttachmentPath(mediaSource.Id, indexName)!; |
| | 0 | 154 | | if (File.Exists(attachmentPath)) |
| | | 155 | | { |
| | | 156 | | continue; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | dumpArgs.AppendFormat( |
| | 0 | 160 | | CultureInfo.InvariantCulture, |
| | 0 | 161 | | "-dump_attachment:{0} \"{1}\" ", |
| | 0 | 162 | | attachment.Index, |
| | 0 | 163 | | EncodingUtils.NormalizePath(attachmentPath)); |
| | 0 | 164 | | missingPaths.Add(attachmentPath); |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if (missingPaths.Count == 0) |
| | | 168 | | { |
| | | 169 | | // Skip extraction if all files already exist |
| | 0 | 170 | | return; |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | var hasVideoOrAudioStream = mediaSource.MediaStreams |
| | 0 | 174 | | .Any(s => s.Type == MediaStreamType.Video || s.Type == MediaStreamType.Audio); |
| | 0 | 175 | | var processArgs = string.Format( |
| | 0 | 176 | | CultureInfo.InvariantCulture, |
| | 0 | 177 | | "{0}{1} -i {2} {3}", |
| | 0 | 178 | | dumpArgs, |
| | 0 | 179 | | inputPath.EndsWith(".concat\"", StringComparison.OrdinalIgnoreCase) ? "-f concat -safe 0" : string.E |
| | 0 | 180 | | inputPath, |
| | 0 | 181 | | hasVideoOrAudioStream ? "-t 0 -f null null" : string.Empty); |
| | | 182 | | |
| | | 183 | | int exitCode; |
| | | 184 | | |
| | 0 | 185 | | using (var process = new Process |
| | 0 | 186 | | { |
| | 0 | 187 | | StartInfo = new ProcessStartInfo |
| | 0 | 188 | | { |
| | 0 | 189 | | Arguments = processArgs, |
| | 0 | 190 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 191 | | UseShellExecute = false, |
| | 0 | 192 | | CreateNoWindow = true, |
| | 0 | 193 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 194 | | ErrorDialog = false |
| | 0 | 195 | | }, |
| | 0 | 196 | | EnableRaisingEvents = true |
| | 0 | 197 | | }) |
| | | 198 | | { |
| | 0 | 199 | | _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments |
| | | 200 | | |
| | 0 | 201 | | process.Start(); |
| | | 202 | | |
| | | 203 | | try |
| | | 204 | | { |
| | 0 | 205 | | await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 206 | | exitCode = process.ExitCode; |
| | 0 | 207 | | } |
| | 0 | 208 | | catch (OperationCanceledException) |
| | | 209 | | { |
| | 0 | 210 | | process.Kill(true); |
| | 0 | 211 | | exitCode = -1; |
| | 0 | 212 | | } |
| | 0 | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | var failed = false; |
| | | 216 | | |
| | 0 | 217 | | if (exitCode != 0 && (hasVideoOrAudioStream || exitCode != 1)) |
| | | 218 | | { |
| | 0 | 219 | | failed = true; |
| | | 220 | | |
| | 0 | 221 | | foreach (var path in missingPaths) |
| | | 222 | | { |
| | 0 | 223 | | if (!File.Exists(path)) |
| | | 224 | | { |
| | | 225 | | continue; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | try |
| | | 229 | | { |
| | 0 | 230 | | _fileSystem.DeleteFile(path); |
| | 0 | 231 | | } |
| | 0 | 232 | | catch (IOException ex) |
| | | 233 | | { |
| | 0 | 234 | | _logger.LogError(ex, "Error deleting extracted attachment {Path}", path); |
| | 0 | 235 | | } |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | if (!failed && missingPaths.Exists(p => !File.Exists(p))) |
| | | 240 | | { |
| | 0 | 241 | | failed = true; |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | if (failed) |
| | | 245 | | { |
| | 0 | 246 | | _logger.LogError("ffmpeg attachment extraction failed for {InputPath} to {OutputPath}", inputPath, o |
| | | 247 | | |
| | 0 | 248 | | throw new InvalidOperationException( |
| | 0 | 249 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg attachment extraction failed for {0} to {1}" |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | _logger.LogInformation("ffmpeg attachment extraction completed for {InputPath} to {OutputPath}", inputPa |
| | 0 | 253 | | } |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | private async Task ExtractAllAttachmentsInternal( |
| | | 257 | | string inputFile, |
| | | 258 | | MediaSourceInfo mediaSource, |
| | | 259 | | CancellationToken cancellationToken) |
| | | 260 | | { |
| | 0 | 261 | | var inputPath = _mediaEncoder.GetInputArgument(inputFile, mediaSource); |
| | | 262 | | |
| | 0 | 263 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | | 264 | | |
| | 0 | 265 | | var outputFolder = _pathManager.GetAttachmentFolderPath(mediaSource.Id); |
| | 0 | 266 | | if (outputFolder is null) |
| | | 267 | | { |
| | 0 | 268 | | _logger.LogDebug("Skipping attachment extraction for input {InputFile}: MediaSource Id is not a GUID.", |
| | 0 | 269 | | return; |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | using (await _semaphoreLocks.LockAsync(outputFolder, cancellationToken).ConfigureAwait(false)) |
| | | 273 | | { |
| | 0 | 274 | | var directory = Directory.CreateDirectory(outputFolder); |
| | 0 | 275 | | var fileNames = directory.GetFiles("*", SearchOption.TopDirectoryOnly).Select(f => f.Name).ToHashSet(); |
| | 0 | 276 | | var missingFiles = mediaSource.MediaAttachments.Where(a => a.FileName is not null && !fileNames.Contains |
| | 0 | 277 | | if (!missingFiles.Any()) |
| | | 278 | | { |
| | | 279 | | // Skip extraction if all files already exist |
| | 0 | 280 | | return; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | // Files without video/audio streams (e.g. MKS subtitle files) don't need a dummy |
| | | 284 | | // output since there are no streams to process. Omit "-t 0 -f null null" so ffmpeg |
| | | 285 | | // doesn't fail trying to open an output with no streams. It will exit with code 1 |
| | | 286 | | // ("at least one output file must be specified") which is expected and harmless |
| | | 287 | | // since we only need the -dump_attachment side effect. |
| | 0 | 288 | | var hasVideoOrAudioStream = mediaSource.MediaStreams |
| | 0 | 289 | | .Any(s => s.Type == MediaStreamType.Video || s.Type == MediaStreamType.Audio); |
| | 0 | 290 | | var processArgs = string.Format( |
| | 0 | 291 | | CultureInfo.InvariantCulture, |
| | 0 | 292 | | "-dump_attachment:t \"\" -y {0} -i {1} {2}", |
| | 0 | 293 | | inputPath.EndsWith(".concat\"", StringComparison.OrdinalIgnoreCase) ? "-f concat -safe 0" : string.E |
| | 0 | 294 | | inputPath, |
| | 0 | 295 | | hasVideoOrAudioStream ? "-t 0 -f null null" : string.Empty); |
| | | 296 | | |
| | | 297 | | int exitCode; |
| | | 298 | | |
| | 0 | 299 | | using (var process = new Process |
| | 0 | 300 | | { |
| | 0 | 301 | | StartInfo = new ProcessStartInfo |
| | 0 | 302 | | { |
| | 0 | 303 | | Arguments = processArgs, |
| | 0 | 304 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 305 | | UseShellExecute = false, |
| | 0 | 306 | | CreateNoWindow = true, |
| | 0 | 307 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 308 | | WorkingDirectory = outputFolder, |
| | 0 | 309 | | ErrorDialog = false |
| | 0 | 310 | | }, |
| | 0 | 311 | | EnableRaisingEvents = true |
| | 0 | 312 | | }) |
| | | 313 | | { |
| | 0 | 314 | | _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments |
| | | 315 | | |
| | 0 | 316 | | process.Start(); |
| | | 317 | | |
| | | 318 | | try |
| | | 319 | | { |
| | 0 | 320 | | await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 321 | | exitCode = process.ExitCode; |
| | 0 | 322 | | } |
| | 0 | 323 | | catch (OperationCanceledException) |
| | | 324 | | { |
| | 0 | 325 | | process.Kill(true); |
| | 0 | 326 | | exitCode = -1; |
| | 0 | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | var failed = false; |
| | | 331 | | |
| | 0 | 332 | | if (exitCode != 0) |
| | | 333 | | { |
| | 0 | 334 | | if (hasVideoOrAudioStream || exitCode != 1) |
| | | 335 | | { |
| | 0 | 336 | | failed = true; |
| | | 337 | | |
| | 0 | 338 | | _logger.LogWarning("Deleting extracted attachments {Path} due to failure: {ExitCode}", outputFol |
| | | 339 | | try |
| | | 340 | | { |
| | 0 | 341 | | Directory.Delete(outputFolder); |
| | 0 | 342 | | } |
| | 0 | 343 | | catch (IOException ex) |
| | | 344 | | { |
| | 0 | 345 | | _logger.LogError(ex, "Error deleting extracted attachments {Path}", outputFolder); |
| | 0 | 346 | | } |
| | | 347 | | } |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | if (!failed && !Directory.Exists(outputFolder)) |
| | | 351 | | { |
| | 0 | 352 | | failed = true; |
| | | 353 | | } |
| | | 354 | | |
| | 0 | 355 | | if (failed) |
| | | 356 | | { |
| | 0 | 357 | | _logger.LogError("ffmpeg attachment extraction failed for {InputPath} to {OutputPath}", inputPath, o |
| | | 358 | | |
| | 0 | 359 | | throw new InvalidOperationException( |
| | 0 | 360 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg attachment extraction failed for {0} to {1}" |
| | | 361 | | } |
| | | 362 | | |
| | 0 | 363 | | _logger.LogInformation("ffmpeg attachment extraction completed for {InputPath} to {OutputPath}", inputPa |
| | 0 | 364 | | } |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | private async Task<Stream> GetAttachmentStream( |
| | | 368 | | MediaSourceInfo mediaSource, |
| | | 369 | | MediaAttachment mediaAttachment, |
| | | 370 | | CancellationToken cancellationToken) |
| | | 371 | | { |
| | 0 | 372 | | var attachmentPath = await ExtractAttachment(mediaSource.Path, mediaSource, mediaAttachment, cancellationTok |
| | 0 | 373 | | .ConfigureAwait(false); |
| | 0 | 374 | | return AsyncFile.OpenRead(attachmentPath); |
| | 0 | 375 | | } |
| | | 376 | | |
| | | 377 | | private async Task<string> ExtractAttachment( |
| | | 378 | | string inputFile, |
| | | 379 | | MediaSourceInfo mediaSource, |
| | | 380 | | MediaAttachment mediaAttachment, |
| | | 381 | | CancellationToken cancellationToken) |
| | | 382 | | { |
| | 0 | 383 | | var attachmentFolderPath = _pathManager.GetAttachmentFolderPath(mediaSource.Id); |
| | 0 | 384 | | if (attachmentFolderPath is null) |
| | | 385 | | { |
| | 0 | 386 | | throw new ResourceNotFoundException($"MediaSource {mediaSource.Id} has no attachment cache (non-GUID Id, |
| | | 387 | | } |
| | | 388 | | |
| | 0 | 389 | | using (await _semaphoreLocks.LockAsync(attachmentFolderPath, cancellationToken).ConfigureAwait(false)) |
| | | 390 | | { |
| | 0 | 391 | | var indexName = mediaAttachment.Index.ToString(CultureInfo.InvariantCulture); |
| | 0 | 392 | | var attachmentPath = _pathManager.GetAttachmentPath(mediaSource.Id, mediaAttachment.FileName ?? indexNam |
| | 0 | 393 | | ?? _pathManager.GetAttachmentPath(mediaSource.Id, indexName)!; |
| | 0 | 394 | | if (!File.Exists(attachmentPath)) |
| | | 395 | | { |
| | 0 | 396 | | await ExtractAttachmentInternal( |
| | 0 | 397 | | _mediaEncoder.GetInputArgument(inputFile, mediaSource), |
| | 0 | 398 | | mediaSource, |
| | 0 | 399 | | mediaAttachment.Index, |
| | 0 | 400 | | attachmentPath, |
| | 0 | 401 | | cancellationToken).ConfigureAwait(false); |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | return attachmentPath; |
| | | 405 | | } |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | private async Task ExtractAttachmentInternal( |
| | | 409 | | string inputPath, |
| | | 410 | | MediaSourceInfo mediaSource, |
| | | 411 | | int attachmentStreamIndex, |
| | | 412 | | string outputPath, |
| | | 413 | | CancellationToken cancellationToken) |
| | | 414 | | { |
| | 0 | 415 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | | 416 | | |
| | 0 | 417 | | ArgumentException.ThrowIfNullOrEmpty(outputPath); |
| | | 418 | | |
| | 0 | 419 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new ArgumentException("Path can't be a |
| | | 420 | | |
| | 0 | 421 | | var hasVideoOrAudioStream = mediaSource.MediaStreams |
| | 0 | 422 | | .Any(s => s.Type == MediaStreamType.Video || s.Type == MediaStreamType.Audio); |
| | 0 | 423 | | var processArgs = string.Format( |
| | 0 | 424 | | CultureInfo.InvariantCulture, |
| | 0 | 425 | | "-dump_attachment:{1} \"{2}\" -i {0} {3}", |
| | 0 | 426 | | inputPath, |
| | 0 | 427 | | attachmentStreamIndex, |
| | 0 | 428 | | EncodingUtils.NormalizePath(outputPath), |
| | 0 | 429 | | hasVideoOrAudioStream ? "-t 0 -f null null" : string.Empty); |
| | | 430 | | |
| | | 431 | | int exitCode; |
| | | 432 | | |
| | 0 | 433 | | using (var process = new Process |
| | 0 | 434 | | { |
| | 0 | 435 | | StartInfo = new ProcessStartInfo |
| | 0 | 436 | | { |
| | 0 | 437 | | Arguments = processArgs, |
| | 0 | 438 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 439 | | UseShellExecute = false, |
| | 0 | 440 | | CreateNoWindow = true, |
| | 0 | 441 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 442 | | ErrorDialog = false |
| | 0 | 443 | | }, |
| | 0 | 444 | | EnableRaisingEvents = true |
| | 0 | 445 | | }) |
| | | 446 | | { |
| | 0 | 447 | | _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments); |
| | | 448 | | |
| | 0 | 449 | | process.Start(); |
| | | 450 | | |
| | | 451 | | try |
| | | 452 | | { |
| | 0 | 453 | | await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 454 | | exitCode = process.ExitCode; |
| | 0 | 455 | | } |
| | 0 | 456 | | catch (OperationCanceledException) |
| | | 457 | | { |
| | 0 | 458 | | process.Kill(true); |
| | 0 | 459 | | exitCode = -1; |
| | 0 | 460 | | } |
| | 0 | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | var failed = false; |
| | | 464 | | |
| | 0 | 465 | | if (exitCode != 0) |
| | | 466 | | { |
| | 0 | 467 | | if (hasVideoOrAudioStream || exitCode != 1) |
| | | 468 | | { |
| | 0 | 469 | | failed = true; |
| | | 470 | | |
| | 0 | 471 | | _logger.LogWarning("Deleting extracted attachment {Path} due to failure: {ExitCode}", outputPath, ex |
| | | 472 | | try |
| | | 473 | | { |
| | 0 | 474 | | if (File.Exists(outputPath)) |
| | | 475 | | { |
| | 0 | 476 | | _fileSystem.DeleteFile(outputPath); |
| | | 477 | | } |
| | 0 | 478 | | } |
| | 0 | 479 | | catch (IOException ex) |
| | | 480 | | { |
| | 0 | 481 | | _logger.LogError(ex, "Error deleting extracted attachment {Path}", outputPath); |
| | 0 | 482 | | } |
| | | 483 | | } |
| | | 484 | | } |
| | | 485 | | |
| | 0 | 486 | | if (!failed && !File.Exists(outputPath)) |
| | | 487 | | { |
| | 0 | 488 | | failed = true; |
| | | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | if (failed) |
| | | 492 | | { |
| | 0 | 493 | | _logger.LogError("ffmpeg attachment extraction failed for {InputPath} to {OutputPath}", inputPath, outpu |
| | | 494 | | |
| | 0 | 495 | | throw new InvalidOperationException( |
| | 0 | 496 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg attachment extraction failed for {0} to {1}", in |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | _logger.LogInformation("ffmpeg attachment extraction completed for {InputPath} to {OutputPath}", inputPath, |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | /// <inheritdoc /> |
| | | 503 | | public void Dispose() |
| | | 504 | | { |
| | 2 | 505 | | _semaphoreLocks.Dispose(); |
| | 2 | 506 | | } |
| | | 507 | | } |
| | | 508 | | } |