2
0

MediaStream.cs 16 KB

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