MediaStream.cs 14 KB

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