MediaStream.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. return "SDR";
  92. }
  93. }
  94. public string LocalizedUndefined { get; set; }
  95. public string LocalizedDefault { get; set; }
  96. public string LocalizedForced { get; set; }
  97. public string DisplayTitle
  98. {
  99. get
  100. {
  101. switch (Type)
  102. {
  103. case MediaStreamType.Audio:
  104. {
  105. var attributes = new List<string>();
  106. if (!string.IsNullOrEmpty(Language))
  107. {
  108. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  109. string fullLanguage = CultureInfo
  110. .GetCultures(CultureTypes.NeutralCultures)
  111. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  112. ?.DisplayName;
  113. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  114. }
  115. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase))
  116. {
  117. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  118. }
  119. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  120. {
  121. attributes.Add(Profile);
  122. }
  123. if (!string.IsNullOrEmpty(ChannelLayout))
  124. {
  125. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  126. }
  127. else if (Channels.HasValue)
  128. {
  129. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  130. }
  131. if (IsDefault)
  132. {
  133. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  134. }
  135. if (!string.IsNullOrEmpty(Title))
  136. {
  137. var result = new StringBuilder(Title);
  138. foreach (var tag in attributes)
  139. {
  140. // Keep Tags that are not already in Title.
  141. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  142. {
  143. result.Append(" - ").Append(tag);
  144. }
  145. }
  146. return result.ToString();
  147. }
  148. return string.Join(" - ", attributes);
  149. }
  150. case MediaStreamType.Video:
  151. {
  152. var attributes = new List<string>();
  153. var resolutionText = GetResolutionText();
  154. if (!string.IsNullOrEmpty(resolutionText))
  155. {
  156. attributes.Add(resolutionText);
  157. }
  158. if (!string.IsNullOrEmpty(Codec))
  159. {
  160. attributes.Add(Codec.ToUpperInvariant());
  161. }
  162. if (!string.IsNullOrEmpty(VideoRange))
  163. {
  164. attributes.Add(VideoRange.ToUpperInvariant());
  165. }
  166. if (!string.IsNullOrEmpty(Title))
  167. {
  168. var result = new StringBuilder(Title);
  169. foreach (var tag in attributes)
  170. {
  171. // Keep Tags that are not already in Title.
  172. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  173. {
  174. result.Append(" - ").Append(tag);
  175. }
  176. }
  177. return result.ToString();
  178. }
  179. return string.Join(' ', attributes);
  180. }
  181. case MediaStreamType.Subtitle:
  182. {
  183. var attributes = new List<string>();
  184. if (!string.IsNullOrEmpty(Language))
  185. {
  186. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  187. string fullLanguage = CultureInfo
  188. .GetCultures(CultureTypes.NeutralCultures)
  189. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  190. ?.DisplayName;
  191. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  192. }
  193. else
  194. {
  195. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  196. }
  197. if (IsDefault)
  198. {
  199. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  200. }
  201. if (IsForced)
  202. {
  203. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  204. }
  205. if (!string.IsNullOrEmpty(Title))
  206. {
  207. var result = new StringBuilder(Title);
  208. foreach (var tag in attributes)
  209. {
  210. // Keep Tags that are not already in Title.
  211. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  212. {
  213. result.Append(" - ").Append(tag);
  214. }
  215. }
  216. return result.ToString();
  217. }
  218. return string.Join(" - ", attributes);
  219. }
  220. default:
  221. return null;
  222. }
  223. }
  224. }
  225. public string NalLengthSize { get; set; }
  226. /// <summary>
  227. /// Gets or sets a value indicating whether this instance is interlaced.
  228. /// </summary>
  229. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  230. public bool IsInterlaced { get; set; }
  231. public bool? IsAVC { get; set; }
  232. /// <summary>
  233. /// Gets or sets the channel layout.
  234. /// </summary>
  235. /// <value>The channel layout.</value>
  236. public string ChannelLayout { get; set; }
  237. /// <summary>
  238. /// Gets or sets the bit rate.
  239. /// </summary>
  240. /// <value>The bit rate.</value>
  241. public int? BitRate { get; set; }
  242. /// <summary>
  243. /// Gets or sets the bit depth.
  244. /// </summary>
  245. /// <value>The bit depth.</value>
  246. public int? BitDepth { get; set; }
  247. /// <summary>
  248. /// Gets or sets the reference frames.
  249. /// </summary>
  250. /// <value>The reference frames.</value>
  251. public int? RefFrames { get; set; }
  252. /// <summary>
  253. /// Gets or sets the length of the packet.
  254. /// </summary>
  255. /// <value>The length of the packet.</value>
  256. public int? PacketLength { get; set; }
  257. /// <summary>
  258. /// Gets or sets the channels.
  259. /// </summary>
  260. /// <value>The channels.</value>
  261. public int? Channels { get; set; }
  262. /// <summary>
  263. /// Gets or sets the sample rate.
  264. /// </summary>
  265. /// <value>The sample rate.</value>
  266. public int? SampleRate { get; set; }
  267. /// <summary>
  268. /// Gets or sets a value indicating whether this instance is default.
  269. /// </summary>
  270. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  271. public bool IsDefault { get; set; }
  272. /// <summary>
  273. /// Gets or sets a value indicating whether this instance is forced.
  274. /// </summary>
  275. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  276. public bool IsForced { get; set; }
  277. /// <summary>
  278. /// Gets or sets the height.
  279. /// </summary>
  280. /// <value>The height.</value>
  281. public int? Height { get; set; }
  282. /// <summary>
  283. /// Gets or sets the width.
  284. /// </summary>
  285. /// <value>The width.</value>
  286. public int? Width { get; set; }
  287. /// <summary>
  288. /// Gets or sets the average frame rate.
  289. /// </summary>
  290. /// <value>The average frame rate.</value>
  291. public float? AverageFrameRate { get; set; }
  292. /// <summary>
  293. /// Gets or sets the real frame rate.
  294. /// </summary>
  295. /// <value>The real frame rate.</value>
  296. public float? RealFrameRate { get; set; }
  297. /// <summary>
  298. /// Gets or sets the profile.
  299. /// </summary>
  300. /// <value>The profile.</value>
  301. public string Profile { get; set; }
  302. /// <summary>
  303. /// Gets or sets the type.
  304. /// </summary>
  305. /// <value>The type.</value>
  306. public MediaStreamType Type { get; set; }
  307. /// <summary>
  308. /// Gets or sets the aspect ratio.
  309. /// </summary>
  310. /// <value>The aspect ratio.</value>
  311. public string AspectRatio { get; set; }
  312. /// <summary>
  313. /// Gets or sets the index.
  314. /// </summary>
  315. /// <value>The index.</value>
  316. public int Index { get; set; }
  317. /// <summary>
  318. /// Gets or sets the score.
  319. /// </summary>
  320. /// <value>The score.</value>
  321. public int? Score { get; set; }
  322. /// <summary>
  323. /// Gets or sets a value indicating whether this instance is external.
  324. /// </summary>
  325. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  326. public bool IsExternal { get; set; }
  327. /// <summary>
  328. /// Gets or sets the method.
  329. /// </summary>
  330. /// <value>The method.</value>
  331. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  332. /// <summary>
  333. /// Gets or sets the delivery URL.
  334. /// </summary>
  335. /// <value>The delivery URL.</value>
  336. public string DeliveryUrl { get; set; }
  337. /// <summary>
  338. /// Gets or sets a value indicating whether this instance is external URL.
  339. /// </summary>
  340. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  341. public bool? IsExternalUrl { get; set; }
  342. public bool IsTextSubtitleStream
  343. {
  344. get
  345. {
  346. if (Type != MediaStreamType.Subtitle)
  347. {
  348. return false;
  349. }
  350. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  351. {
  352. return false;
  353. }
  354. return IsTextFormat(Codec);
  355. }
  356. }
  357. /// <summary>
  358. /// Gets or sets a value indicating whether [supports external stream].
  359. /// </summary>
  360. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  361. public bool SupportsExternalStream { get; set; }
  362. /// <summary>
  363. /// Gets or sets the filename.
  364. /// </summary>
  365. /// <value>The filename.</value>
  366. public string Path { get; set; }
  367. /// <summary>
  368. /// Gets or sets the pixel format.
  369. /// </summary>
  370. /// <value>The pixel format.</value>
  371. public string PixelFormat { get; set; }
  372. /// <summary>
  373. /// Gets or sets the level.
  374. /// </summary>
  375. /// <value>The level.</value>
  376. public double? Level { get; set; }
  377. /// <summary>
  378. /// Gets or sets whether this instance is anamorphic.
  379. /// </summary>
  380. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  381. public bool? IsAnamorphic { get; set; }
  382. private string GetResolutionText()
  383. {
  384. var i = this;
  385. if (i.Width.HasValue && i.Height.HasValue)
  386. {
  387. var width = i.Width.Value;
  388. var height = i.Height.Value;
  389. if (width >= 3800 || height >= 2000)
  390. {
  391. return "4K";
  392. }
  393. if (width >= 2500)
  394. {
  395. if (i.IsInterlaced)
  396. {
  397. return "1440i";
  398. }
  399. return "1440p";
  400. }
  401. if (width >= 1900 || height >= 1000)
  402. {
  403. if (i.IsInterlaced)
  404. {
  405. return "1080i";
  406. }
  407. return "1080p";
  408. }
  409. if (width >= 1260 || height >= 700)
  410. {
  411. if (i.IsInterlaced)
  412. {
  413. return "720i";
  414. }
  415. return "720p";
  416. }
  417. if (width >= 700 || height >= 440)
  418. {
  419. if (i.IsInterlaced)
  420. {
  421. return "480i";
  422. }
  423. return "480p";
  424. }
  425. return "SD";
  426. }
  427. return null;
  428. }
  429. public static bool IsTextFormat(string format)
  430. {
  431. string codec = format ?? string.Empty;
  432. // sub = external .sub file
  433. return codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) == -1 &&
  434. codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) == -1 &&
  435. codec.IndexOf("dvbsub", StringComparison.OrdinalIgnoreCase) == -1 &&
  436. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  437. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  438. }
  439. public bool SupportsSubtitleConversionTo(string toCodec)
  440. {
  441. if (!IsTextSubtitleStream)
  442. {
  443. return false;
  444. }
  445. var fromCodec = Codec;
  446. // Can't convert from this
  447. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  448. {
  449. return false;
  450. }
  451. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  452. {
  453. return false;
  454. }
  455. // Can't convert to this
  456. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  457. {
  458. return false;
  459. }
  460. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  461. {
  462. return false;
  463. }
  464. return true;
  465. }
  466. }
  467. }