MediaEncoderHelpers.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using MediaBrowser.Common.MediaInfo;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.IO;
  9. namespace MediaBrowser.Controller.MediaInfo
  10. {
  11. /// <summary>
  12. /// Class MediaEncoderHelpers
  13. /// </summary>
  14. public static class MediaEncoderHelpers
  15. {
  16. /// <summary>
  17. /// Gets the input argument.
  18. /// </summary>
  19. /// <param name="videoPath">The video path.</param>
  20. /// <param name="isRemote">if set to <c>true</c> [is remote].</param>
  21. /// <param name="videoType">Type of the video.</param>
  22. /// <param name="isoType">Type of the iso.</param>
  23. /// <param name="isoMount">The iso mount.</param>
  24. /// <param name="playableStreamFileNames">The playable stream file names.</param>
  25. /// <param name="type">The type.</param>
  26. /// <returns>System.String[][].</returns>
  27. public static string[] GetInputArgument(string videoPath, bool isRemote, VideoType videoType, IsoType? isoType, IIsoMount isoMount, IEnumerable<string> playableStreamFileNames, out InputType type)
  28. {
  29. var inputPath = isoMount == null ? new[] { videoPath } : new[] { isoMount.MountedPath };
  30. type = InputType.VideoFile;
  31. switch (videoType)
  32. {
  33. case VideoType.BluRay:
  34. type = InputType.Bluray;
  35. break;
  36. case VideoType.Dvd:
  37. type = InputType.Dvd;
  38. inputPath = GetPlayableStreamFiles(inputPath[0], playableStreamFileNames).ToArray();
  39. break;
  40. case VideoType.Iso:
  41. if (isoType.HasValue)
  42. {
  43. switch (isoType.Value)
  44. {
  45. case IsoType.BluRay:
  46. type = InputType.Bluray;
  47. break;
  48. case IsoType.Dvd:
  49. type = InputType.Dvd;
  50. inputPath = GetPlayableStreamFiles(inputPath[0], playableStreamFileNames).ToArray();
  51. break;
  52. }
  53. }
  54. break;
  55. case VideoType.VideoFile:
  56. {
  57. if (isRemote)
  58. {
  59. type = InputType.Url;
  60. }
  61. break;
  62. }
  63. }
  64. return inputPath;
  65. }
  66. public static List<string> GetPlayableStreamFiles(string rootPath, IEnumerable<string> filenames)
  67. {
  68. var allFiles = Directory
  69. .EnumerateFiles(rootPath, "*", SearchOption.AllDirectories)
  70. .ToList();
  71. return filenames.Select(name => allFiles.FirstOrDefault(f => string.Equals(Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  72. .Where(f => !string.IsNullOrEmpty(f))
  73. .ToList();
  74. }
  75. /// <summary>
  76. /// Gets the type of the input.
  77. /// </summary>
  78. /// <param name="videoType">Type of the video.</param>
  79. /// <param name="isoType">Type of the iso.</param>
  80. /// <returns>InputType.</returns>
  81. public static InputType GetInputType(VideoType? videoType, IsoType? isoType)
  82. {
  83. var type = InputType.AudioFile;
  84. if (videoType.HasValue)
  85. {
  86. switch (videoType.Value)
  87. {
  88. case VideoType.BluRay:
  89. type = InputType.Bluray;
  90. break;
  91. case VideoType.Dvd:
  92. type = InputType.Dvd;
  93. break;
  94. case VideoType.Iso:
  95. if (isoType.HasValue)
  96. {
  97. switch (isoType.Value)
  98. {
  99. case IsoType.BluRay:
  100. type = InputType.Bluray;
  101. break;
  102. case IsoType.Dvd:
  103. type = InputType.Dvd;
  104. break;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. return type;
  111. }
  112. public static IEnumerable<MediaStream> GetMediaStreams(MediaInfoResult data)
  113. {
  114. var internalStreams = data.streams ?? new MediaStreamInfo[] { };
  115. return internalStreams.Select(s => GetMediaStream(s, data.format))
  116. .Where(i => i != null);
  117. }
  118. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  119. /// <summary>
  120. /// Converts ffprobe stream info to our MediaStream class
  121. /// </summary>
  122. /// <param name="streamInfo">The stream info.</param>
  123. /// <param name="formatInfo">The format info.</param>
  124. /// <returns>MediaStream.</returns>
  125. private static MediaStream GetMediaStream(MediaStreamInfo streamInfo, MediaFormatInfo formatInfo)
  126. {
  127. var stream = new MediaStream
  128. {
  129. Codec = streamInfo.codec_name,
  130. Profile = streamInfo.profile,
  131. Level = streamInfo.level,
  132. Index = streamInfo.index
  133. };
  134. if (streamInfo.tags != null)
  135. {
  136. stream.Language = GetDictionaryValue(streamInfo.tags, "language");
  137. }
  138. if (string.Equals(streamInfo.codec_type, "audio", StringComparison.OrdinalIgnoreCase))
  139. {
  140. stream.Type = MediaStreamType.Audio;
  141. stream.Channels = streamInfo.channels;
  142. if (!string.IsNullOrEmpty(streamInfo.sample_rate))
  143. {
  144. stream.SampleRate = int.Parse(streamInfo.sample_rate, UsCulture);
  145. }
  146. stream.ChannelLayout = ParseChannelLayout(streamInfo.channel_layout);
  147. }
  148. else if (string.Equals(streamInfo.codec_type, "subtitle", StringComparison.OrdinalIgnoreCase))
  149. {
  150. stream.Type = MediaStreamType.Subtitle;
  151. }
  152. else if (string.Equals(streamInfo.codec_type, "video", StringComparison.OrdinalIgnoreCase))
  153. {
  154. stream.Type = MediaStreamType.Video;
  155. stream.Width = streamInfo.width;
  156. stream.Height = streamInfo.height;
  157. stream.AspectRatio = GetAspectRatio(streamInfo);
  158. stream.AverageFrameRate = GetFrameRate(streamInfo.avg_frame_rate);
  159. stream.RealFrameRate = GetFrameRate(streamInfo.r_frame_rate);
  160. }
  161. else
  162. {
  163. return null;
  164. }
  165. // Get stream bitrate
  166. if (stream.Type != MediaStreamType.Subtitle)
  167. {
  168. var bitrate = 0;
  169. if (!string.IsNullOrEmpty(streamInfo.bit_rate))
  170. {
  171. bitrate = int.Parse(streamInfo.bit_rate, UsCulture);
  172. }
  173. else if (formatInfo != null && !string.IsNullOrEmpty(formatInfo.bit_rate))
  174. {
  175. // If the stream info doesn't have a bitrate get the value from the media format info
  176. bitrate = int.Parse(formatInfo.bit_rate, UsCulture);
  177. }
  178. if (bitrate > 0)
  179. {
  180. stream.BitRate = bitrate;
  181. }
  182. }
  183. if (streamInfo.disposition != null)
  184. {
  185. var isDefault = GetDictionaryValue(streamInfo.disposition, "default");
  186. var isForced = GetDictionaryValue(streamInfo.disposition, "forced");
  187. stream.IsDefault = string.Equals(isDefault, "1", StringComparison.OrdinalIgnoreCase);
  188. stream.IsForced = string.Equals(isForced, "1", StringComparison.OrdinalIgnoreCase);
  189. }
  190. return stream;
  191. }
  192. /// <summary>
  193. /// Gets a string from an FFProbeResult tags dictionary
  194. /// </summary>
  195. /// <param name="tags">The tags.</param>
  196. /// <param name="key">The key.</param>
  197. /// <returns>System.String.</returns>
  198. private static string GetDictionaryValue(Dictionary<string, string> tags, string key)
  199. {
  200. if (tags == null)
  201. {
  202. return null;
  203. }
  204. string val;
  205. tags.TryGetValue(key, out val);
  206. return val;
  207. }
  208. private static string ParseChannelLayout(string input)
  209. {
  210. if (string.IsNullOrEmpty(input))
  211. {
  212. return input;
  213. }
  214. return input.Split('(').FirstOrDefault();
  215. }
  216. private static string GetAspectRatio(MediaStreamInfo info)
  217. {
  218. var original = info.display_aspect_ratio;
  219. int height;
  220. int width;
  221. var parts = (original ?? string.Empty).Split(':');
  222. if (!(parts.Length == 2 &&
  223. int.TryParse(parts[0], NumberStyles.Any, UsCulture, out width) &&
  224. int.TryParse(parts[1], NumberStyles.Any, UsCulture, out height) &&
  225. width > 0 &&
  226. height > 0))
  227. {
  228. width = info.width;
  229. height = info.height;
  230. }
  231. if (width > 0 && height > 0)
  232. {
  233. double ratio = width;
  234. ratio /= height;
  235. if (IsClose(ratio, 1.777777778, .03))
  236. {
  237. return "16:9";
  238. }
  239. if (IsClose(ratio, 1.3333333333, .05))
  240. {
  241. return "4:3";
  242. }
  243. if (IsClose(ratio, 1.41))
  244. {
  245. return "1.41:1";
  246. }
  247. if (IsClose(ratio, 1.5))
  248. {
  249. return "1.5:1";
  250. }
  251. if (IsClose(ratio, 1.6))
  252. {
  253. return "1.6:1";
  254. }
  255. if (IsClose(ratio, 1.66666666667))
  256. {
  257. return "5:3";
  258. }
  259. if (IsClose(ratio, 1.85, .02))
  260. {
  261. return "1.85:1";
  262. }
  263. if (IsClose(ratio, 2.35, .025))
  264. {
  265. return "2.35:1";
  266. }
  267. if (IsClose(ratio, 2.4, .025))
  268. {
  269. return "2.40:1";
  270. }
  271. }
  272. return original;
  273. }
  274. private static bool IsClose(double d1, double d2, double variance = .005)
  275. {
  276. return Math.Abs(d1 - d2) <= variance;
  277. }
  278. /// <summary>
  279. /// Gets a frame rate from a string value in ffprobe output
  280. /// This could be a number or in the format of 2997/125.
  281. /// </summary>
  282. /// <param name="value">The value.</param>
  283. /// <returns>System.Nullable{System.Single}.</returns>
  284. private static float? GetFrameRate(string value)
  285. {
  286. if (!string.IsNullOrEmpty(value))
  287. {
  288. var parts = value.Split('/');
  289. float result;
  290. if (parts.Length == 2)
  291. {
  292. result = float.Parse(parts[0], UsCulture) / float.Parse(parts[1], UsCulture);
  293. }
  294. else
  295. {
  296. result = float.Parse(parts[0], UsCulture);
  297. }
  298. return float.IsNaN(result) ? (float?)null : result;
  299. }
  300. return null;
  301. }
  302. }
  303. }