AudioImagesTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.MediaInfo;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Entities;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Server.Implementations.ScheduledTasks
  15. {
  16. /// <summary>
  17. /// Class AudioImagesTask
  18. /// </summary>
  19. public class AudioImagesTask : IScheduledTask
  20. {
  21. /// <summary>
  22. /// Gets or sets the image cache.
  23. /// </summary>
  24. /// <value>The image cache.</value>
  25. public FileSystemRepository ImageCache { get; set; }
  26. /// <summary>
  27. /// The _library manager
  28. /// </summary>
  29. private readonly ILibraryManager _libraryManager;
  30. /// <summary>
  31. /// The _media encoder
  32. /// </summary>
  33. private readonly IMediaEncoder _mediaEncoder;
  34. /// <summary>
  35. /// The _locks
  36. /// </summary>
  37. private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
  40. /// </summary>
  41. /// <param name="libraryManager">The library manager.</param>
  42. /// <param name="mediaEncoder">The media encoder.</param>
  43. public AudioImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
  44. {
  45. _libraryManager = libraryManager;
  46. _mediaEncoder = mediaEncoder;
  47. ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
  48. }
  49. /// <summary>
  50. /// Gets the name of the task
  51. /// </summary>
  52. /// <value>The name.</value>
  53. public string Name
  54. {
  55. get { return "Audio image extraction"; }
  56. }
  57. /// <summary>
  58. /// Gets the description.
  59. /// </summary>
  60. /// <value>The description.</value>
  61. public string Description
  62. {
  63. get { return "Extracts images from audio files that do not have external images."; }
  64. }
  65. /// <summary>
  66. /// Gets the category.
  67. /// </summary>
  68. /// <value>The category.</value>
  69. public string Category
  70. {
  71. get { return "Library"; }
  72. }
  73. /// <summary>
  74. /// Executes the task
  75. /// </summary>
  76. /// <param name="cancellationToken">The cancellation token.</param>
  77. /// <param name="progress">The progress.</param>
  78. /// <returns>Task.</returns>
  79. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  80. {
  81. var items = _libraryManager.RootFolder.RecursiveChildren
  82. .OfType<Audio>()
  83. .Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
  84. .ToList();
  85. progress.Report(0);
  86. var numComplete = 0;
  87. foreach (var item in items)
  88. {
  89. cancellationToken.ThrowIfCancellationRequested();
  90. var album = item.Parent as MusicAlbum;
  91. var filename = album != null && string.IsNullOrEmpty(item.Album + album.DateModified.Ticks) ? (item.Id.ToString() + item.DateModified.Ticks) : item.Album;
  92. var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
  93. var success = true;
  94. if (!ImageCache.ContainsFilePath(path))
  95. {
  96. var semaphore = GetLock(path);
  97. // Acquire a lock
  98. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  99. // Check again
  100. if (!ImageCache.ContainsFilePath(path))
  101. {
  102. try
  103. {
  104. await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
  105. }
  106. catch
  107. {
  108. success = false;
  109. }
  110. finally
  111. {
  112. semaphore.Release();
  113. }
  114. }
  115. else
  116. {
  117. semaphore.Release();
  118. }
  119. }
  120. numComplete++;
  121. double percent = numComplete;
  122. percent /= items.Count;
  123. progress.Report(100 * percent);
  124. if (success)
  125. {
  126. // Image is already in the cache
  127. item.PrimaryImagePath = path;
  128. await _libraryManager.SaveItem(item, cancellationToken).ConfigureAwait(false);
  129. }
  130. }
  131. progress.Report(100);
  132. }
  133. /// <summary>
  134. /// Gets the default triggers.
  135. /// </summary>
  136. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  137. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  138. {
  139. return new ITaskTrigger[]
  140. {
  141. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1) }
  142. };
  143. }
  144. /// <summary>
  145. /// Gets the lock.
  146. /// </summary>
  147. /// <param name="filename">The filename.</param>
  148. /// <returns>System.Object.</returns>
  149. private SemaphoreSlim GetLock(string filename)
  150. {
  151. return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  152. }
  153. }
  154. }