MediaStream.cs 14 KB

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