| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.IO; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Emby.Naming.Common; |
| | 10 | | using MediaBrowser.Controller.Chapters; |
| | 11 | | using MediaBrowser.Controller.Configuration; |
| | 12 | | using MediaBrowser.Controller.Entities; |
| | 13 | | using MediaBrowser.Controller.Entities.Audio; |
| | 14 | | using MediaBrowser.Controller.Entities.Movies; |
| | 15 | | using MediaBrowser.Controller.Entities.TV; |
| | 16 | | using MediaBrowser.Controller.Library; |
| | 17 | | using MediaBrowser.Controller.Lyrics; |
| | 18 | | using MediaBrowser.Controller.MediaEncoding; |
| | 19 | | using MediaBrowser.Controller.Persistence; |
| | 20 | | using MediaBrowser.Controller.Providers; |
| | 21 | | using MediaBrowser.Controller.Subtitles; |
| | 22 | | using MediaBrowser.Model.Entities; |
| | 23 | | using MediaBrowser.Model.Globalization; |
| | 24 | | using MediaBrowser.Model.IO; |
| | 25 | | using MediaBrowser.Model.MediaInfo; |
| | 26 | | using Microsoft.Extensions.Logging; |
| | 27 | |
|
| | 28 | | namespace MediaBrowser.Providers.MediaInfo |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// The probe provider. |
| | 32 | | /// </summary> |
| | 33 | | public class ProbeProvider : ICustomMetadataProvider<Episode>, |
| | 34 | | ICustomMetadataProvider<MusicVideo>, |
| | 35 | | ICustomMetadataProvider<Movie>, |
| | 36 | | ICustomMetadataProvider<Trailer>, |
| | 37 | | ICustomMetadataProvider<Video>, |
| | 38 | | ICustomMetadataProvider<Audio>, |
| | 39 | | ICustomMetadataProvider<AudioBook>, |
| | 40 | | IHasOrder, |
| | 41 | | IForcedProvider, |
| | 42 | | IPreRefreshProvider, |
| | 43 | | IHasItemChangeMonitor |
| | 44 | | { |
| | 45 | | private readonly ILogger<ProbeProvider> _logger; |
| | 46 | | private readonly AudioResolver _audioResolver; |
| | 47 | | private readonly SubtitleResolver _subtitleResolver; |
| | 48 | | private readonly LyricResolver _lyricResolver; |
| | 49 | | private readonly FFProbeVideoInfo _videoProber; |
| | 50 | | private readonly AudioFileProber _audioProber; |
| 21 | 51 | | private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Initializes a new instance of the <see cref="ProbeProvider"/> class. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param> |
| | 57 | | /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param> |
| | 58 | | /// <param name="blurayExaminer">Instance of the <see cref="IBlurayExaminer"/> interface.</param> |
| | 59 | | /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | 60 | | /// <param name="chapterManager">Instance of the <see cref="IChapterManager"/> interface.</param> |
| | 61 | | /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param> |
| | 62 | | /// <param name="subtitleManager">Instance of the <see cref="ISubtitleManager"/> interface.</param> |
| | 63 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 64 | | /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/>.</param> |
| | 65 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | 66 | | /// <param name="namingOptions">The <see cref="NamingOptions"/>.</param> |
| | 67 | | /// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param> |
| | 68 | | /// <param name="mediaAttachmentRepository">Instance of the <see cref="IMediaAttachmentRepository"/> interface.< |
| | 69 | | /// <param name="mediaStreamRepository">Instance of the <see cref="IMediaStreamRepository"/> interface.</param> |
| | 70 | | public ProbeProvider( |
| | 71 | | IMediaSourceManager mediaSourceManager, |
| | 72 | | IMediaEncoder mediaEncoder, |
| | 73 | | IBlurayExaminer blurayExaminer, |
| | 74 | | ILocalizationManager localization, |
| | 75 | | IChapterManager chapterManager, |
| | 76 | | IServerConfigurationManager config, |
| | 77 | | ISubtitleManager subtitleManager, |
| | 78 | | ILibraryManager libraryManager, |
| | 79 | | IFileSystem fileSystem, |
| | 80 | | ILoggerFactory loggerFactory, |
| | 81 | | NamingOptions namingOptions, |
| | 82 | | ILyricManager lyricManager, |
| | 83 | | IMediaAttachmentRepository mediaAttachmentRepository, |
| | 84 | | IMediaStreamRepository mediaStreamRepository) |
| | 85 | | { |
| 21 | 86 | | _logger = loggerFactory.CreateLogger<ProbeProvider>(); |
| 21 | 87 | | _audioResolver = new AudioResolver(loggerFactory.CreateLogger<AudioResolver>(), localization, mediaEncoder, |
| 21 | 88 | | _subtitleResolver = new SubtitleResolver(loggerFactory.CreateLogger<SubtitleResolver>(), localization, media |
| 21 | 89 | | _lyricResolver = new LyricResolver(loggerFactory.CreateLogger<LyricResolver>(), localization, mediaEncoder, |
| | 90 | |
|
| 21 | 91 | | _videoProber = new FFProbeVideoInfo( |
| 21 | 92 | | loggerFactory.CreateLogger<FFProbeVideoInfo>(), |
| 21 | 93 | | mediaSourceManager, |
| 21 | 94 | | mediaEncoder, |
| 21 | 95 | | blurayExaminer, |
| 21 | 96 | | localization, |
| 21 | 97 | | chapterManager, |
| 21 | 98 | | config, |
| 21 | 99 | | subtitleManager, |
| 21 | 100 | | libraryManager, |
| 21 | 101 | | _audioResolver, |
| 21 | 102 | | _subtitleResolver, |
| 21 | 103 | | mediaAttachmentRepository, |
| 21 | 104 | | mediaStreamRepository); |
| | 105 | |
|
| 21 | 106 | | _audioProber = new AudioFileProber( |
| 21 | 107 | | loggerFactory.CreateLogger<AudioFileProber>(), |
| 21 | 108 | | mediaSourceManager, |
| 21 | 109 | | mediaEncoder, |
| 21 | 110 | | libraryManager, |
| 21 | 111 | | _lyricResolver, |
| 21 | 112 | | lyricManager, |
| 21 | 113 | | mediaStreamRepository); |
| 21 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <inheritdoc /> |
| 0 | 117 | | public string Name => "Probe Provider"; |
| | 118 | |
|
| | 119 | | /// <inheritdoc /> |
| 0 | 120 | | public int Order => 100; |
| | 121 | |
|
| | 122 | | /// <inheritdoc /> |
| | 123 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | 124 | | { |
| 0 | 125 | | var video = item as Video; |
| 0 | 126 | | if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso) |
| | 127 | | { |
| 0 | 128 | | var path = item.Path; |
| | 129 | |
|
| 0 | 130 | | if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol) |
| | 131 | | { |
| 0 | 132 | | var file = directoryService.GetFile(path); |
| 0 | 133 | | if (file is not null && file.LastWriteTimeUtc != item.DateModified && file.Length != item.Size) |
| | 134 | | { |
| 0 | 135 | | _logger.LogDebug("Refreshing {ItemPath} due to file system modification.", path); |
| 0 | 136 | | return true; |
| | 137 | | } |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | if (video is not null |
| 0 | 142 | | && item.SupportsLocalMetadata |
| 0 | 143 | | && !video.IsPlaceHolder) |
| | 144 | | { |
| 0 | 145 | | var externalFiles = new HashSet<string>(_subtitleResolver.GetExternalFiles(video, directoryService, fals |
| 0 | 146 | | if (!new HashSet<string>(video.SubtitleFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | 147 | | { |
| 0 | 148 | | _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path); |
| 0 | 149 | | return true; |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | externalFiles = new HashSet<string>(_audioResolver.GetExternalFiles(video, directoryService, false).Sele |
| 0 | 153 | | if (!new HashSet<string>(video.AudioFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | 154 | | { |
| 0 | 155 | | _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path); |
| 0 | 156 | | return true; |
| | 157 | | } |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | if (item is Audio audio |
| 0 | 161 | | && item.SupportsLocalMetadata) |
| | 162 | | { |
| 0 | 163 | | var externalFiles = new HashSet<string>(_lyricResolver.GetExternalFiles(audio, directoryService, false). |
| 0 | 164 | | if (!new HashSet<string>(audio.LyricFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | 165 | | { |
| 0 | 166 | | _logger.LogDebug("Refreshing {ItemPath} due to external lyrics change.", item.Path); |
| 0 | 167 | | return true; |
| | 168 | | } |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | return false; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | /// <inheritdoc /> |
| | 175 | | public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellat |
| | 176 | | { |
| 0 | 177 | | return FetchVideoInfo(item, options, cancellationToken); |
| | 178 | | } |
| | 179 | |
|
| | 180 | | /// <inheritdoc /> |
| | 181 | | public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancel |
| | 182 | | { |
| 0 | 183 | | return FetchVideoInfo(item, options, cancellationToken); |
| | 184 | | } |
| | 185 | |
|
| | 186 | | /// <inheritdoc /> |
| | 187 | | public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | 188 | | { |
| 0 | 189 | | return FetchVideoInfo(item, options, cancellationToken); |
| | 190 | | } |
| | 191 | |
|
| | 192 | | /// <inheritdoc /> |
| | 193 | | public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellat |
| | 194 | | { |
| 0 | 195 | | return FetchVideoInfo(item, options, cancellationToken); |
| | 196 | | } |
| | 197 | |
|
| | 198 | | /// <inheritdoc /> |
| | 199 | | public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | 200 | | { |
| 0 | 201 | | return FetchVideoInfo(item, options, cancellationToken); |
| | 202 | | } |
| | 203 | |
|
| | 204 | | /// <inheritdoc /> |
| | 205 | | public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | 206 | | { |
| 0 | 207 | | return FetchAudioInfo(item, options, cancellationToken); |
| | 208 | | } |
| | 209 | |
|
| | 210 | | /// <inheritdoc /> |
| | 211 | | public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancell |
| | 212 | | { |
| 0 | 213 | | return FetchAudioInfo(item, options, cancellationToken); |
| | 214 | | } |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Fetches video information for an item. |
| | 218 | | /// </summary> |
| | 219 | | /// <param name="item">The item.</param> |
| | 220 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | 221 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | 222 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | 223 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | 224 | | public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | 225 | | where T : Video |
| | 226 | | { |
| 0 | 227 | | if (item.IsPlaceHolder) |
| | 228 | | { |
| 0 | 229 | | return _cachedTask; |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | if (!item.IsCompleteMedia) |
| | 233 | | { |
| 0 | 234 | | return _cachedTask; |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | if (item.IsVirtualItem) |
| | 238 | | { |
| 0 | 239 | | return _cachedTask; |
| | 240 | | } |
| | 241 | |
|
| 0 | 242 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | 243 | | { |
| 0 | 244 | | return _cachedTask; |
| | 245 | | } |
| | 246 | |
|
| 0 | 247 | | if (item.IsShortcut) |
| | 248 | | { |
| 0 | 249 | | FetchShortcutInfo(item); |
| | 250 | | } |
| | 251 | |
|
| 0 | 252 | | return _videoProber.ProbeVideo(item, options, cancellationToken); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | private string NormalizeStrmLine(string line) |
| | 256 | | { |
| 0 | 257 | | return line.Replace("\t", string.Empty, StringComparison.Ordinal) |
| 0 | 258 | | .Replace("\r", string.Empty, StringComparison.Ordinal) |
| 0 | 259 | | .Replace("\n", string.Empty, StringComparison.Ordinal) |
| 0 | 260 | | .Trim(); |
| | 261 | | } |
| | 262 | |
|
| | 263 | | private void FetchShortcutInfo(BaseItem item) |
| | 264 | | { |
| 0 | 265 | | item.ShortcutPath = File.ReadAllLines(item.Path) |
| 0 | 266 | | .Select(NormalizeStrmLine) |
| 0 | 267 | | .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#')); |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | /// <summary> |
| | 271 | | /// Fetches audio information for an item. |
| | 272 | | /// </summary> |
| | 273 | | /// <param name="item">The item.</param> |
| | 274 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | 275 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | 276 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | 277 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | 278 | | public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | 279 | | where T : Audio |
| | 280 | | { |
| 0 | 281 | | if (item.IsVirtualItem) |
| | 282 | | { |
| 0 | 283 | | return _cachedTask; |
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | 287 | | { |
| 0 | 288 | | return _cachedTask; |
| | 289 | | } |
| | 290 | |
|
| 0 | 291 | | if (item.IsShortcut) |
| | 292 | | { |
| 0 | 293 | | FetchShortcutInfo(item); |
| | 294 | | } |
| | 295 | |
|
| 0 | 296 | | return _audioProber.Probe(item, options, cancellationToken); |
| | 297 | | } |
| | 298 | | } |
| | 299 | | } |