AudioImagesTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = item.Album ?? string.Empty;
  92. filename += album == null ? item.Id.ToString() + item.DateModified.Ticks : album.Id.ToString() + album.DateModified.Ticks;
  93. var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
  94. var success = true;
  95. if (!ImageCache.ContainsFilePath(path))
  96. {
  97. var semaphore = GetLock(path);
  98. // Acquire a lock
  99. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  100. // Check again
  101. if (!ImageCache.ContainsFilePath(path))
  102. {
  103. try
  104. {
  105. await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
  106. }
  107. catch
  108. {
  109. success = false;
  110. }
  111. finally
  112. {
  113. semaphore.Release();
  114. }
  115. }
  116. else
  117. {
  118. semaphore.Release();
  119. }
  120. }
  121. numComplete++;
  122. double percent = numComplete;
  123. percent /= items.Count;
  124. progress.Report(100 * percent);
  125. if (success)
  126. {
  127. // Image is already in the cache
  128. item.PrimaryImagePath = path;
  129. await _libraryManager.SaveItem(item, cancellationToken).ConfigureAwait(false);
  130. }
  131. }
  132. progress.Report(100);
  133. }
  134. /// <summary>
  135. /// Gets the default triggers.
  136. /// </summary>
  137. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  138. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  139. {
  140. return new ITaskTrigger[]
  141. {
  142. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1) }
  143. };
  144. }
  145. /// <summary>
  146. /// Gets the lock.
  147. /// </summary>
  148. /// <param name="filename">The filename.</param>
  149. /// <returns>System.Object.</returns>
  150. private SemaphoreSlim GetLock(string filename)
  151. {
  152. return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  153. }
  154. }
  155. }