StreamInfo.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. using MediaBrowser.Model.Drawing;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Extensions;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. namespace MediaBrowser.Model.Dlna
  12. {
  13. /// <summary>
  14. /// Class StreamInfo.
  15. /// </summary>
  16. public class StreamInfo
  17. {
  18. public StreamInfo()
  19. {
  20. AudioCodecs = new string[] { };
  21. VideoCodecs = new string[] { };
  22. SubtitleCodecs = new string[] { };
  23. TranscodeReasons = new List<TranscodeReason>();
  24. StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  25. }
  26. public void SetOption(string qualifier, string name, string value)
  27. {
  28. SetOption(qualifier + "-" + name, value);
  29. }
  30. public void SetOption(string name, string value)
  31. {
  32. StreamOptions[name] = value;
  33. }
  34. public string GetOption(string qualifier, string name)
  35. {
  36. return GetOption(qualifier + "-" + name);
  37. }
  38. public string GetOption(string name)
  39. {
  40. string value;
  41. if (StreamOptions.TryGetValue(name, out value))
  42. {
  43. return value;
  44. }
  45. return null;
  46. }
  47. public string ItemId { get; set; }
  48. public PlayMethod PlayMethod { get; set; }
  49. public EncodingContext Context { get; set; }
  50. public DlnaProfileType MediaType { get; set; }
  51. public string Container { get; set; }
  52. public string SubProtocol { get; set; }
  53. public long StartPositionTicks { get; set; }
  54. public int? SegmentLength { get; set; }
  55. public int? MinSegments { get; set; }
  56. public bool BreakOnNonKeyFrames { get; set; }
  57. public bool RequireAvc { get; set; }
  58. public bool RequireNonAnamorphic { get; set; }
  59. public bool CopyTimestamps { get; set; }
  60. public bool EnableSubtitlesInManifest { get; set; }
  61. public string[] AudioCodecs { get; set; }
  62. public string[] VideoCodecs { get; set; }
  63. public int? AudioStreamIndex { get; set; }
  64. public int? SubtitleStreamIndex { get; set; }
  65. public int? TranscodingMaxAudioChannels { get; set; }
  66. public int? MaxAudioChannels { get; set; }
  67. public int? AudioBitrate { get; set; }
  68. public int? VideoBitrate { get; set; }
  69. public int? MaxWidth { get; set; }
  70. public int? MaxHeight { get; set; }
  71. public int? MaxVideoBitDepth { get; set; }
  72. public float? MaxFramerate { get; set; }
  73. public DeviceProfile DeviceProfile { get; set; }
  74. public string DeviceProfileId { get; set; }
  75. public string DeviceId { get; set; }
  76. public long? RunTimeTicks { get; set; }
  77. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  78. public bool EstimateContentLength { get; set; }
  79. public MediaSourceInfo MediaSource { get; set; }
  80. public string[] SubtitleCodecs { get; set; }
  81. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  82. public string SubtitleFormat { get; set; }
  83. public string PlaySessionId { get; set; }
  84. public List<MediaSourceInfo> AllMediaSources { get; set; }
  85. public List<TranscodeReason> TranscodeReasons { get; set; }
  86. public Dictionary<string, string> StreamOptions { get; private set; }
  87. public string MediaSourceId
  88. {
  89. get
  90. {
  91. return MediaSource == null ? null : MediaSource.Id;
  92. }
  93. }
  94. public bool IsDirectStream
  95. {
  96. get
  97. {
  98. return PlayMethod == PlayMethod.DirectStream ||
  99. PlayMethod == PlayMethod.DirectPlay;
  100. }
  101. }
  102. public string ToUrl(string baseUrl, string accessToken)
  103. {
  104. if (PlayMethod == PlayMethod.DirectPlay)
  105. {
  106. return MediaSource.Path;
  107. }
  108. if (string.IsNullOrEmpty(baseUrl))
  109. {
  110. throw new ArgumentNullException(baseUrl);
  111. }
  112. List<string> list = new List<string>();
  113. foreach (NameValuePair pair in BuildParams(this, accessToken, false))
  114. {
  115. if (string.IsNullOrEmpty(pair.Value))
  116. {
  117. continue;
  118. }
  119. // Try to keep the url clean by omitting defaults
  120. if (StringHelper.EqualsIgnoreCase(pair.Name, "StartTimeTicks") &&
  121. StringHelper.EqualsIgnoreCase(pair.Value, "0"))
  122. {
  123. continue;
  124. }
  125. if (StringHelper.EqualsIgnoreCase(pair.Name, "SubtitleStreamIndex") &&
  126. StringHelper.EqualsIgnoreCase(pair.Value, "-1"))
  127. {
  128. continue;
  129. }
  130. if (StringHelper.EqualsIgnoreCase(pair.Name, "Static") &&
  131. StringHelper.EqualsIgnoreCase(pair.Value, "false"))
  132. {
  133. continue;
  134. }
  135. var encodedValue = pair.Value.Replace(" ", "%20");
  136. list.Add(string.Format("{0}={1}", pair.Name, encodedValue));
  137. }
  138. string queryString = string.Join("&", list.ToArray(list.Count));
  139. return GetUrl(baseUrl, queryString);
  140. }
  141. public string ToDlnaUrl(string baseUrl, string accessToken)
  142. {
  143. if (PlayMethod == PlayMethod.DirectPlay)
  144. {
  145. return MediaSource.Path;
  146. }
  147. string dlnaCommand = BuildDlnaParam(this, accessToken);
  148. return GetUrl(baseUrl, dlnaCommand);
  149. }
  150. private string GetUrl(string baseUrl, string queryString)
  151. {
  152. if (string.IsNullOrEmpty(baseUrl))
  153. {
  154. throw new ArgumentNullException(baseUrl);
  155. }
  156. string extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  157. baseUrl = baseUrl.TrimEnd('/');
  158. if (MediaType == DlnaProfileType.Audio)
  159. {
  160. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  161. {
  162. return string.Format("{0}/audio/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  163. }
  164. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  165. }
  166. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  167. {
  168. return string.Format("{0}/videos/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  169. }
  170. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  171. }
  172. private static string BuildDlnaParam(StreamInfo item, string accessToken)
  173. {
  174. List<string> list = new List<string>();
  175. foreach (NameValuePair pair in BuildParams(item, accessToken, true))
  176. {
  177. list.Add(pair.Value);
  178. }
  179. return string.Format("Params={0}", string.Join(";", list.ToArray(list.Count)));
  180. }
  181. private static List<NameValuePair> BuildParams(StreamInfo item, string accessToken, bool isDlna)
  182. {
  183. List<NameValuePair> list = new List<NameValuePair>();
  184. string audioCodecs = item.AudioCodecs.Length == 0 ?
  185. string.Empty :
  186. string.Join(",", item.AudioCodecs);
  187. string videoCodecs = item.VideoCodecs.Length == 0 ?
  188. string.Empty :
  189. string.Join(",", item.VideoCodecs);
  190. list.Add(new NameValuePair("DeviceProfileId", item.DeviceProfileId ?? string.Empty));
  191. list.Add(new NameValuePair("DeviceId", item.DeviceId ?? string.Empty));
  192. list.Add(new NameValuePair("MediaSourceId", item.MediaSourceId ?? string.Empty));
  193. list.Add(new NameValuePair("Static", item.IsDirectStream.ToString().ToLower()));
  194. list.Add(new NameValuePair("VideoCodec", videoCodecs));
  195. list.Add(new NameValuePair("AudioCodec", audioCodecs));
  196. list.Add(new NameValuePair("AudioStreamIndex", item.AudioStreamIndex.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioStreamIndex.Value) : string.Empty));
  197. list.Add(new NameValuePair("SubtitleStreamIndex", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? StringHelper.ToStringCultureInvariant(item.SubtitleStreamIndex.Value) : string.Empty));
  198. list.Add(new NameValuePair("VideoBitrate", item.VideoBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoBitrate.Value) : string.Empty));
  199. list.Add(new NameValuePair("AudioBitrate", item.AudioBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioBitrate.Value) : string.Empty));
  200. list.Add(new NameValuePair("MaxAudioChannels", item.MaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxAudioChannels.Value) : string.Empty));
  201. list.Add(new NameValuePair("MaxFramerate", item.MaxFramerate.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxFramerate.Value) : string.Empty));
  202. list.Add(new NameValuePair("MaxWidth", item.MaxWidth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxWidth.Value) : string.Empty));
  203. list.Add(new NameValuePair("MaxHeight", item.MaxHeight.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxHeight.Value) : string.Empty));
  204. long startPositionTicks = item.StartPositionTicks;
  205. var isHls = StringHelper.EqualsIgnoreCase(item.SubProtocol, "hls");
  206. if (isHls)
  207. {
  208. list.Add(new NameValuePair("StartTimeTicks", string.Empty));
  209. }
  210. else
  211. {
  212. list.Add(new NameValuePair("StartTimeTicks", StringHelper.ToStringCultureInvariant(startPositionTicks)));
  213. }
  214. if (isDlna)
  215. {
  216. // hack alert
  217. // dlna needs to be update to support the qualified params
  218. var level = item.GetTargetVideoLevel("h264");
  219. list.Add(new NameValuePair("Level", level.HasValue ? StringHelper.ToStringCultureInvariant(level.Value) : string.Empty));
  220. }
  221. if (isDlna)
  222. {
  223. // hack alert
  224. // dlna needs to be update to support the qualified params
  225. var refframes = item.GetTargetRefFrames("h264");
  226. list.Add(new NameValuePair("MaxRefFrames", refframes.HasValue ? StringHelper.ToStringCultureInvariant(refframes.Value) : string.Empty));
  227. }
  228. list.Add(new NameValuePair("MaxVideoBitDepth", item.MaxVideoBitDepth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxVideoBitDepth.Value) : string.Empty));
  229. if (isDlna)
  230. {
  231. // hack alert
  232. // dlna needs to be update to support the qualified params
  233. var profile = item.GetOption("h264", "profile");
  234. list.Add(new NameValuePair("Profile", profile ?? string.Empty));
  235. }
  236. // no longer used
  237. list.Add(new NameValuePair("Cabac", string.Empty));
  238. list.Add(new NameValuePair("PlaySessionId", item.PlaySessionId ?? string.Empty));
  239. list.Add(new NameValuePair("api_key", accessToken ?? string.Empty));
  240. string liveStreamId = item.MediaSource == null ? null : item.MediaSource.LiveStreamId;
  241. list.Add(new NameValuePair("LiveStreamId", liveStreamId ?? string.Empty));
  242. if (isDlna)
  243. {
  244. list.Add(new NameValuePair("ItemId", item.ItemId));
  245. }
  246. list.Add(new NameValuePair("CopyTimestamps", item.CopyTimestamps.ToString().ToLower()));
  247. list.Add(new NameValuePair("SubtitleMethod", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? item.SubtitleDeliveryMethod.ToString() : string.Empty));
  248. list.Add(new NameValuePair("TranscodingMaxAudioChannels", item.TranscodingMaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.TranscodingMaxAudioChannels.Value) : string.Empty));
  249. list.Add(new NameValuePair("EnableSubtitlesInManifest", item.EnableSubtitlesInManifest.ToString().ToLower()));
  250. list.Add(new NameValuePair("Tag", item.MediaSource.ETag ?? string.Empty));
  251. list.Add(new NameValuePair("RequireAvc", item.RequireAvc.ToString().ToLower()));
  252. string subtitleCodecs = item.SubtitleCodecs.Length == 0 ?
  253. string.Empty :
  254. string.Join(",", item.SubtitleCodecs);
  255. list.Add(new NameValuePair("SubtitleCodec", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Embed ? subtitleCodecs : string.Empty));
  256. list.Add(new NameValuePair("RequireNonAnamorphic", item.RequireNonAnamorphic.ToString().ToLower()));
  257. if (isDlna)
  258. {
  259. // hack alert
  260. // dlna needs to be update to support the qualified params
  261. var deinterlace = string.Equals(item.GetOption("h264", "deinterlace"), "true", StringComparison.OrdinalIgnoreCase) ||
  262. string.Equals(item.GetOption("mpeg2video", "deinterlace"), "true", StringComparison.OrdinalIgnoreCase);
  263. list.Add(new NameValuePair("DeInterlace", deinterlace.ToString().ToLower()));
  264. }
  265. if (!isDlna && isHls)
  266. {
  267. list.Add(new NameValuePair("SegmentContainer", item.Container ?? string.Empty));
  268. if (item.SegmentLength.HasValue)
  269. {
  270. list.Add(new NameValuePair("SegmentLength", item.SegmentLength.Value.ToString(CultureInfo.InvariantCulture)));
  271. }
  272. if (item.MinSegments.HasValue)
  273. {
  274. list.Add(new NameValuePair("MinSegments", item.MinSegments.Value.ToString(CultureInfo.InvariantCulture)));
  275. }
  276. list.Add(new NameValuePair("BreakOnNonKeyFrames", item.BreakOnNonKeyFrames.ToString()));
  277. }
  278. if (isDlna || !item.IsDirectStream)
  279. {
  280. list.Add(new NameValuePair("TranscodeReasons", string.Join(",", item.TranscodeReasons.Distinct().Select(i => i.ToString()).ToArray())));
  281. }
  282. if (!isDlna)
  283. {
  284. foreach (var pair in item.StreamOptions)
  285. {
  286. if (string.IsNullOrWhiteSpace(pair.Value))
  287. {
  288. continue;
  289. }
  290. list.Add(new NameValuePair(pair.Key, pair.Value));
  291. }
  292. }
  293. return list;
  294. }
  295. public List<SubtitleStreamInfo> GetExternalSubtitles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  296. {
  297. return GetExternalSubtitles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken);
  298. }
  299. public List<SubtitleStreamInfo> GetExternalSubtitles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  300. {
  301. List<SubtitleStreamInfo> list = GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, enableAllProfiles, baseUrl, accessToken);
  302. List<SubtitleStreamInfo> newList = new List<SubtitleStreamInfo>();
  303. // First add the selected track
  304. foreach (SubtitleStreamInfo stream in list)
  305. {
  306. if (stream.DeliveryMethod == SubtitleDeliveryMethod.External)
  307. {
  308. newList.Add(stream);
  309. }
  310. }
  311. return newList;
  312. }
  313. public List<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  314. {
  315. return GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken);
  316. }
  317. public List<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  318. {
  319. List<SubtitleStreamInfo> list = new List<SubtitleStreamInfo>();
  320. // HLS will preserve timestamps so we can just grab the full subtitle stream
  321. long startPositionTicks = StringHelper.EqualsIgnoreCase(SubProtocol, "hls")
  322. ? 0
  323. : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0);
  324. // First add the selected track
  325. if (SubtitleStreamIndex.HasValue)
  326. {
  327. foreach (MediaStream stream in MediaSource.MediaStreams)
  328. {
  329. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  330. {
  331. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  332. }
  333. }
  334. }
  335. if (!includeSelectedTrackOnly)
  336. {
  337. foreach (MediaStream stream in MediaSource.MediaStreams)
  338. {
  339. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  340. {
  341. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  342. }
  343. }
  344. }
  345. return list;
  346. }
  347. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, ITranscoderSupport transcoderSupport, bool enableAllProfiles, string baseUrl, string accessToken, long startPositionTicks)
  348. {
  349. if (enableAllProfiles)
  350. {
  351. foreach (SubtitleProfile profile in DeviceProfile.SubtitleProfiles)
  352. {
  353. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile }, transcoderSupport);
  354. list.Add(info);
  355. }
  356. }
  357. else
  358. {
  359. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles, transcoderSupport);
  360. list.Add(info);
  361. }
  362. }
  363. private SubtitleStreamInfo GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles, ITranscoderSupport transcoderSupport)
  364. {
  365. SubtitleProfile subtitleProfile = StreamBuilder.GetSubtitleProfile(stream, subtitleProfiles, PlayMethod, transcoderSupport, SubProtocol, Container);
  366. SubtitleStreamInfo info = new SubtitleStreamInfo
  367. {
  368. IsForced = stream.IsForced,
  369. Language = stream.Language,
  370. Name = stream.Language ?? "Unknown",
  371. Format = subtitleProfile.Format,
  372. Index = stream.Index,
  373. DeliveryMethod = subtitleProfile.Method,
  374. DisplayTitle = stream.DisplayTitle
  375. };
  376. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  377. {
  378. if (MediaSource.Protocol == MediaProtocol.File || !StringHelper.EqualsIgnoreCase(stream.Codec, subtitleProfile.Format))
  379. {
  380. info.Url = string.Format("{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  381. baseUrl,
  382. ItemId,
  383. MediaSourceId,
  384. StringHelper.ToStringCultureInvariant(stream.Index),
  385. StringHelper.ToStringCultureInvariant(startPositionTicks),
  386. subtitleProfile.Format);
  387. if (!string.IsNullOrEmpty(accessToken))
  388. {
  389. info.Url += "?api_key=" + accessToken;
  390. }
  391. info.IsExternalUrl = false;
  392. }
  393. else
  394. {
  395. info.Url = stream.Path;
  396. info.IsExternalUrl = true;
  397. }
  398. }
  399. return info;
  400. }
  401. /// <summary>
  402. /// Returns the audio stream that will be used
  403. /// </summary>
  404. public MediaStream TargetAudioStream
  405. {
  406. get
  407. {
  408. if (MediaSource != null)
  409. {
  410. return MediaSource.GetDefaultAudioStream(AudioStreamIndex);
  411. }
  412. return null;
  413. }
  414. }
  415. /// <summary>
  416. /// Returns the video stream that will be used
  417. /// </summary>
  418. public MediaStream TargetVideoStream
  419. {
  420. get
  421. {
  422. if (MediaSource != null)
  423. {
  424. return MediaSource.VideoStream;
  425. }
  426. return null;
  427. }
  428. }
  429. /// <summary>
  430. /// Predicts the audio sample rate that will be in the output stream
  431. /// </summary>
  432. public int? TargetAudioSampleRate
  433. {
  434. get
  435. {
  436. MediaStream stream = TargetAudioStream;
  437. return stream == null ? null : stream.SampleRate;
  438. }
  439. }
  440. /// <summary>
  441. /// Predicts the audio sample rate that will be in the output stream
  442. /// </summary>
  443. public int? TargetAudioBitDepth
  444. {
  445. get
  446. {
  447. MediaStream stream = TargetAudioStream;
  448. return stream == null ? null : stream.BitDepth;
  449. }
  450. }
  451. /// <summary>
  452. /// Predicts the audio sample rate that will be in the output stream
  453. /// </summary>
  454. public int? TargetVideoBitDepth
  455. {
  456. get
  457. {
  458. MediaStream stream = TargetVideoStream;
  459. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  460. }
  461. }
  462. /// <summary>
  463. /// Gets the target reference frames.
  464. /// </summary>
  465. /// <value>The target reference frames.</value>
  466. public int? TargetRefFrames
  467. {
  468. get
  469. {
  470. if (IsDirectStream)
  471. {
  472. return TargetVideoStream == null ? (int?)null : TargetVideoStream.RefFrames;
  473. }
  474. var targetVideoCodecs = TargetVideoCodec;
  475. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  476. if (!string.IsNullOrWhiteSpace(videoCodec))
  477. {
  478. return GetTargetRefFrames(videoCodec);
  479. }
  480. return TargetVideoStream == null ? (int?)null : TargetVideoStream.RefFrames;
  481. }
  482. }
  483. /// <summary>
  484. /// Predicts the audio sample rate that will be in the output stream
  485. /// </summary>
  486. public float? TargetFramerate
  487. {
  488. get
  489. {
  490. MediaStream stream = TargetVideoStream;
  491. return MaxFramerate.HasValue && !IsDirectStream
  492. ? MaxFramerate
  493. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  494. }
  495. }
  496. /// <summary>
  497. /// Predicts the audio sample rate that will be in the output stream
  498. /// </summary>
  499. public double? TargetVideoLevel
  500. {
  501. get
  502. {
  503. if (IsDirectStream)
  504. {
  505. return TargetVideoStream == null ? (double?)null : TargetVideoStream.Level;
  506. }
  507. var targetVideoCodecs = TargetVideoCodec;
  508. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  509. if (!string.IsNullOrWhiteSpace(videoCodec))
  510. {
  511. return GetTargetVideoLevel(videoCodec);
  512. }
  513. return TargetVideoStream == null ? (double?)null : TargetVideoStream.Level;
  514. }
  515. }
  516. public double? GetTargetVideoLevel(string codec)
  517. {
  518. var value = GetOption(codec, "level");
  519. if (string.IsNullOrWhiteSpace(value))
  520. {
  521. return null;
  522. }
  523. double result;
  524. if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
  525. {
  526. return result;
  527. }
  528. return null;
  529. }
  530. public int? GetTargetRefFrames(string codec)
  531. {
  532. var value = GetOption(codec, "maxrefframes");
  533. if (string.IsNullOrWhiteSpace(value))
  534. {
  535. return null;
  536. }
  537. int result;
  538. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
  539. {
  540. return result;
  541. }
  542. return null;
  543. }
  544. /// <summary>
  545. /// Predicts the audio sample rate that will be in the output stream
  546. /// </summary>
  547. public int? TargetPacketLength
  548. {
  549. get
  550. {
  551. MediaStream stream = TargetVideoStream;
  552. return !IsDirectStream
  553. ? null
  554. : stream == null ? null : stream.PacketLength;
  555. }
  556. }
  557. /// <summary>
  558. /// Predicts the audio sample rate that will be in the output stream
  559. /// </summary>
  560. public string TargetVideoProfile
  561. {
  562. get
  563. {
  564. if (IsDirectStream)
  565. {
  566. return TargetVideoStream == null ? null : TargetVideoStream.Profile;
  567. }
  568. var targetVideoCodecs = TargetVideoCodec;
  569. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  570. if (!string.IsNullOrWhiteSpace(videoCodec))
  571. {
  572. return GetOption(videoCodec, "profile");
  573. }
  574. return TargetVideoStream == null ? null : TargetVideoStream.Profile;
  575. }
  576. }
  577. /// <summary>
  578. /// Gets the target video codec tag.
  579. /// </summary>
  580. /// <value>The target video codec tag.</value>
  581. public string TargetVideoCodecTag
  582. {
  583. get
  584. {
  585. MediaStream stream = TargetVideoStream;
  586. return !IsDirectStream
  587. ? null
  588. : stream == null ? null : stream.CodecTag;
  589. }
  590. }
  591. /// <summary>
  592. /// Predicts the audio bitrate that will be in the output stream
  593. /// </summary>
  594. public int? TargetAudioBitrate
  595. {
  596. get
  597. {
  598. MediaStream stream = TargetAudioStream;
  599. return AudioBitrate.HasValue && !IsDirectStream
  600. ? AudioBitrate
  601. : stream == null ? null : stream.BitRate;
  602. }
  603. }
  604. /// <summary>
  605. /// Predicts the audio channels that will be in the output stream
  606. /// </summary>
  607. public int? TargetAudioChannels
  608. {
  609. get
  610. {
  611. MediaStream stream = TargetAudioStream;
  612. int? streamChannels = stream == null ? null : stream.Channels;
  613. if (MaxAudioChannels.HasValue && !IsDirectStream)
  614. {
  615. if (streamChannels.HasValue)
  616. {
  617. return Math.Min(MaxAudioChannels.Value, streamChannels.Value);
  618. }
  619. return MaxAudioChannels.Value;
  620. }
  621. return streamChannels;
  622. }
  623. }
  624. /// <summary>
  625. /// Predicts the audio codec that will be in the output stream
  626. /// </summary>
  627. public string[] TargetAudioCodec
  628. {
  629. get
  630. {
  631. MediaStream stream = TargetAudioStream;
  632. string inputCodec = stream == null ? null : stream.Codec;
  633. if (IsDirectStream)
  634. {
  635. return string.IsNullOrWhiteSpace(inputCodec) ? new string[] { } : new[] { inputCodec };
  636. }
  637. foreach (string codec in AudioCodecs)
  638. {
  639. if (StringHelper.EqualsIgnoreCase(codec, inputCodec))
  640. {
  641. return string.IsNullOrWhiteSpace(codec) ? new string[] { } : new[] { codec };
  642. }
  643. }
  644. return AudioCodecs;
  645. }
  646. }
  647. public string[] TargetVideoCodec
  648. {
  649. get
  650. {
  651. MediaStream stream = TargetVideoStream;
  652. string inputCodec = stream == null ? null : stream.Codec;
  653. if (IsDirectStream)
  654. {
  655. return string.IsNullOrWhiteSpace(inputCodec) ? new string[] { } : new[] { inputCodec };
  656. }
  657. foreach (string codec in VideoCodecs)
  658. {
  659. if (StringHelper.EqualsIgnoreCase(codec, inputCodec))
  660. {
  661. return string.IsNullOrWhiteSpace(codec) ? new string[] { } : new[] { codec };
  662. }
  663. }
  664. return VideoCodecs;
  665. }
  666. }
  667. /// <summary>
  668. /// Predicts the audio channels that will be in the output stream
  669. /// </summary>
  670. public long? TargetSize
  671. {
  672. get
  673. {
  674. if (IsDirectStream)
  675. {
  676. return MediaSource.Size;
  677. }
  678. if (RunTimeTicks.HasValue)
  679. {
  680. int? totalBitrate = TargetTotalBitrate;
  681. double totalSeconds = RunTimeTicks.Value;
  682. // Convert to ms
  683. totalSeconds /= 10000;
  684. // Convert to seconds
  685. totalSeconds /= 1000;
  686. return totalBitrate.HasValue ?
  687. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  688. (long?)null;
  689. }
  690. return null;
  691. }
  692. }
  693. public int? TargetVideoBitrate
  694. {
  695. get
  696. {
  697. MediaStream stream = TargetVideoStream;
  698. return VideoBitrate.HasValue && !IsDirectStream
  699. ? VideoBitrate
  700. : stream == null ? null : stream.BitRate;
  701. }
  702. }
  703. public TransportStreamTimestamp TargetTimestamp
  704. {
  705. get
  706. {
  707. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  708. ? TransportStreamTimestamp.Valid
  709. : TransportStreamTimestamp.None;
  710. return !IsDirectStream
  711. ? defaultValue
  712. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  713. }
  714. }
  715. public int? TargetTotalBitrate
  716. {
  717. get
  718. {
  719. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  720. }
  721. }
  722. public bool? IsTargetAnamorphic
  723. {
  724. get
  725. {
  726. if (IsDirectStream)
  727. {
  728. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  729. }
  730. return false;
  731. }
  732. }
  733. public bool? IsTargetInterlaced
  734. {
  735. get
  736. {
  737. if (IsDirectStream)
  738. {
  739. return TargetVideoStream == null ? (bool?)null : TargetVideoStream.IsInterlaced;
  740. }
  741. var targetVideoCodecs = TargetVideoCodec;
  742. var videoCodec = targetVideoCodecs.Length == 0 ? null : targetVideoCodecs[0];
  743. if (!string.IsNullOrWhiteSpace(videoCodec))
  744. {
  745. if (string.Equals(GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  746. {
  747. return false;
  748. }
  749. }
  750. return TargetVideoStream == null ? (bool?)null : TargetVideoStream.IsInterlaced;
  751. }
  752. }
  753. public bool? IsTargetAVC
  754. {
  755. get
  756. {
  757. if (IsDirectStream)
  758. {
  759. return TargetVideoStream == null ? null : TargetVideoStream.IsAVC;
  760. }
  761. return true;
  762. }
  763. }
  764. public int? TargetWidth
  765. {
  766. get
  767. {
  768. MediaStream videoStream = TargetVideoStream;
  769. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  770. {
  771. ImageSize size = new ImageSize
  772. {
  773. Width = videoStream.Width.Value,
  774. Height = videoStream.Height.Value
  775. };
  776. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  777. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  778. ImageSize newSize = DrawingUtils.Resize(size,
  779. null,
  780. null,
  781. maxWidth,
  782. maxHeight);
  783. return Convert.ToInt32(newSize.Width);
  784. }
  785. return MaxWidth;
  786. }
  787. }
  788. public int? TargetHeight
  789. {
  790. get
  791. {
  792. MediaStream videoStream = TargetVideoStream;
  793. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  794. {
  795. ImageSize size = new ImageSize
  796. {
  797. Width = videoStream.Width.Value,
  798. Height = videoStream.Height.Value
  799. };
  800. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  801. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  802. ImageSize newSize = DrawingUtils.Resize(size,
  803. null,
  804. null,
  805. maxWidth,
  806. maxHeight);
  807. return Convert.ToInt32(newSize.Height);
  808. }
  809. return MaxHeight;
  810. }
  811. }
  812. public int? TargetVideoStreamCount
  813. {
  814. get
  815. {
  816. if (IsDirectStream)
  817. {
  818. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  819. }
  820. return GetMediaStreamCount(MediaStreamType.Video, 1);
  821. }
  822. }
  823. public int? TargetAudioStreamCount
  824. {
  825. get
  826. {
  827. if (IsDirectStream)
  828. {
  829. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  830. }
  831. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  832. }
  833. }
  834. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  835. {
  836. var count = MediaSource.GetStreamCount(type);
  837. if (count.HasValue)
  838. {
  839. count = Math.Min(count.Value, limit);
  840. }
  841. return count;
  842. }
  843. public List<MediaStream> GetSelectableAudioStreams()
  844. {
  845. return GetSelectableStreams(MediaStreamType.Audio);
  846. }
  847. public List<MediaStream> GetSelectableSubtitleStreams()
  848. {
  849. return GetSelectableStreams(MediaStreamType.Subtitle);
  850. }
  851. public List<MediaStream> GetSelectableStreams(MediaStreamType type)
  852. {
  853. List<MediaStream> list = new List<MediaStream>();
  854. foreach (MediaStream stream in MediaSource.MediaStreams)
  855. {
  856. if (type == stream.Type)
  857. {
  858. list.Add(stream);
  859. }
  860. }
  861. return list;
  862. }
  863. }
  864. }