MediaStream.cs 16 KB

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