| | | 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 | | chapterManager); |
| | 21 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | 0 | 118 | | public string Name => "Probe Provider"; |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | 0 | 121 | | public int Order => 100; |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 125 | | { |
| | 0 | 126 | | var video = item as Video; |
| | 0 | 127 | | if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso) |
| | | 128 | | { |
| | 0 | 129 | | var path = item.Path; |
| | | 130 | | |
| | 0 | 131 | | if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol) |
| | | 132 | | { |
| | 0 | 133 | | var file = directoryService.GetFile(path); |
| | 0 | 134 | | if (file is not null && item.HasChanged(file.LastWriteTimeUtc)) |
| | | 135 | | { |
| | 0 | 136 | | _logger.LogDebug("Refreshing {ItemPath} due to file system modification.", path); |
| | 0 | 137 | | return true; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | if (video is not null |
| | 0 | 143 | | && item.SupportsLocalMetadata |
| | 0 | 144 | | && !video.IsPlaceHolder) |
| | | 145 | | { |
| | 0 | 146 | | var externalFiles = new HashSet<string>(_subtitleResolver.GetExternalFiles(video, directoryService, fals |
| | 0 | 147 | | if (!new HashSet<string>(video.SubtitleFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 148 | | { |
| | 0 | 149 | | _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path); |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | externalFiles = new HashSet<string>(_audioResolver.GetExternalFiles(video, directoryService, false).Sele |
| | 0 | 154 | | if (!new HashSet<string>(video.AudioFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 155 | | { |
| | 0 | 156 | | _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path); |
| | 0 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | if (item is Audio audio |
| | 0 | 162 | | && item.SupportsLocalMetadata) |
| | | 163 | | { |
| | 0 | 164 | | var externalFiles = new HashSet<string>(_lyricResolver.GetExternalFiles(audio, directoryService, false). |
| | 0 | 165 | | if (!new HashSet<string>(audio.LyricFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 166 | | { |
| | 0 | 167 | | _logger.LogDebug("Refreshing {ItemPath} due to external lyrics change.", item.Path); |
| | 0 | 168 | | return true; |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <inheritdoc /> |
| | | 176 | | public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 177 | | { |
| | 0 | 178 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <inheritdoc /> |
| | | 182 | | public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancel |
| | | 183 | | { |
| | 0 | 184 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <inheritdoc /> |
| | | 188 | | public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 189 | | { |
| | 0 | 190 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <inheritdoc /> |
| | | 194 | | public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 195 | | { |
| | 0 | 196 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <inheritdoc /> |
| | | 200 | | public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 201 | | { |
| | 0 | 202 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <inheritdoc /> |
| | | 206 | | public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 207 | | { |
| | 0 | 208 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <inheritdoc /> |
| | | 212 | | public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancell |
| | | 213 | | { |
| | 0 | 214 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Fetches video information for an item. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="item">The item.</param> |
| | | 221 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 222 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 223 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 224 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 225 | | public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 226 | | where T : Video |
| | | 227 | | { |
| | 0 | 228 | | if (item.IsPlaceHolder) |
| | | 229 | | { |
| | 0 | 230 | | return _cachedTask; |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | if (!item.IsCompleteMedia) |
| | | 234 | | { |
| | 0 | 235 | | return _cachedTask; |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | if (item.IsVirtualItem) |
| | | 239 | | { |
| | 0 | 240 | | return _cachedTask; |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 244 | | { |
| | 0 | 245 | | return _cachedTask; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | if (item.IsShortcut) |
| | | 249 | | { |
| | 0 | 250 | | FetchShortcutInfo(item); |
| | | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | return _videoProber.ProbeVideo(item, options, cancellationToken); |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | private string NormalizeStrmLine(string line) |
| | | 257 | | { |
| | 0 | 258 | | return line.Replace("\t", string.Empty, StringComparison.Ordinal) |
| | 0 | 259 | | .Replace("\r", string.Empty, StringComparison.Ordinal) |
| | 0 | 260 | | .Replace("\n", string.Empty, StringComparison.Ordinal) |
| | 0 | 261 | | .Trim(); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | private void FetchShortcutInfo(BaseItem item) |
| | | 265 | | { |
| | 0 | 266 | | var shortcutPath = File.ReadAllLines(item.Path) |
| | 0 | 267 | | .Select(NormalizeStrmLine) |
| | 0 | 268 | | .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#')); |
| | | 269 | | |
| | 0 | 270 | | if (string.IsNullOrWhiteSpace(shortcutPath)) |
| | | 271 | | { |
| | 0 | 272 | | return; |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | // Only allow remote URLs in .strm files to prevent local file access |
| | 0 | 276 | | if (Uri.TryCreate(shortcutPath, UriKind.Absolute, out var uri) |
| | 0 | 277 | | && (string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) |
| | 0 | 278 | | || string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase) |
| | 0 | 279 | | || string.Equals(uri.Scheme, "rtsp", StringComparison.OrdinalIgnoreCase) |
| | 0 | 280 | | || string.Equals(uri.Scheme, "rtp", StringComparison.OrdinalIgnoreCase))) |
| | | 281 | | { |
| | 0 | 282 | | item.ShortcutPath = shortcutPath; |
| | | 283 | | } |
| | | 284 | | else |
| | | 285 | | { |
| | 0 | 286 | | _logger.LogWarning("Ignoring invalid or non-remote .strm path in {File}: {Path}", item.Path, shortcutPat |
| | | 287 | | } |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Fetches audio information for an item. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <param name="item">The item.</param> |
| | | 294 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 295 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 296 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 297 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 298 | | public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 299 | | where T : Audio |
| | | 300 | | { |
| | 0 | 301 | | if (item.IsVirtualItem) |
| | | 302 | | { |
| | 0 | 303 | | return _cachedTask; |
| | | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 307 | | { |
| | 0 | 308 | | return _cachedTask; |
| | | 309 | | } |
| | | 310 | | |
| | 0 | 311 | | if (item.IsShortcut) |
| | | 312 | | { |
| | 0 | 313 | | FetchShortcutInfo(item); |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return _audioProber.Probe(item, options, cancellationToken); |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | } |