MediaStream.cs 15 KB

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