| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Text.Json.Serialization; |
| | 10 | | using System.Threading; |
| | 11 | | using Jellyfin.Data.Enums; |
| | 12 | | using Jellyfin.Extensions; |
| | 13 | | using MediaBrowser.Controller.MediaEncoding; |
| | 14 | | using MediaBrowser.Controller.Providers; |
| | 15 | | using MediaBrowser.Model.Dlna; |
| | 16 | | using MediaBrowser.Model.Entities; |
| | 17 | | using MediaBrowser.Model.IO; |
| | 18 | | using Microsoft.Extensions.Logging; |
| | 19 | |
|
| | 20 | | namespace MediaBrowser.Controller.Entities.TV |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Class Episode. |
| | 24 | | /// </summary> |
| | 25 | | public class Episode : Video, IHasTrailers, IHasLookupInfo<EpisodeInfo>, IHasSeries |
| | 26 | | { |
| | 27 | | public static IMediaEncoder MediaEncoder { get; set; } |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| | 30 | | [JsonIgnore] |
| 0 | 31 | | public IReadOnlyList<BaseItem> LocalTrailers => GetExtras() |
| 0 | 32 | | .Where(extra => extra.ExtraType == Model.Entities.ExtraType.Trailer) |
| 0 | 33 | | .ToArray(); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gets or sets the season in which it aired. |
| | 37 | | /// </summary> |
| | 38 | | /// <value>The aired season.</value> |
| | 39 | | public int? AirsBeforeSeasonNumber { get; set; } |
| | 40 | |
|
| | 41 | | public int? AirsAfterSeasonNumber { get; set; } |
| | 42 | |
|
| | 43 | | public int? AirsBeforeEpisodeNumber { get; set; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets or sets the ending episode number for double episodes. |
| | 47 | | /// </summary> |
| | 48 | | /// <value>The index number.</value> |
| | 49 | | public int? IndexNumberEnd { get; set; } |
| | 50 | |
|
| | 51 | | [JsonIgnore] |
| 0 | 52 | | protected override bool SupportsOwnedItems => IsStacked || MediaSourceCount > 1; |
| | 53 | |
|
| | 54 | | [JsonIgnore] |
| 0 | 55 | | public override bool SupportsInheritedParentImages => true; |
| | 56 | |
|
| | 57 | | [JsonIgnore] |
| 0 | 58 | | public override bool SupportsPeople => true; |
| | 59 | |
|
| | 60 | | [JsonIgnore] |
| 0 | 61 | | public int? AiredSeasonNumber => AirsAfterSeasonNumber ?? AirsBeforeSeasonNumber ?? ParentIndexNumber; |
| | 62 | |
|
| | 63 | | [JsonIgnore] |
| 0 | 64 | | public override Folder LatestItemsIndexContainer => Series; |
| | 65 | |
|
| | 66 | | [JsonIgnore] |
| 0 | 67 | | public override Guid DisplayParentId => SeasonId; |
| | 68 | |
|
| | 69 | | [JsonIgnore] |
| 0 | 70 | | protected override bool EnableDefaultVideoUserDataKeys => false; |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets the Episode's Series Instance. |
| | 74 | | /// </summary> |
| | 75 | | /// <value>The series.</value> |
| | 76 | | [JsonIgnore] |
| | 77 | | public Series Series |
| | 78 | | { |
| | 79 | | get |
| | 80 | | { |
| 0 | 81 | | var seriesId = SeriesId; |
| 0 | 82 | | if (seriesId.IsEmpty()) |
| | 83 | | { |
| 0 | 84 | | seriesId = FindSeriesId(); |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | return seriesId.IsEmpty() ? null : (LibraryManager.GetItemById(seriesId) as Series); |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | [JsonIgnore] |
| | 92 | | public Season Season |
| | 93 | | { |
| | 94 | | get |
| | 95 | | { |
| 0 | 96 | | var seasonId = SeasonId; |
| 0 | 97 | | if (seasonId.IsEmpty()) |
| | 98 | | { |
| 0 | 99 | | seasonId = FindSeasonId(); |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | return seasonId.IsEmpty() ? null : (LibraryManager.GetItemById(seasonId) as Season); |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | [JsonIgnore] |
| 0 | 107 | | public bool IsInSeasonFolder => FindParent<Season>() is not null; |
| | 108 | |
|
| | 109 | | [JsonIgnore] |
| | 110 | | public string SeriesPresentationUniqueKey { get; set; } |
| | 111 | |
|
| | 112 | | [JsonIgnore] |
| | 113 | | public string SeriesName { get; set; } |
| | 114 | |
|
| | 115 | | [JsonIgnore] |
| | 116 | | public string SeasonName { get; set; } |
| | 117 | |
|
| | 118 | | [JsonIgnore] |
| | 119 | | public override bool SupportsRemoteImageDownloading |
| | 120 | | { |
| | 121 | | get |
| | 122 | | { |
| 0 | 123 | | if (IsMissingEpisode) |
| | 124 | | { |
| 0 | 125 | | return false; |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | return true; |
| | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| | 132 | | [JsonIgnore] |
| 0 | 133 | | public bool IsMissingEpisode => LocationType == LocationType.Virtual; |
| | 134 | |
|
| | 135 | | [JsonIgnore] |
| | 136 | | public Guid SeasonId { get; set; } |
| | 137 | |
|
| | 138 | | [JsonIgnore] |
| | 139 | | public Guid SeriesId { get; set; } |
| | 140 | |
|
| | 141 | | public string FindSeriesSortName() |
| | 142 | | { |
| 0 | 143 | | var series = Series; |
| 0 | 144 | | return series is null ? SeriesName : series.SortName; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public override double GetDefaultPrimaryImageAspectRatio() |
| | 148 | | { |
| | 149 | | // hack for tv plugins |
| 0 | 150 | | if (SourceType == SourceType.Channel) |
| | 151 | | { |
| 0 | 152 | | return 0; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | return 16.0 / 9; |
| | 156 | | } |
| | 157 | |
|
| | 158 | | public override List<string> GetUserDataKeys() |
| | 159 | | { |
| 0 | 160 | | var list = base.GetUserDataKeys(); |
| | 161 | |
|
| 0 | 162 | | var series = Series; |
| 0 | 163 | | if (series is not null && ParentIndexNumber.HasValue && IndexNumber.HasValue) |
| | 164 | | { |
| 0 | 165 | | var seriesUserDataKeys = series.GetUserDataKeys(); |
| 0 | 166 | | var take = seriesUserDataKeys.Count; |
| 0 | 167 | | if (seriesUserDataKeys.Count > 1) |
| | 168 | | { |
| 0 | 169 | | take--; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | var newList = seriesUserDataKeys.GetRange(0, take); |
| 0 | 173 | | var suffix = ParentIndexNumber.Value.ToString("000", CultureInfo.InvariantCulture) + IndexNumber.Value.T |
| 0 | 174 | | for (int i = 0; i < take; i++) |
| | 175 | | { |
| 0 | 176 | | newList[i] = newList[i] + suffix; |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | newList.AddRange(list); |
| 0 | 180 | | list = newList; |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | return list; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | public string FindSeriesPresentationUniqueKey() |
| 0 | 187 | | => Series?.PresentationUniqueKey; |
| | 188 | |
|
| | 189 | | public string FindSeasonName() |
| | 190 | | { |
| 0 | 191 | | var season = Season; |
| | 192 | |
|
| 0 | 193 | | if (season is null) |
| | 194 | | { |
| 0 | 195 | | if (ParentIndexNumber.HasValue) |
| | 196 | | { |
| 0 | 197 | | return "Season " + ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture); |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | return "Season Unknown"; |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | return season.Name; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | public string FindSeriesName() |
| | 207 | | { |
| 0 | 208 | | var series = Series; |
| 0 | 209 | | return series is null ? SeriesName : series.Name; |
| | 210 | | } |
| | 211 | |
|
| | 212 | | public Guid FindSeasonId() |
| | 213 | | { |
| 0 | 214 | | var season = FindParent<Season>(); |
| | 215 | |
|
| | 216 | | // Episodes directly in series folder |
| 0 | 217 | | if (season is null) |
| | 218 | | { |
| 0 | 219 | | var series = Series; |
| | 220 | |
|
| 0 | 221 | | if (series is not null && ParentIndexNumber.HasValue) |
| | 222 | | { |
| 0 | 223 | | var findNumber = ParentIndexNumber.Value; |
| | 224 | |
|
| 0 | 225 | | season = series.Children |
| 0 | 226 | | .OfType<Season>() |
| 0 | 227 | | .FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == findNumber); |
| | 228 | | } |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | return season is null ? Guid.Empty : season.Id; |
| | 232 | | } |
| | 233 | |
|
| | 234 | | /// <summary> |
| | 235 | | /// Creates the name of the sort. |
| | 236 | | /// </summary> |
| | 237 | | /// <returns>System.String.</returns> |
| | 238 | | protected override string CreateSortName() |
| | 239 | | { |
| 0 | 240 | | return (ParentIndexNumber is not null ? ParentIndexNumber.Value.ToString("000 - ", CultureInfo.InvariantCult |
| 0 | 241 | | + (IndexNumber is not null ? IndexNumber.Value.ToString("0000 - ", CultureInfo.InvariantCulture) : s |
| | 242 | | } |
| | 243 | |
|
| | 244 | | /// <summary> |
| | 245 | | /// Determines whether [contains episode number] [the specified number]. |
| | 246 | | /// </summary> |
| | 247 | | /// <param name="number">The number.</param> |
| | 248 | | /// <returns><c>true</c> if [contains episode number] [the specified number]; otherwise, <c>false</c>.</returns> |
| | 249 | | public bool ContainsEpisodeNumber(int number) |
| | 250 | | { |
| 0 | 251 | | if (IndexNumber.HasValue) |
| | 252 | | { |
| 0 | 253 | | if (IndexNumberEnd.HasValue) |
| | 254 | | { |
| 0 | 255 | | return number >= IndexNumber.Value && number <= IndexNumberEnd.Value; |
| | 256 | | } |
| | 257 | |
|
| 0 | 258 | | return IndexNumber.Value == number; |
| | 259 | | } |
| | 260 | |
|
| 0 | 261 | | return false; |
| | 262 | | } |
| | 263 | |
|
| | 264 | | public Guid FindSeriesId() |
| | 265 | | { |
| 0 | 266 | | var series = FindParent<Series>(); |
| 0 | 267 | | return series is null ? Guid.Empty : series.Id; |
| | 268 | | } |
| | 269 | |
|
| | 270 | | public override IEnumerable<Guid> GetAncestorIds() |
| | 271 | | { |
| 0 | 272 | | var list = base.GetAncestorIds().ToList(); |
| | 273 | |
|
| 0 | 274 | | var seasonId = SeasonId; |
| | 275 | |
|
| 0 | 276 | | if (!seasonId.IsEmpty() && !list.Contains(seasonId)) |
| | 277 | | { |
| 0 | 278 | | list.Add(seasonId); |
| | 279 | | } |
| | 280 | |
|
| 0 | 281 | | return list; |
| | 282 | | } |
| | 283 | |
|
| | 284 | | public override IEnumerable<FileSystemMetadata> GetDeletePaths() |
| | 285 | | { |
| 0 | 286 | | return new[] |
| 0 | 287 | | { |
| 0 | 288 | | new FileSystemMetadata |
| 0 | 289 | | { |
| 0 | 290 | | FullName = Path, |
| 0 | 291 | | IsDirectory = IsFolder |
| 0 | 292 | | } |
| 0 | 293 | | }.Concat(GetLocalMetadataFilesToDelete()); |
| | 294 | | } |
| | 295 | |
|
| | 296 | | public override UnratedItem GetBlockUnratedType() |
| | 297 | | { |
| 0 | 298 | | return UnratedItem.Series; |
| | 299 | | } |
| | 300 | |
|
| | 301 | | public EpisodeInfo GetLookupInfo() |
| | 302 | | { |
| 0 | 303 | | var id = GetItemLookupInfo<EpisodeInfo>(); |
| | 304 | |
|
| 0 | 305 | | var series = Series; |
| | 306 | |
|
| 0 | 307 | | if (series is not null) |
| | 308 | | { |
| 0 | 309 | | id.SeriesProviderIds = series.ProviderIds; |
| 0 | 310 | | id.SeriesDisplayOrder = series.DisplayOrder; |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | if (Season is not null) |
| | 314 | | { |
| 0 | 315 | | id.SeasonProviderIds = Season.ProviderIds; |
| | 316 | | } |
| | 317 | |
|
| 0 | 318 | | id.IsMissingEpisode = IsMissingEpisode; |
| 0 | 319 | | id.IndexNumberEnd = IndexNumberEnd; |
| | 320 | |
|
| 0 | 321 | | return id; |
| | 322 | | } |
| | 323 | |
|
| | 324 | | public override bool BeforeMetadataRefresh(bool replaceAllMetadata) |
| | 325 | | { |
| 0 | 326 | | var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata); |
| | 327 | |
|
| 0 | 328 | | if (!IsLocked) |
| | 329 | | { |
| 0 | 330 | | if (SourceType == SourceType.Library || SourceType == SourceType.LiveTV) |
| | 331 | | { |
| 0 | 332 | | var libraryOptions = LibraryManager.GetLibraryOptions(this); |
| 0 | 333 | | if (libraryOptions.EnableEmbeddedEpisodeInfos && string.Equals(Container, "mp4", StringComparison.Or |
| | 334 | | { |
| | 335 | | try |
| | 336 | | { |
| 0 | 337 | | var mediaInfo = MediaEncoder.GetMediaInfo( |
| 0 | 338 | | new MediaInfoRequest |
| 0 | 339 | | { |
| 0 | 340 | | MediaSource = GetMediaSources(false)[0], |
| 0 | 341 | | MediaType = DlnaProfileType.Video |
| 0 | 342 | | }, |
| 0 | 343 | | CancellationToken.None).GetAwaiter().GetResult(); |
| 0 | 344 | | if (mediaInfo.ParentIndexNumber > 0) |
| | 345 | | { |
| 0 | 346 | | ParentIndexNumber = mediaInfo.ParentIndexNumber; |
| | 347 | | } |
| | 348 | |
|
| 0 | 349 | | if (mediaInfo.IndexNumber > 0) |
| | 350 | | { |
| 0 | 351 | | IndexNumber = mediaInfo.IndexNumber; |
| | 352 | | } |
| | 353 | |
|
| 0 | 354 | | if (!string.IsNullOrEmpty(mediaInfo.ShowName)) |
| | 355 | | { |
| 0 | 356 | | SeriesName = mediaInfo.ShowName; |
| | 357 | | } |
| 0 | 358 | | } |
| 0 | 359 | | catch (Exception ex) |
| | 360 | | { |
| 0 | 361 | | Logger.LogError(ex, "Error reading the episode information with ffprobe. Episode: {EpisodeIn |
| 0 | 362 | | } |
| | 363 | | } |
| | 364 | |
|
| | 365 | | try |
| | 366 | | { |
| 0 | 367 | | if (LibraryManager.FillMissingEpisodeNumbersFromPath(this, replaceAllMetadata)) |
| | 368 | | { |
| 0 | 369 | | hasChanges = true; |
| | 370 | | } |
| 0 | 371 | | } |
| 0 | 372 | | catch (Exception ex) |
| | 373 | | { |
| 0 | 374 | | Logger.LogError(ex, "Error in FillMissingEpisodeNumbersFromPath. Episode: {Episode}", Path ?? Na |
| 0 | 375 | | } |
| | 376 | | } |
| | 377 | | } |
| | 378 | |
|
| 0 | 379 | | return hasChanges; |
| | 380 | | } |
| | 381 | | } |
| | 382 | | } |