FFMpegAudioImageProvider.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.Providers.MediaInfo
  9. {
  10. /// <summary>
  11. /// Uses ffmpeg to create video images
  12. /// </summary>
  13. public class FFMpegAudioImageProvider : BaseFFMpegImageProvider<Audio>
  14. {
  15. /// <summary>
  16. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  17. /// </summary>
  18. /// <param name="item">The item.</param>
  19. /// <param name="force">if set to <c>true</c> [force].</param>
  20. /// <param name="cancellationToken">The cancellation token.</param>
  21. /// <returns>Task{System.Boolean}.</returns>
  22. protected override Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  23. {
  24. var audio = (Audio)item;
  25. if (string.IsNullOrEmpty(audio.PrimaryImagePath))
  26. {
  27. // First try to use the parent's image
  28. audio.PrimaryImagePath = audio.ResolveArgs.Parent.PrimaryImagePath;
  29. // If it's still empty see if there's an embedded image
  30. if (string.IsNullOrEmpty(audio.PrimaryImagePath))
  31. {
  32. if (audio.MediaStreams != null && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
  33. {
  34. var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
  35. var path = Kernel.Instance.FFMpegManager.AudioImageCache.GetResourcePath(filename, ".jpg");
  36. if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
  37. {
  38. return ExtractImage(audio, path, cancellationToken);
  39. }
  40. // Image is already in the cache
  41. audio.PrimaryImagePath = path;
  42. }
  43. }
  44. }
  45. SetLastRefreshed(item, DateTime.UtcNow);
  46. return TrueTaskResult;
  47. }
  48. /// <summary>
  49. /// Extracts the image.
  50. /// </summary>
  51. /// <param name="audio">The audio.</param>
  52. /// <param name="path">The path.</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <returns>Task{System.Boolean}.</returns>
  55. private async Task<bool> ExtractImage(Audio audio, string path, CancellationToken cancellationToken)
  56. {
  57. var success = await Kernel.Instance.FFMpegManager.ExtractImage(audio, path, cancellationToken).ConfigureAwait(false);
  58. if (success)
  59. {
  60. audio.PrimaryImagePath = path;
  61. SetLastRefreshed(audio, DateTime.UtcNow);
  62. }
  63. else
  64. {
  65. SetLastRefreshed(audio, DateTime.UtcNow, ProviderRefreshStatus.Failure);
  66. }
  67. return true;
  68. }
  69. }
  70. }