MediaStream.cs 14 KB

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