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