MediaStream.cs 14 KB

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