MediaStream.cs 14 KB

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