VideoHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Model.Entities;
  6. using System.Drawing;
  7. using MediaBrowser.Common.Drawing;
  8. namespace MediaBrowser.Api.HttpHandlers
  9. {
  10. /// <summary>
  11. /// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
  12. /// </summary>
  13. class VideoHandler : BaseMediaHandler<Video>
  14. {
  15. /// <summary>
  16. /// We can output these files directly, but we can't encode them
  17. /// </summary>
  18. protected override IEnumerable<string> UnsupportedOutputEncodingFormats
  19. {
  20. get
  21. {
  22. // mp4, 3gp, mov - muxer does not support non-seekable output
  23. // avi, mov, mkv, m4v - can't stream these when encoding. the player will try to download them completely before starting playback.
  24. // wmv - can't seem to figure out the output format name
  25. return new string[] { "mp4", "3gp", "m4v", "mkv", "avi", "mov", "wmv" };
  26. }
  27. }
  28. protected override bool RequiresConversion()
  29. {
  30. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  31. // For now we won't allow these to pass through.
  32. // Later we'll add some intelligence to allow it when possible
  33. if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
  34. {
  35. return true;
  36. }
  37. if (base.RequiresConversion())
  38. {
  39. return true;
  40. }
  41. if (RequiresVideoConversion())
  42. {
  43. return true;
  44. }
  45. AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
  46. if (audio != null)
  47. {
  48. if (RequiresAudioConversion(audio))
  49. {
  50. return true;
  51. }
  52. }
  53. // Yay
  54. return false;
  55. }
  56. /// <summary>
  57. /// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
  58. /// </summary>
  59. private string GetFFMpegOutputFormat(string outputFormat)
  60. {
  61. if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
  62. {
  63. return "matroska";
  64. }
  65. else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
  66. {
  67. return "mpegts";
  68. }
  69. else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
  70. {
  71. return "ogg";
  72. }
  73. return outputFormat;
  74. }
  75. /// <summary>
  76. /// Creates arguments to pass to ffmpeg
  77. /// </summary>
  78. protected override string GetCommandLineArguments()
  79. {
  80. List<string> audioTranscodeParams = new List<string>();
  81. string outputFormat = GetConversionOutputFormat();
  82. return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
  83. LibraryItem.Path,
  84. GetVideoArguments(outputFormat),
  85. GetAudioArguments(outputFormat),
  86. GetFFMpegOutputFormat(outputFormat)
  87. );
  88. }
  89. private string GetVideoArguments(string outputFormat)
  90. {
  91. string codec = GetVideoCodec(outputFormat);
  92. string args = "-vcodec " + codec;
  93. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  94. {
  95. if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
  96. {
  97. Size size = DrawingUtils.Resize(LibraryItem.Width, LibraryItem.Height, Width, Height, MaxWidth, MaxHeight);
  98. args += string.Format(" -s {0}x{1}", size.Width, size.Height);
  99. }
  100. }
  101. return args;
  102. }
  103. private string GetAudioArguments(string outputFormat)
  104. {
  105. AudioStream audioStream = LibraryItem.AudioStreams.FirstOrDefault();
  106. if (audioStream == null)
  107. {
  108. return string.Empty;
  109. }
  110. string codec = GetAudioCodec(audioStream, outputFormat);
  111. string args = "-acodec " + codec;
  112. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  113. {
  114. int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
  115. if (channels.HasValue)
  116. {
  117. args += " -ac " + channels.Value;
  118. }
  119. int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
  120. if (sampleRate.HasValue)
  121. {
  122. args += " -ar " + sampleRate.Value;
  123. }
  124. }
  125. return args;
  126. }
  127. private string GetVideoCodec(string outputFormat)
  128. {
  129. if (outputFormat.Equals("webm"))
  130. {
  131. // Per webm specification, it must be vpx
  132. return "libvpx";
  133. }
  134. else if (outputFormat.Equals("asf"))
  135. {
  136. return "wmv2";
  137. }
  138. else if (outputFormat.Equals("wmv"))
  139. {
  140. return "wmv2";
  141. }
  142. else if (outputFormat.Equals("ogv"))
  143. {
  144. return "libtheora";
  145. }
  146. if (!RequiresVideoConversion())
  147. {
  148. return "copy";
  149. }
  150. return "libx264";
  151. }
  152. private string GetAudioCodec(AudioStream audioStream, string outputFormat)
  153. {
  154. if (outputFormat.Equals("webm"))
  155. {
  156. // Per webm specification, it must be vorbis
  157. return "libvorbis";
  158. }
  159. else if (outputFormat.Equals("asf"))
  160. {
  161. return "wmav2";
  162. }
  163. else if (outputFormat.Equals("wmv"))
  164. {
  165. return "wmav2";
  166. }
  167. else if (outputFormat.Equals("ogv"))
  168. {
  169. return "libvorbis";
  170. }
  171. // See if we can just copy the stream
  172. if (!RequiresAudioConversion(audioStream))
  173. {
  174. return "copy";
  175. }
  176. return "libvo_aacenc";
  177. }
  178. private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
  179. {
  180. if (libraryItemChannels > 2)
  181. {
  182. if (audioCodec.Equals("libvo_aacenc"))
  183. {
  184. // libvo_aacenc currently only supports two channel output
  185. return 2;
  186. }
  187. else if (audioCodec.Equals("wmav2"))
  188. {
  189. // wmav2 currently only supports two channel output
  190. return 2;
  191. }
  192. }
  193. return GetNumAudioChannelsParam(libraryItemChannels);
  194. }
  195. private bool RequiresVideoConversion()
  196. {
  197. // Check dimensions
  198. if (Width.HasValue)
  199. {
  200. if (Width.Value != LibraryItem.Width)
  201. {
  202. return true;
  203. }
  204. }
  205. if (Height.HasValue)
  206. {
  207. if (Height.Value != LibraryItem.Height)
  208. {
  209. return true;
  210. }
  211. }
  212. if (MaxWidth.HasValue)
  213. {
  214. if (MaxWidth.Value < LibraryItem.Width)
  215. {
  216. return true;
  217. }
  218. }
  219. if (MaxHeight.HasValue)
  220. {
  221. if (MaxHeight.Value < LibraryItem.Height)
  222. {
  223. return true;
  224. }
  225. }
  226. if (LibraryItem.VideoCodec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || LibraryItem.VideoCodec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1)
  227. {
  228. return false;
  229. }
  230. return false;
  231. }
  232. private bool RequiresAudioConversion(AudioStream audio)
  233. {
  234. if (AudioChannels.HasValue)
  235. {
  236. if (audio.Channels > AudioChannels.Value)
  237. {
  238. return true;
  239. }
  240. }
  241. if (audio.AudioFormat.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
  242. {
  243. return false;
  244. }
  245. if (audio.AudioFormat.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
  246. {
  247. return false;
  248. }
  249. if (audio.AudioFormat.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
  250. {
  251. return false;
  252. }
  253. return true;
  254. }
  255. private int? Height
  256. {
  257. get
  258. {
  259. string val = QueryString["height"];
  260. if (string.IsNullOrEmpty(val))
  261. {
  262. return null;
  263. }
  264. return int.Parse(val);
  265. }
  266. }
  267. private int? Width
  268. {
  269. get
  270. {
  271. string val = QueryString["width"];
  272. if (string.IsNullOrEmpty(val))
  273. {
  274. return null;
  275. }
  276. return int.Parse(val);
  277. }
  278. }
  279. private int? MaxHeight
  280. {
  281. get
  282. {
  283. string val = QueryString["maxheight"];
  284. if (string.IsNullOrEmpty(val))
  285. {
  286. return null;
  287. }
  288. return int.Parse(val);
  289. }
  290. }
  291. private int? MaxWidth
  292. {
  293. get
  294. {
  295. string val = QueryString["maxwidth"];
  296. if (string.IsNullOrEmpty(val))
  297. {
  298. return null;
  299. }
  300. return int.Parse(val);
  301. }
  302. }
  303. }
  304. }