MediaStream.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using MediaBrowser.Model.Dlna;
  9. using MediaBrowser.Model.Extensions;
  10. using MediaBrowser.Model.MediaInfo;
  11. namespace MediaBrowser.Model.Entities
  12. {
  13. /// <summary>
  14. /// Class MediaStream.
  15. /// </summary>
  16. public class MediaStream
  17. {
  18. /// <summary>
  19. /// Gets or sets the codec.
  20. /// </summary>
  21. /// <value>The codec.</value>
  22. public string Codec { get; set; }
  23. /// <summary>
  24. /// Gets or sets the codec tag.
  25. /// </summary>
  26. /// <value>The codec tag.</value>
  27. public string CodecTag { get; set; }
  28. /// <summary>
  29. /// Gets or sets the language.
  30. /// </summary>
  31. /// <value>The language.</value>
  32. public string Language { get; set; }
  33. /// <summary>
  34. /// Gets or sets the color range.
  35. /// </summary>
  36. /// <value>The color range.</value>
  37. public string ColorRange { get; set; }
  38. /// <summary>
  39. /// Gets or sets the color space.
  40. /// </summary>
  41. /// <value>The color space.</value>
  42. public string ColorSpace { get; set; }
  43. /// <summary>
  44. /// Gets or sets the color transfer.
  45. /// </summary>
  46. /// <value>The color transfer.</value>
  47. public string ColorTransfer { get; set; }
  48. /// <summary>
  49. /// Gets or sets the color primaries.
  50. /// </summary>
  51. /// <value>The color primaries.</value>
  52. public string ColorPrimaries { get; set; }
  53. /// <summary>
  54. /// Gets or sets the comment.
  55. /// </summary>
  56. /// <value>The comment.</value>
  57. public string Comment { get; set; }
  58. /// <summary>
  59. /// Gets or sets the time base.
  60. /// </summary>
  61. /// <value>The time base.</value>
  62. public string TimeBase { get; set; }
  63. /// <summary>
  64. /// Gets or sets the codec time base.
  65. /// </summary>
  66. /// <value>The codec time base.</value>
  67. public string CodecTimeBase { get; set; }
  68. /// <summary>
  69. /// Gets or sets the title.
  70. /// </summary>
  71. /// <value>The title.</value>
  72. public string Title { get; set; }
  73. /// <summary>
  74. /// Gets the video range.
  75. /// </summary>
  76. /// <value>The video range.</value>
  77. public string VideoRange
  78. {
  79. get
  80. {
  81. if (Type != MediaStreamType.Video)
  82. {
  83. return null;
  84. }
  85. var colorTransfer = ColorTransfer;
  86. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
  87. || string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  88. {
  89. return "HDR";
  90. }
  91. // For some Dolby Vision files, no color transfer is provided, so check the codec
  92. var codecTag = CodecTag;
  93. if (string.Equals(codecTag, "dva1", StringComparison.OrdinalIgnoreCase)
  94. || string.Equals(codecTag, "dvav", StringComparison.OrdinalIgnoreCase)
  95. || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
  96. || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
  97. || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
  98. {
  99. return "HDR";
  100. }
  101. return "SDR";
  102. }
  103. }
  104. public string LocalizedUndefined { get; set; }
  105. public string LocalizedDefault { get; set; }
  106. public string LocalizedForced { get; set; }
  107. public string DisplayTitle
  108. {
  109. get
  110. {
  111. switch (Type)
  112. {
  113. case MediaStreamType.Audio:
  114. {
  115. var attributes = new List<string>();
  116. if (!string.IsNullOrEmpty(Language))
  117. {
  118. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  119. string fullLanguage = CultureInfo
  120. .GetCultures(CultureTypes.NeutralCultures)
  121. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  122. ?.DisplayName;
  123. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  124. }
  125. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase))
  126. {
  127. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  128. }
  129. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  130. {
  131. attributes.Add(Profile);
  132. }
  133. if (!string.IsNullOrEmpty(ChannelLayout))
  134. {
  135. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  136. }
  137. else if (Channels.HasValue)
  138. {
  139. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  140. }
  141. if (IsDefault)
  142. {
  143. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  144. }
  145. if (!string.IsNullOrEmpty(Title))
  146. {
  147. var result = new StringBuilder(Title);
  148. foreach (var tag in attributes)
  149. {
  150. // Keep Tags that are not already in Title.
  151. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  152. {
  153. result.Append(" - ").Append(tag);
  154. }
  155. }
  156. return result.ToString();
  157. }
  158. return string.Join(" - ", attributes);
  159. }
  160. case MediaStreamType.Video:
  161. {
  162. var attributes = new List<string>();
  163. var resolutionText = GetResolutionText();
  164. if (!string.IsNullOrEmpty(resolutionText))
  165. {
  166. attributes.Add(resolutionText);
  167. }
  168. if (!string.IsNullOrEmpty(Codec))
  169. {
  170. attributes.Add(Codec.ToUpperInvariant());
  171. }
  172. if (!string.IsNullOrEmpty(VideoRange))
  173. {
  174. attributes.Add(VideoRange.ToUpperInvariant());
  175. }
  176. if (!string.IsNullOrEmpty(Title))
  177. {
  178. var result = new StringBuilder(Title);
  179. foreach (var tag in attributes)
  180. {
  181. // Keep Tags that are not already in Title.
  182. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  183. {
  184. result.Append(" - ").Append(tag);
  185. }
  186. }
  187. return result.ToString();
  188. }
  189. return string.Join(' ', attributes);
  190. }
  191. case MediaStreamType.Subtitle:
  192. {
  193. var attributes = new List<string>();
  194. if (!string.IsNullOrEmpty(Language))
  195. {
  196. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  197. string fullLanguage = CultureInfo
  198. .GetCultures(CultureTypes.NeutralCultures)
  199. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  200. ?.DisplayName;
  201. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  202. }
  203. else
  204. {
  205. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  206. }
  207. if (IsDefault)
  208. {
  209. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  210. }
  211. if (IsForced)
  212. {
  213. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  214. }
  215. if (!string.IsNullOrEmpty(Title))
  216. {
  217. var result = new StringBuilder(Title);
  218. foreach (var tag in attributes)
  219. {
  220. // Keep Tags that are not already in Title.
  221. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  222. {
  223. result.Append(" - ").Append(tag);
  224. }
  225. }
  226. return result.ToString();
  227. }
  228. return string.Join(" - ", attributes);
  229. }
  230. default:
  231. return null;
  232. }
  233. }
  234. }
  235. public string NalLengthSize { get; set; }
  236. /// <summary>
  237. /// Gets or sets a value indicating whether this instance is interlaced.
  238. /// </summary>
  239. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  240. public bool IsInterlaced { get; set; }
  241. public bool? IsAVC { get; set; }
  242. /// <summary>
  243. /// Gets or sets the channel layout.
  244. /// </summary>
  245. /// <value>The channel layout.</value>
  246. public string ChannelLayout { get; set; }
  247. /// <summary>
  248. /// Gets or sets the bit rate.
  249. /// </summary>
  250. /// <value>The bit rate.</value>
  251. public int? BitRate { get; set; }
  252. /// <summary>
  253. /// Gets or sets the bit depth.
  254. /// </summary>
  255. /// <value>The bit depth.</value>
  256. public int? BitDepth { get; set; }
  257. /// <summary>
  258. /// Gets or sets the reference frames.
  259. /// </summary>
  260. /// <value>The reference frames.</value>
  261. public int? RefFrames { get; set; }
  262. /// <summary>
  263. /// Gets or sets the length of the packet.
  264. /// </summary>
  265. /// <value>The length of the packet.</value>
  266. public int? PacketLength { get; set; }
  267. /// <summary>
  268. /// Gets or sets the channels.
  269. /// </summary>
  270. /// <value>The channels.</value>
  271. public int? Channels { get; set; }
  272. /// <summary>
  273. /// Gets or sets the sample rate.
  274. /// </summary>
  275. /// <value>The sample rate.</value>
  276. public int? SampleRate { get; set; }
  277. /// <summary>
  278. /// Gets or sets a value indicating whether this instance is default.
  279. /// </summary>
  280. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  281. public bool IsDefault { get; set; }
  282. /// <summary>
  283. /// Gets or sets a value indicating whether this instance is forced.
  284. /// </summary>
  285. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  286. public bool IsForced { get; set; }
  287. /// <summary>
  288. /// Gets or sets the height.
  289. /// </summary>
  290. /// <value>The height.</value>
  291. public int? Height { get; set; }
  292. /// <summary>
  293. /// Gets or sets the width.
  294. /// </summary>
  295. /// <value>The width.</value>
  296. public int? Width { get; set; }
  297. /// <summary>
  298. /// Gets or sets the average frame rate.
  299. /// </summary>
  300. /// <value>The average frame rate.</value>
  301. public float? AverageFrameRate { get; set; }
  302. /// <summary>
  303. /// Gets or sets the real frame rate.
  304. /// </summary>
  305. /// <value>The real frame rate.</value>
  306. public float? RealFrameRate { get; set; }
  307. /// <summary>
  308. /// Gets or sets the profile.
  309. /// </summary>
  310. /// <value>The profile.</value>
  311. public string Profile { get; set; }
  312. /// <summary>
  313. /// Gets or sets the type.
  314. /// </summary>
  315. /// <value>The type.</value>
  316. public MediaStreamType Type { get; set; }
  317. /// <summary>
  318. /// Gets or sets the aspect ratio.
  319. /// </summary>
  320. /// <value>The aspect ratio.</value>
  321. public string AspectRatio { get; set; }
  322. /// <summary>
  323. /// Gets or sets the index.
  324. /// </summary>
  325. /// <value>The index.</value>
  326. public int Index { get; set; }
  327. /// <summary>
  328. /// Gets or sets the score.
  329. /// </summary>
  330. /// <value>The score.</value>
  331. public int? Score { get; set; }
  332. /// <summary>
  333. /// Gets or sets a value indicating whether this instance is external.
  334. /// </summary>
  335. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  336. public bool IsExternal { get; set; }
  337. /// <summary>
  338. /// Gets or sets the method.
  339. /// </summary>
  340. /// <value>The method.</value>
  341. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  342. /// <summary>
  343. /// Gets or sets the delivery URL.
  344. /// </summary>
  345. /// <value>The delivery URL.</value>
  346. public string DeliveryUrl { get; set; }
  347. /// <summary>
  348. /// Gets or sets a value indicating whether this instance is external URL.
  349. /// </summary>
  350. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  351. public bool? IsExternalUrl { get; set; }
  352. public bool IsTextSubtitleStream
  353. {
  354. get
  355. {
  356. if (Type != MediaStreamType.Subtitle)
  357. {
  358. return false;
  359. }
  360. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  361. {
  362. return false;
  363. }
  364. return IsTextFormat(Codec);
  365. }
  366. }
  367. /// <summary>
  368. /// Gets or sets a value indicating whether [supports external stream].
  369. /// </summary>
  370. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  371. public bool SupportsExternalStream { get; set; }
  372. /// <summary>
  373. /// Gets or sets the filename.
  374. /// </summary>
  375. /// <value>The filename.</value>
  376. public string Path { get; set; }
  377. /// <summary>
  378. /// Gets or sets the pixel format.
  379. /// </summary>
  380. /// <value>The pixel format.</value>
  381. public string PixelFormat { get; set; }
  382. /// <summary>
  383. /// Gets or sets the level.
  384. /// </summary>
  385. /// <value>The level.</value>
  386. public double? Level { get; set; }
  387. /// <summary>
  388. /// Gets or sets whether this instance is anamorphic.
  389. /// </summary>
  390. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  391. public bool? IsAnamorphic { get; set; }
  392. internal string GetResolutionText()
  393. {
  394. if (!Width.HasValue || !Height.HasValue)
  395. {
  396. return null;
  397. }
  398. return Width switch
  399. {
  400. <= 720 when Height <= 480 => IsInterlaced ? "480i" : "480p",
  401. // 720x576 (PAL) (768 when rescaled for square pixels)
  402. <= 768 when Height <= 576 => IsInterlaced ? "576i" : "576p",
  403. // 960x540 (sometimes 544 which is multiple of 16)
  404. <= 960 when Height <= 544 => IsInterlaced ? "540i" : "540p",
  405. // 1280x720
  406. <= 1280 when Height <= 962 => IsInterlaced ? "720i" : "720p",
  407. // 1920x1080
  408. <= 1920 when Height <= 1440 => IsInterlaced ? "1080i" : "1080p",
  409. // 4K
  410. <= 4096 when Height <= 3072 => "4K",
  411. // 8K
  412. <= 8192 when Height <= 6144 => "8K",
  413. _ => null
  414. };
  415. }
  416. public static bool IsTextFormat(string format)
  417. {
  418. string codec = format ?? string.Empty;
  419. // sub = external .sub file
  420. return !codec.Contains("pgs", StringComparison.OrdinalIgnoreCase) &&
  421. !codec.Contains("dvd", StringComparison.OrdinalIgnoreCase) &&
  422. !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase) &&
  423. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  424. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  425. }
  426. public bool SupportsSubtitleConversionTo(string toCodec)
  427. {
  428. if (!IsTextSubtitleStream)
  429. {
  430. return false;
  431. }
  432. var fromCodec = Codec;
  433. // Can't convert from this
  434. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  435. {
  436. return false;
  437. }
  438. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  439. {
  440. return false;
  441. }
  442. // Can't convert to this
  443. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  444. {
  445. return false;
  446. }
  447. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  448. {
  449. return false;
  450. }
  451. return true;
  452. }
  453. }
  454. }