MediaEncoderHelpers.cs 12 KB

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