VideoHandler.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Model.Entities;
  6. namespace MediaBrowser.Api.HttpHandlers
  7. {
  8. /// <summary>
  9. /// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
  10. /// </summary>
  11. class VideoHandler : BaseMediaHandler<Video>
  12. {
  13. /// <summary>
  14. /// We can output these files directly, but we can't encode them
  15. /// </summary>
  16. protected override IEnumerable<string> UnsupportedOutputEncodingFormats
  17. {
  18. get
  19. {
  20. // mp4, 3gp, mov - muxer does not support non-seekable output
  21. // avi, mov, mkv, m4v - can't stream these when encoding. the player will try to download them completely before starting playback.
  22. // wmv - can't seem to figure out the output format name
  23. return new string[] { "mp4", "3gp", "m4v", "mkv", "avi", "mov", "wmv" };
  24. }
  25. }
  26. protected override bool RequiresConversion()
  27. {
  28. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  29. // For now we won't allow these to pass through.
  30. // Later we'll add some intelligence to allow it when possible
  31. if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
  32. {
  33. return true;
  34. }
  35. if (base.RequiresConversion())
  36. {
  37. return true;
  38. }
  39. AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
  40. if (audio != null)
  41. {
  42. // If the number of channels is greater than our desired channels, we need to transcode
  43. if (AudioChannels.HasValue && AudioChannels.Value < audio.Channels)
  44. {
  45. return true;
  46. }
  47. }
  48. // Yay
  49. return false;
  50. }
  51. /// <summary>
  52. /// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
  53. /// </summary>
  54. private string GetFFMpegOutputFormat(string outputFormat)
  55. {
  56. if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
  57. {
  58. return "matroska";
  59. }
  60. else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
  61. {
  62. return "mpegts";
  63. }
  64. else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
  65. {
  66. return "ogg";
  67. }
  68. return outputFormat;
  69. }
  70. /// <summary>
  71. /// Creates arguments to pass to ffmpeg
  72. /// </summary>
  73. protected override string GetCommandLineArguments()
  74. {
  75. List<string> audioTranscodeParams = new List<string>();
  76. string outputFormat = GetConversionOutputFormat();
  77. return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
  78. LibraryItem.Path,
  79. GetVideoArguments(outputFormat),
  80. GetAudioArguments(outputFormat),
  81. GetFFMpegOutputFormat(outputFormat)
  82. );
  83. }
  84. private string GetVideoArguments(string outputFormat)
  85. {
  86. string codec = GetVideoCodec(outputFormat);
  87. string args = "-vcodec " + codec;
  88. return args;
  89. }
  90. private string GetAudioArguments(string outputFormat)
  91. {
  92. AudioStream audioStream = LibraryItem.AudioStreams.FirstOrDefault();
  93. if (audioStream == null)
  94. {
  95. return string.Empty;
  96. }
  97. string codec = GetAudioCodec(audioStream, outputFormat);
  98. string args = "-acodec " + codec;
  99. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  100. {
  101. int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
  102. if (channels.HasValue)
  103. {
  104. args += " -ac " + channels.Value;
  105. }
  106. int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
  107. if (sampleRate.HasValue)
  108. {
  109. args += " -ar " + sampleRate.Value;
  110. }
  111. }
  112. return args;
  113. }
  114. private string GetVideoCodec(string outputFormat)
  115. {
  116. if (outputFormat.Equals("webm"))
  117. {
  118. // Per webm specification, it must be vpx
  119. return "libvpx";
  120. }
  121. else if (outputFormat.Equals("asf"))
  122. {
  123. return "wmv2";
  124. }
  125. else if (outputFormat.Equals("wmv"))
  126. {
  127. return "wmv2";
  128. }
  129. else if (outputFormat.Equals("ogv"))
  130. {
  131. return "libtheora";
  132. }
  133. return "libx264";
  134. }
  135. private string GetAudioCodec(AudioStream audioStream, string outputFormat)
  136. {
  137. if (outputFormat.Equals("webm"))
  138. {
  139. // Per webm specification, it must be vorbis
  140. return "libvorbis";
  141. }
  142. else if (outputFormat.Equals("asf"))
  143. {
  144. return "wmav2";
  145. }
  146. else if (outputFormat.Equals("wmv"))
  147. {
  148. return "wmav2";
  149. }
  150. else if (outputFormat.Equals("ogv"))
  151. {
  152. return "libvorbis";
  153. }
  154. // See if we can just copy the stream
  155. if (HasBasicAudio(audioStream))
  156. {
  157. return "copy";
  158. }
  159. return "libvo_aacenc";
  160. }
  161. private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
  162. {
  163. if (libraryItemChannels > 2)
  164. {
  165. if (audioCodec.Equals("libvo_aacenc"))
  166. {
  167. // libvo_aacenc currently only supports two channel output
  168. return 2;
  169. }
  170. else if (audioCodec.Equals("wmav2"))
  171. {
  172. // wmav2 currently only supports two channel output
  173. return 2;
  174. }
  175. }
  176. return GetNumAudioChannelsParam(libraryItemChannels);
  177. }
  178. private bool HasBasicAudio(AudioStream audio)
  179. {
  180. if (AudioChannels.HasValue)
  181. {
  182. if (audio.Channels > AudioChannels.Value)
  183. {
  184. return false;
  185. }
  186. }
  187. if (audio.AudioFormat.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
  188. {
  189. return true;
  190. }
  191. if (audio.AudioFormat.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
  192. {
  193. return true;
  194. }
  195. if (audio.AudioFormat.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
  196. {
  197. return true;
  198. }
  199. return false;
  200. }
  201. }
  202. }