Video.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Persistence;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.MediaInfo;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Runtime.Serialization;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using CommonIO;
  15. using MediaBrowser.Controller.Channels;
  16. namespace MediaBrowser.Controller.Entities
  17. {
  18. /// <summary>
  19. /// Class Video
  20. /// </summary>
  21. public class Video : BaseItem,
  22. IHasAspectRatio,
  23. IHasTags,
  24. ISupportsPlaceHolders,
  25. IHasMediaSources,
  26. IHasShortOverview,
  27. IThemeMedia,
  28. IArchivable
  29. {
  30. [IgnoreDataMember]
  31. public string PrimaryVersionId { get; set; }
  32. public List<string> AdditionalParts { get; set; }
  33. public List<string> LocalAlternateVersions { get; set; }
  34. public List<LinkedChild> LinkedAlternateVersions { get; set; }
  35. public List<ChannelMediaInfo> ChannelMediaSources { get; set; }
  36. [IgnoreDataMember]
  37. public bool IsThemeMedia
  38. {
  39. get
  40. {
  41. return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeVideo;
  42. }
  43. }
  44. [IgnoreDataMember]
  45. public override string PresentationUniqueKey
  46. {
  47. get
  48. {
  49. if (!string.IsNullOrWhiteSpace(PrimaryVersionId))
  50. {
  51. return PrimaryVersionId;
  52. }
  53. return base.PresentationUniqueKey;
  54. }
  55. }
  56. public long? Size { get; set; }
  57. public string Container { get; set; }
  58. public int? TotalBitrate { get; set; }
  59. public string ShortOverview { get; set; }
  60. public ExtraType? ExtraType { get; set; }
  61. /// <summary>
  62. /// Gets or sets the timestamp.
  63. /// </summary>
  64. /// <value>The timestamp.</value>
  65. public TransportStreamTimestamp? Timestamp { get; set; }
  66. /// <summary>
  67. /// Gets or sets the subtitle paths.
  68. /// </summary>
  69. /// <value>The subtitle paths.</value>
  70. public List<string> SubtitleFiles { get; set; }
  71. /// <summary>
  72. /// Gets or sets a value indicating whether this instance has subtitles.
  73. /// </summary>
  74. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  75. public bool HasSubtitles { get; set; }
  76. public bool IsPlaceHolder { get; set; }
  77. public bool IsShortcut { get; set; }
  78. public string ShortcutPath { get; set; }
  79. /// <summary>
  80. /// Gets or sets the video bit rate.
  81. /// </summary>
  82. /// <value>The video bit rate.</value>
  83. public int? VideoBitRate { get; set; }
  84. /// <summary>
  85. /// Gets or sets the default index of the video stream.
  86. /// </summary>
  87. /// <value>The default index of the video stream.</value>
  88. public int? DefaultVideoStreamIndex { get; set; }
  89. /// <summary>
  90. /// Gets or sets the type of the video.
  91. /// </summary>
  92. /// <value>The type of the video.</value>
  93. public VideoType VideoType { get; set; }
  94. /// <summary>
  95. /// Gets or sets the type of the iso.
  96. /// </summary>
  97. /// <value>The type of the iso.</value>
  98. public IsoType? IsoType { get; set; }
  99. /// <summary>
  100. /// Gets or sets the video3 D format.
  101. /// </summary>
  102. /// <value>The video3 D format.</value>
  103. public Video3DFormat? Video3DFormat { get; set; }
  104. /// <summary>
  105. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  106. /// </summary>
  107. public List<string> PlayableStreamFileNames { get; set; }
  108. /// <summary>
  109. /// Gets the playable stream files.
  110. /// </summary>
  111. /// <returns>List{System.String}.</returns>
  112. public List<string> GetPlayableStreamFiles()
  113. {
  114. return GetPlayableStreamFiles(Path);
  115. }
  116. /// <summary>
  117. /// Gets or sets the aspect ratio.
  118. /// </summary>
  119. /// <value>The aspect ratio.</value>
  120. public string AspectRatio { get; set; }
  121. public Video()
  122. {
  123. PlayableStreamFileNames = new List<string>();
  124. AdditionalParts = new List<string>();
  125. LocalAlternateVersions = new List<string>();
  126. Tags = new List<string>();
  127. SubtitleFiles = new List<string>();
  128. LinkedAlternateVersions = new List<LinkedChild>();
  129. }
  130. public override bool CanDownload()
  131. {
  132. if (VideoType == VideoType.HdDvd || VideoType == VideoType.Dvd ||
  133. VideoType == VideoType.BluRay)
  134. {
  135. return false;
  136. }
  137. var locationType = LocationType;
  138. return locationType != LocationType.Remote &&
  139. locationType != LocationType.Virtual;
  140. }
  141. [IgnoreDataMember]
  142. public override bool SupportsAddingToPlaylist
  143. {
  144. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  145. }
  146. [IgnoreDataMember]
  147. public int MediaSourceCount
  148. {
  149. get
  150. {
  151. if (!string.IsNullOrWhiteSpace(PrimaryVersionId))
  152. {
  153. var item = LibraryManager.GetItemById(PrimaryVersionId) as Video;
  154. if (item != null)
  155. {
  156. return item.MediaSourceCount;
  157. }
  158. }
  159. return LinkedAlternateVersions.Count + LocalAlternateVersions.Count + 1;
  160. }
  161. }
  162. [IgnoreDataMember]
  163. public bool IsStacked
  164. {
  165. get { return AdditionalParts.Count > 0; }
  166. }
  167. [IgnoreDataMember]
  168. public bool HasLocalAlternateVersions
  169. {
  170. get { return LocalAlternateVersions.Count > 0; }
  171. }
  172. [IgnoreDataMember]
  173. public bool IsArchive
  174. {
  175. get
  176. {
  177. if (string.IsNullOrWhiteSpace(Path))
  178. {
  179. return false;
  180. }
  181. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  182. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  183. }
  184. }
  185. public IEnumerable<Guid> GetAdditionalPartIds()
  186. {
  187. return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  188. }
  189. public IEnumerable<Guid> GetLocalAlternateVersionIds()
  190. {
  191. return LocalAlternateVersions.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  192. }
  193. [IgnoreDataMember]
  194. protected virtual bool EnableDefaultVideoUserDataKeys
  195. {
  196. get
  197. {
  198. return true;
  199. }
  200. }
  201. public override List<string> GetUserDataKeys()
  202. {
  203. var list = base.GetUserDataKeys();
  204. if (EnableDefaultVideoUserDataKeys)
  205. {
  206. if (ExtraType.HasValue)
  207. {
  208. var key = this.GetProviderId(MetadataProviders.Tmdb);
  209. if (!string.IsNullOrWhiteSpace(key))
  210. {
  211. list.Insert(0, GetUserDataKey(key));
  212. }
  213. key = this.GetProviderId(MetadataProviders.Imdb);
  214. if (!string.IsNullOrWhiteSpace(key))
  215. {
  216. list.Insert(0, GetUserDataKey(key));
  217. }
  218. }
  219. else
  220. {
  221. var key = this.GetProviderId(MetadataProviders.Imdb);
  222. if (!string.IsNullOrWhiteSpace(key))
  223. {
  224. list.Insert(0, key);
  225. }
  226. key = this.GetProviderId(MetadataProviders.Tmdb);
  227. if (!string.IsNullOrWhiteSpace(key))
  228. {
  229. list.Insert(0, key);
  230. }
  231. }
  232. }
  233. return list;
  234. }
  235. private string GetUserDataKey(string providerId)
  236. {
  237. var key = providerId + "-" + ExtraType.ToString().ToLower();
  238. // Make sure different trailers have their own data.
  239. if (RunTimeTicks.HasValue)
  240. {
  241. key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
  242. }
  243. return key;
  244. }
  245. public IEnumerable<Video> GetLinkedAlternateVersions()
  246. {
  247. var linkedVersions = LinkedAlternateVersions
  248. .Select(GetLinkedChild)
  249. .Where(i => i != null)
  250. .OfType<Video>();
  251. return linkedVersions
  252. .OrderBy(i => i.SortName);
  253. }
  254. /// <summary>
  255. /// Gets the additional parts.
  256. /// </summary>
  257. /// <returns>IEnumerable{Video}.</returns>
  258. public IEnumerable<Video> GetAdditionalParts()
  259. {
  260. return GetAdditionalPartIds()
  261. .Select(i => LibraryManager.GetItemById(i))
  262. .Where(i => i != null)
  263. .OfType<Video>()
  264. .OrderBy(i => i.SortName);
  265. }
  266. [IgnoreDataMember]
  267. public override string ContainingFolderPath
  268. {
  269. get
  270. {
  271. if (IsStacked)
  272. {
  273. return System.IO.Path.GetDirectoryName(Path);
  274. }
  275. if (!IsPlaceHolder)
  276. {
  277. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
  278. VideoType == VideoType.HdDvd)
  279. {
  280. return Path;
  281. }
  282. }
  283. return base.ContainingFolderPath;
  284. }
  285. }
  286. [IgnoreDataMember]
  287. public override string FileNameWithoutExtension
  288. {
  289. get
  290. {
  291. if (LocationType == LocationType.FileSystem)
  292. {
  293. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd || VideoType == VideoType.HdDvd)
  294. {
  295. return System.IO.Path.GetFileName(Path);
  296. }
  297. return System.IO.Path.GetFileNameWithoutExtension(Path);
  298. }
  299. return null;
  300. }
  301. }
  302. internal override bool IsValidFromResolver(BaseItem newItem)
  303. {
  304. var current = this;
  305. var newAsVideo = newItem as Video;
  306. if (newAsVideo != null)
  307. {
  308. if (!current.AdditionalParts.SequenceEqual(newAsVideo.AdditionalParts, StringComparer.OrdinalIgnoreCase))
  309. {
  310. return false;
  311. }
  312. if (!current.LocalAlternateVersions.SequenceEqual(newAsVideo.LocalAlternateVersions, StringComparer.OrdinalIgnoreCase))
  313. {
  314. return false;
  315. }
  316. if (newAsVideo.VideoType != VideoType)
  317. {
  318. return false;
  319. }
  320. }
  321. return base.IsValidFromResolver(newItem);
  322. }
  323. /// <summary>
  324. /// Gets the playable stream files.
  325. /// </summary>
  326. /// <param name="rootPath">The root path.</param>
  327. /// <returns>List{System.String}.</returns>
  328. public List<string> GetPlayableStreamFiles(string rootPath)
  329. {
  330. var allFiles = FileSystem.GetFilePaths(rootPath, true).ToList();
  331. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  332. .Where(f => !string.IsNullOrEmpty(f))
  333. .ToList();
  334. }
  335. /// <summary>
  336. /// Gets a value indicating whether [is3 D].
  337. /// </summary>
  338. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  339. [IgnoreDataMember]
  340. public bool Is3D
  341. {
  342. get { return Video3DFormat.HasValue; }
  343. }
  344. /// <summary>
  345. /// Gets the type of the media.
  346. /// </summary>
  347. /// <value>The type of the media.</value>
  348. [IgnoreDataMember]
  349. public override string MediaType
  350. {
  351. get
  352. {
  353. return Model.Entities.MediaType.Video;
  354. }
  355. }
  356. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  357. {
  358. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  359. if (IsStacked)
  360. {
  361. var tasks = AdditionalParts
  362. .Select(i => RefreshMetadataForOwnedVideo(options, i, cancellationToken));
  363. await Task.WhenAll(tasks).ConfigureAwait(false);
  364. }
  365. // Must have a parent to have additional parts or alternate versions
  366. // In other words, it must be part of the Parent/Child tree
  367. // The additional parts won't have additional parts themselves
  368. if (LocationType == LocationType.FileSystem && GetParent() != null)
  369. {
  370. if (!IsStacked)
  371. {
  372. RefreshLinkedAlternateVersions();
  373. var tasks = LocalAlternateVersions
  374. .Select(i => RefreshMetadataForOwnedVideo(options, i, cancellationToken));
  375. await Task.WhenAll(tasks).ConfigureAwait(false);
  376. }
  377. }
  378. return hasChanges;
  379. }
  380. private void RefreshLinkedAlternateVersions()
  381. {
  382. foreach (var child in LinkedAlternateVersions)
  383. {
  384. // Reset the cached value
  385. if (child.ItemId.HasValue && child.ItemId.Value == Guid.Empty)
  386. {
  387. child.ItemId = null;
  388. }
  389. }
  390. }
  391. public override async Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  392. {
  393. await base.UpdateToRepository(updateReason, cancellationToken).ConfigureAwait(false);
  394. var localAlternates = GetLocalAlternateVersionIds()
  395. .Select(i => LibraryManager.GetItemById(i))
  396. .Where(i => i != null);
  397. foreach (var item in localAlternates)
  398. {
  399. item.ImageInfos = ImageInfos;
  400. item.Overview = Overview;
  401. item.ProductionYear = ProductionYear;
  402. item.PremiereDate = PremiereDate;
  403. item.CommunityRating = CommunityRating;
  404. item.OfficialRating = OfficialRating;
  405. item.Genres = Genres;
  406. item.ProviderIds = ProviderIds;
  407. await item.UpdateToRepository(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(false);
  408. }
  409. }
  410. public override IEnumerable<string> GetDeletePaths()
  411. {
  412. if (!IsInMixedFolder)
  413. {
  414. return new[] { ContainingFolderPath };
  415. }
  416. return base.GetDeletePaths();
  417. }
  418. public IEnumerable<MediaStream> GetMediaStreams()
  419. {
  420. var mediaSource = GetMediaSources(false)
  421. .FirstOrDefault();
  422. if (mediaSource == null)
  423. {
  424. return new List<MediaStream>();
  425. }
  426. return mediaSource.MediaStreams;
  427. }
  428. public virtual MediaStream GetDefaultVideoStream()
  429. {
  430. if (!DefaultVideoStreamIndex.HasValue)
  431. {
  432. return null;
  433. }
  434. return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
  435. {
  436. ItemId = Id,
  437. Index = DefaultVideoStreamIndex.Value
  438. }).FirstOrDefault();
  439. }
  440. private List<Tuple<Video, MediaSourceType>> GetAllVideosForMediaSources()
  441. {
  442. var list = new List<Tuple<Video, MediaSourceType>>();
  443. list.Add(new Tuple<Video, MediaSourceType>(this, MediaSourceType.Default));
  444. list.AddRange(GetLinkedAlternateVersions().Select(i => new Tuple<Video, MediaSourceType>(i, MediaSourceType.Grouping)));
  445. if (!string.IsNullOrWhiteSpace(PrimaryVersionId))
  446. {
  447. var primary = LibraryManager.GetItemById(PrimaryVersionId) as Video;
  448. if (primary != null)
  449. {
  450. var existingIds = list.Select(i => i.Item1.Id).ToList();
  451. list.Add(new Tuple<Video, MediaSourceType>(primary, MediaSourceType.Grouping));
  452. list.AddRange(primary.GetLinkedAlternateVersions().Where(i => !existingIds.Contains(i.Id)).Select(i => new Tuple<Video, MediaSourceType>(i, MediaSourceType.Grouping)));
  453. }
  454. }
  455. var localAlternates = list
  456. .SelectMany(i => i.Item1.GetLocalAlternateVersionIds())
  457. .Select(LibraryManager.GetItemById)
  458. .Where(i => i != null)
  459. .OfType<Video>()
  460. .ToList();
  461. list.AddRange(localAlternates.Select(i => new Tuple<Video, MediaSourceType>(i, MediaSourceType.Default)));
  462. return list;
  463. }
  464. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  465. {
  466. if (SourceType == SourceType.Channel)
  467. {
  468. var sources = ChannelManager.GetStaticMediaSources(this, false, CancellationToken.None)
  469. .Result.ToList();
  470. if (sources.Count > 0)
  471. {
  472. return sources;
  473. }
  474. return new List<MediaSourceInfo>
  475. {
  476. GetVersionInfo(enablePathSubstitution, this, MediaSourceType.Placeholder)
  477. };
  478. }
  479. var list = GetAllVideosForMediaSources();
  480. var result = list.Select(i => GetVersionInfo(enablePathSubstitution, i.Item1, i.Item2)).ToList();
  481. return result.OrderBy(i =>
  482. {
  483. if (i.VideoType == VideoType.VideoFile)
  484. {
  485. return 0;
  486. }
  487. return 1;
  488. }).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
  489. .ThenByDescending(i =>
  490. {
  491. var stream = i.VideoStream;
  492. return stream == null || stream.Width == null ? 0 : stream.Width.Value;
  493. })
  494. .ToList();
  495. }
  496. private static MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, Video i, MediaSourceType type)
  497. {
  498. var mediaStreams = MediaSourceManager.GetMediaStreams(i.Id)
  499. .ToList();
  500. var locationType = i.LocationType;
  501. var info = new MediaSourceInfo
  502. {
  503. Id = i.Id.ToString("N"),
  504. IsoType = i.IsoType,
  505. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  506. MediaStreams = mediaStreams,
  507. Name = GetMediaSourceName(i, mediaStreams),
  508. Path = enablePathSubstitution ? GetMappedPath(i.Path, locationType) : i.Path,
  509. RunTimeTicks = i.RunTimeTicks,
  510. Video3DFormat = i.Video3DFormat,
  511. VideoType = i.VideoType,
  512. Container = i.Container,
  513. Size = i.Size,
  514. Timestamp = i.Timestamp,
  515. Type = type,
  516. PlayableStreamFileNames = i.PlayableStreamFileNames.ToList(),
  517. SupportsDirectStream = i.VideoType == VideoType.VideoFile
  518. };
  519. if (i.IsShortcut)
  520. {
  521. info.Path = i.ShortcutPath;
  522. if (info.Path.StartsWith("Http", StringComparison.OrdinalIgnoreCase))
  523. {
  524. info.Protocol = MediaProtocol.Http;
  525. }
  526. else if (info.Path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase))
  527. {
  528. info.Protocol = MediaProtocol.Rtmp;
  529. }
  530. else if (info.Path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase))
  531. {
  532. info.Protocol = MediaProtocol.Rtsp;
  533. }
  534. else
  535. {
  536. info.Protocol = MediaProtocol.File;
  537. }
  538. }
  539. if (string.IsNullOrEmpty(info.Container))
  540. {
  541. if (i.VideoType == VideoType.VideoFile || i.VideoType == VideoType.Iso)
  542. {
  543. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  544. {
  545. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  546. }
  547. }
  548. }
  549. try
  550. {
  551. var bitrate = i.TotalBitrate ??
  552. info.MediaStreams.Where(m => m.Type != MediaStreamType.Subtitle && !string.Equals(m.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase))
  553. .Select(m => m.BitRate ?? 0)
  554. .Sum();
  555. if (bitrate > 0)
  556. {
  557. info.Bitrate = bitrate;
  558. }
  559. }
  560. catch (OverflowException ex)
  561. {
  562. Logger.ErrorException("Error calculating total bitrate", ex);
  563. }
  564. return info;
  565. }
  566. private static string GetMediaSourceName(Video video, List<MediaStream> mediaStreams)
  567. {
  568. var terms = new List<string>();
  569. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  570. var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
  571. if (video.Video3DFormat.HasValue)
  572. {
  573. terms.Add("3D");
  574. }
  575. if (video.VideoType == VideoType.BluRay)
  576. {
  577. terms.Add("Bluray");
  578. }
  579. else if (video.VideoType == VideoType.Dvd)
  580. {
  581. terms.Add("DVD");
  582. }
  583. else if (video.VideoType == VideoType.HdDvd)
  584. {
  585. terms.Add("HD-DVD");
  586. }
  587. else if (video.VideoType == VideoType.Iso)
  588. {
  589. if (video.IsoType.HasValue)
  590. {
  591. if (video.IsoType.Value == Model.Entities.IsoType.BluRay)
  592. {
  593. terms.Add("Bluray");
  594. }
  595. else if (video.IsoType.Value == Model.Entities.IsoType.Dvd)
  596. {
  597. terms.Add("DVD");
  598. }
  599. }
  600. else
  601. {
  602. terms.Add("ISO");
  603. }
  604. }
  605. if (videoStream != null)
  606. {
  607. if (videoStream.Width.HasValue)
  608. {
  609. if (videoStream.Width.Value >= 3800)
  610. {
  611. terms.Add("4K");
  612. }
  613. else if (videoStream.Width.Value >= 1900)
  614. {
  615. terms.Add("1080P");
  616. }
  617. else if (videoStream.Width.Value >= 1270)
  618. {
  619. terms.Add("720P");
  620. }
  621. else if (videoStream.Width.Value >= 700)
  622. {
  623. terms.Add("480P");
  624. }
  625. else
  626. {
  627. terms.Add("SD");
  628. }
  629. }
  630. }
  631. if (videoStream != null && !string.IsNullOrWhiteSpace(videoStream.Codec))
  632. {
  633. terms.Add(videoStream.Codec.ToUpper());
  634. }
  635. if (audioStream != null)
  636. {
  637. var audioCodec = string.Equals(audioStream.Codec, "dca", StringComparison.OrdinalIgnoreCase)
  638. ? audioStream.Profile
  639. : audioStream.Codec;
  640. if (!string.IsNullOrEmpty(audioCodec))
  641. {
  642. terms.Add(audioCodec.ToUpper());
  643. }
  644. }
  645. return string.Join("/", terms.ToArray());
  646. }
  647. }
  648. }