MediaStream.cs 18 KB

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