MediaStream.cs 17 KB

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