| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Emby.Naming.Common; |
| | 8 | | using Emby.Naming.ExternalFiles; |
| | 9 | | using MediaBrowser.Controller.Entities; |
| | 10 | | using MediaBrowser.Controller.Entities.Audio; |
| | 11 | | using MediaBrowser.Controller.MediaEncoding; |
| | 12 | | using MediaBrowser.Controller.Providers; |
| | 13 | | using MediaBrowser.Model.Dlna; |
| | 14 | | using MediaBrowser.Model.Dto; |
| | 15 | | using MediaBrowser.Model.Entities; |
| | 16 | | using MediaBrowser.Model.Globalization; |
| | 17 | | using MediaBrowser.Model.IO; |
| | 18 | | using MediaBrowser.Model.MediaInfo; |
| | 19 | | using Microsoft.Extensions.Logging; |
| | 20 | |
|
| | 21 | | namespace MediaBrowser.Providers.MediaInfo |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// Resolves external files for <see cref="Video"/>. |
| | 25 | | /// </summary> |
| | 26 | | public abstract class MediaInfoResolver |
| | 27 | | { |
| | 28 | | /// <summary> |
| | 29 | | /// The <see cref="ExternalPathParser"/> instance. |
| | 30 | | /// </summary> |
| | 31 | | private readonly ExternalPathParser _externalPathParser; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The <see cref="IMediaEncoder"/> instance. |
| | 35 | | /// </summary> |
| | 36 | | private readonly IMediaEncoder _mediaEncoder; |
| | 37 | |
|
| | 38 | | private readonly ILogger _logger; |
| | 39 | | private readonly IFileSystem _fileSystem; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The <see cref="NamingOptions"/> instance. |
| | 43 | | /// </summary> |
| | 44 | | private readonly NamingOptions _namingOptions; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The <see cref="DlnaProfileType"/> of the files this resolver should resolve. |
| | 48 | | /// </summary> |
| | 49 | | private readonly DlnaProfileType _type; |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Initializes a new instance of the <see cref="MediaInfoResolver"/> class. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="logger">The logger.</param> |
| | 55 | | /// <param name="localizationManager">The localization manager.</param> |
| | 56 | | /// <param name="mediaEncoder">The media encoder.</param> |
| | 57 | | /// <param name="fileSystem">The file system.</param> |
| | 58 | | /// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFl |
| | 59 | | /// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param> |
| | 60 | | protected MediaInfoResolver( |
| | 61 | | ILogger logger, |
| | 62 | | ILocalizationManager localizationManager, |
| | 63 | | IMediaEncoder mediaEncoder, |
| | 64 | | IFileSystem fileSystem, |
| | 65 | | NamingOptions namingOptions, |
| | 66 | | DlnaProfileType type) |
| | 67 | | { |
| 125 | 68 | | _logger = logger; |
| 125 | 69 | | _mediaEncoder = mediaEncoder; |
| 125 | 70 | | _fileSystem = fileSystem; |
| 125 | 71 | | _namingOptions = namingOptions; |
| 125 | 72 | | _type = type; |
| 125 | 73 | | _externalPathParser = new ExternalPathParser(namingOptions, localizationManager, _type); |
| 125 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Retrieves the external streams for the provided video. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="video">The <see cref="Video"/> object to search external streams for.</param> |
| | 80 | | /// <param name="startIndex">The stream index to start adding external streams at.</param> |
| | 81 | | /// <param name="directoryService">The directory service to search for files.</param> |
| | 82 | | /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param> |
| | 83 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 84 | | /// <returns>The external streams located.</returns> |
| | 85 | | public async Task<IReadOnlyList<MediaStream>> GetExternalStreamsAsync( |
| | 86 | | Video video, |
| | 87 | | int startIndex, |
| | 88 | | IDirectoryService directoryService, |
| | 89 | | bool clearCache, |
| | 90 | | CancellationToken cancellationToken) |
| | 91 | | { |
| | 92 | | if (!video.IsFileProtocol) |
| | 93 | | { |
| | 94 | | return Array.Empty<MediaStream>(); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | var pathInfos = GetExternalFiles(video, directoryService, clearCache); |
| | 98 | |
|
| | 99 | | if (!pathInfos.Any()) |
| | 100 | | { |
| | 101 | | return Array.Empty<MediaStream>(); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | var mediaStreams = new List<MediaStream>(); |
| | 105 | |
|
| | 106 | | foreach (var pathInfo in pathInfos) |
| | 107 | | { |
| | 108 | | if (!pathInfo.Path.AsSpan().EndsWith(".strm", StringComparison.OrdinalIgnoreCase)) |
| | 109 | | { |
| | 110 | | try |
| | 111 | | { |
| | 112 | | var mediaInfo = await GetMediaInfo(pathInfo.Path, _type, cancellationToken).ConfigureAwait(false |
| | 113 | |
|
| | 114 | | if (mediaInfo.MediaStreams.Count == 1) |
| | 115 | | { |
| | 116 | | MediaStream mediaStream = mediaInfo.MediaStreams[0]; |
| | 117 | |
|
| | 118 | | if ((mediaStream.Type == MediaStreamType.Audio && _type == DlnaProfileType.Audio) |
| | 119 | | || (mediaStream.Type == MediaStreamType.Subtitle && _type == DlnaProfileType.Subtitle)) |
| | 120 | | { |
| | 121 | | mediaStream.Index = startIndex++; |
| | 122 | | mediaStream.IsDefault = pathInfo.IsDefault; |
| | 123 | | mediaStream.IsForced = pathInfo.IsForced || mediaStream.IsForced; |
| | 124 | | mediaStream.IsHearingImpaired = pathInfo.IsHearingImpaired || mediaStream.IsHearingImpai |
| | 125 | |
|
| | 126 | | mediaStreams.Add(MergeMetadata(mediaStream, pathInfo)); |
| | 127 | | } |
| | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| | 131 | | foreach (MediaStream mediaStream in mediaInfo.MediaStreams) |
| | 132 | | { |
| | 133 | | if ((mediaStream.Type == MediaStreamType.Audio && _type == DlnaProfileType.Audio) |
| | 134 | | || (mediaStream.Type == MediaStreamType.Subtitle && _type == DlnaProfileType.Subtitl |
| | 135 | | { |
| | 136 | | mediaStream.Index = startIndex++; |
| | 137 | |
|
| | 138 | | mediaStreams.Add(MergeMetadata(mediaStream, pathInfo)); |
| | 139 | | } |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |
| | 143 | | catch (Exception ex) |
| | 144 | | { |
| | 145 | | _logger.LogError(ex, "Error getting external streams from {Path}", pathInfo.Path); |
| | 146 | |
|
| | 147 | | continue; |
| | 148 | | } |
| | 149 | | } |
| | 150 | | } |
| | 151 | |
|
| | 152 | | return mediaStreams; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Retrieves the external streams for the provided audio. |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="audio">The <see cref="Audio"/> object to search external streams for.</param> |
| | 159 | | /// <param name="startIndex">The stream index to start adding external streams at.</param> |
| | 160 | | /// <param name="directoryService">The directory service to search for files.</param> |
| | 161 | | /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param> |
| | 162 | | /// <returns>The external streams located.</returns> |
| | 163 | | public IReadOnlyList<MediaStream> GetExternalStreams( |
| | 164 | | Audio audio, |
| | 165 | | int startIndex, |
| | 166 | | IDirectoryService directoryService, |
| | 167 | | bool clearCache) |
| | 168 | | { |
| 0 | 169 | | if (!audio.IsFileProtocol) |
| | 170 | | { |
| 0 | 171 | | return Array.Empty<MediaStream>(); |
| | 172 | | } |
| | 173 | |
|
| 0 | 174 | | var pathInfos = GetExternalFiles(audio, directoryService, clearCache); |
| | 175 | |
|
| 0 | 176 | | if (pathInfos.Count == 0) |
| | 177 | | { |
| 0 | 178 | | return Array.Empty<MediaStream>(); |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | var mediaStreams = new MediaStream[pathInfos.Count]; |
| | 182 | |
|
| 0 | 183 | | for (var i = 0; i < pathInfos.Count; i++) |
| | 184 | | { |
| 0 | 185 | | mediaStreams[i] = new MediaStream |
| 0 | 186 | | { |
| 0 | 187 | | Type = MediaStreamType.Lyric, |
| 0 | 188 | | Path = pathInfos[i].Path, |
| 0 | 189 | | Language = pathInfos[i].Language, |
| 0 | 190 | | Index = startIndex++ |
| 0 | 191 | | }; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | return mediaStreams; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | /// <summary> |
| | 198 | | /// Returns the external file infos for the given video. |
| | 199 | | /// </summary> |
| | 200 | | /// <param name="video">The <see cref="Video"/> object to search external files for.</param> |
| | 201 | | /// <param name="directoryService">The directory service to search for files.</param> |
| | 202 | | /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param> |
| | 203 | | /// <returns>The external file paths located.</returns> |
| | 204 | | public IReadOnlyList<ExternalPathParserResult> GetExternalFiles( |
| | 205 | | Video video, |
| | 206 | | IDirectoryService directoryService, |
| | 207 | | bool clearCache) |
| | 208 | | { |
| 32 | 209 | | if (!video.IsFileProtocol) |
| | 210 | | { |
| 1 | 211 | | return Array.Empty<ExternalPathParserResult>(); |
| | 212 | | } |
| | 213 | |
|
| | 214 | | // Check if video folder exists |
| 31 | 215 | | string folder = video.ContainingFolderPath; |
| 31 | 216 | | if (!_fileSystem.DirectoryExists(folder)) |
| | 217 | | { |
| 2 | 218 | | return Array.Empty<ExternalPathParserResult>(); |
| | 219 | | } |
| | 220 | |
|
| 29 | 221 | | var files = directoryService.GetFilePaths(folder, clearCache, true).ToList(); |
| 29 | 222 | | files.Remove(video.Path); |
| 29 | 223 | | var internalMetadataPath = video.GetInternalMetadataPath(); |
| 29 | 224 | | if (_fileSystem.DirectoryExists(internalMetadataPath)) |
| | 225 | | { |
| 28 | 226 | | files.AddRange(directoryService.GetFilePaths(internalMetadataPath, clearCache, true)); |
| | 227 | | } |
| | 228 | |
|
| 29 | 229 | | if (files.Count == 0) |
| | 230 | | { |
| 1 | 231 | | return Array.Empty<ExternalPathParserResult>(); |
| | 232 | | } |
| | 233 | |
|
| 28 | 234 | | var externalPathInfos = new List<ExternalPathParserResult>(); |
| 28 | 235 | | ReadOnlySpan<char> prefix = video.FileNameWithoutExtension; |
| 116 | 236 | | foreach (var file in files) |
| | 237 | | { |
| 30 | 238 | | var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.AsSpan()); |
| 30 | 239 | | if (fileNameWithoutExtension.Length >= prefix.Length |
| 30 | 240 | | && prefix.Equals(fileNameWithoutExtension[..prefix.Length], StringComparison.OrdinalIgnoreCase) |
| 30 | 241 | | && (fileNameWithoutExtension.Length == prefix.Length || _namingOptions.MediaFlagDelimiters.Contains( |
| | 242 | | { |
| 27 | 243 | | var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[prefix.Length..] |
| | 244 | |
|
| 27 | 245 | | if (externalPathInfo is not null) |
| | 246 | | { |
| 20 | 247 | | externalPathInfos.Add(externalPathInfo); |
| | 248 | | } |
| | 249 | | } |
| | 250 | | } |
| | 251 | |
|
| 28 | 252 | | return externalPathInfos; |
| | 253 | | } |
| | 254 | |
|
| | 255 | | /// <summary> |
| | 256 | | /// Returns the external file infos for the given audio. |
| | 257 | | /// </summary> |
| | 258 | | /// <param name="audio">The <see cref="Audio"/> object to search external files for.</param> |
| | 259 | | /// <param name="directoryService">The directory service to search for files.</param> |
| | 260 | | /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param> |
| | 261 | | /// <returns>The external file paths located.</returns> |
| | 262 | | public IReadOnlyList<ExternalPathParserResult> GetExternalFiles( |
| | 263 | | Audio audio, |
| | 264 | | IDirectoryService directoryService, |
| | 265 | | bool clearCache) |
| | 266 | | { |
| 0 | 267 | | if (!audio.IsFileProtocol) |
| | 268 | | { |
| 0 | 269 | | return Array.Empty<ExternalPathParserResult>(); |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | string folder = audio.ContainingFolderPath; |
| 0 | 273 | | var files = directoryService.GetFilePaths(folder, clearCache, true).ToList(); |
| 0 | 274 | | files.Remove(audio.Path); |
| 0 | 275 | | var internalMetadataPath = audio.GetInternalMetadataPath(); |
| 0 | 276 | | if (_fileSystem.DirectoryExists(internalMetadataPath)) |
| | 277 | | { |
| 0 | 278 | | files.AddRange(directoryService.GetFilePaths(internalMetadataPath, clearCache, true)); |
| | 279 | | } |
| | 280 | |
|
| 0 | 281 | | if (files.Count == 0) |
| | 282 | | { |
| 0 | 283 | | return Array.Empty<ExternalPathParserResult>(); |
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | var externalPathInfos = new List<ExternalPathParserResult>(); |
| 0 | 287 | | ReadOnlySpan<char> prefix = audio.FileNameWithoutExtension; |
| 0 | 288 | | foreach (var file in files) |
| | 289 | | { |
| 0 | 290 | | var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.AsSpan()); |
| 0 | 291 | | if (fileNameWithoutExtension.Length >= prefix.Length |
| 0 | 292 | | && prefix.Equals(fileNameWithoutExtension[..prefix.Length], StringComparison.OrdinalIgnoreCase) |
| 0 | 293 | | && (fileNameWithoutExtension.Length == prefix.Length || _namingOptions.MediaFlagDelimiters.Contains( |
| | 294 | | { |
| 0 | 295 | | var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[prefix.Length..] |
| | 296 | |
|
| 0 | 297 | | if (externalPathInfo is not null) |
| | 298 | | { |
| 0 | 299 | | externalPathInfos.Add(externalPathInfo); |
| | 300 | | } |
| | 301 | | } |
| | 302 | | } |
| | 303 | |
|
| 0 | 304 | | return externalPathInfos; |
| | 305 | | } |
| | 306 | |
|
| | 307 | | /// <summary> |
| | 308 | | /// Returns the media info of the given file. |
| | 309 | | /// </summary> |
| | 310 | | /// <param name="path">The path to the file.</param> |
| | 311 | | /// <param name="type">The <see cref="DlnaProfileType"/>.</param> |
| | 312 | | /// <param name="cancellationToken">The cancellation token to cancel operation.</param> |
| | 313 | | /// <returns>The media info for the given file.</returns> |
| | 314 | | private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, DlnaProfileType type, CancellationToken cancel |
| | 315 | | { |
| 15 | 316 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 317 | |
|
| 15 | 318 | | return _mediaEncoder.GetMediaInfo( |
| 15 | 319 | | new MediaInfoRequest |
| 15 | 320 | | { |
| 15 | 321 | | MediaType = type, |
| 15 | 322 | | MediaSource = new MediaSourceInfo |
| 15 | 323 | | { |
| 15 | 324 | | Path = path, |
| 15 | 325 | | Protocol = MediaProtocol.File |
| 15 | 326 | | } |
| 15 | 327 | | }, |
| 15 | 328 | | cancellationToken); |
| | 329 | | } |
| | 330 | |
|
| | 331 | | /// <summary> |
| | 332 | | /// Merges path metadata into stream metadata. |
| | 333 | | /// </summary> |
| | 334 | | /// <param name="mediaStream">The <see cref="MediaStream"/> object.</param> |
| | 335 | | /// <param name="pathInfo">The <see cref="ExternalPathParserResult"/> object.</param> |
| | 336 | | /// <returns>The modified mediaStream.</returns> |
| | 337 | | private MediaStream MergeMetadata(MediaStream mediaStream, ExternalPathParserResult pathInfo) |
| | 338 | | { |
| 19 | 339 | | mediaStream.Path = pathInfo.Path; |
| 19 | 340 | | mediaStream.IsExternal = true; |
| 19 | 341 | | mediaStream.Title = string.IsNullOrEmpty(mediaStream.Title) ? (string.IsNullOrEmpty(pathInfo.Title) ? null : |
| 19 | 342 | | mediaStream.Language = string.IsNullOrEmpty(mediaStream.Language) ? (string.IsNullOrEmpty(pathInfo.Language) |
| | 343 | |
|
| 19 | 344 | | return mediaStream; |
| | 345 | | } |
| | 346 | | } |
| | 347 | | } |