VideoService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.IO;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.MediaInfo;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities;
  6. using System;
  7. using MediaBrowser.Controller.Library;
  8. using ServiceStack.ServiceHost;
  9. namespace MediaBrowser.Api.Playback.Progressive
  10. {
  11. /// <summary>
  12. /// Class GetAudioStream
  13. /// </summary>
  14. [Route("/Videos/{Id}/stream.ts", "GET")]
  15. [Route("/Videos/{Id}/stream.webm", "GET")]
  16. [Route("/Videos/{Id}/stream.asf", "GET")]
  17. [Route("/Videos/{Id}/stream.wmv", "GET")]
  18. [Route("/Videos/{Id}/stream.ogv", "GET")]
  19. [Route("/Videos/{Id}/stream.mp4", "GET")]
  20. [Route("/Videos/{Id}/stream.m4v", "GET")]
  21. [Route("/Videos/{Id}/stream.mkv", "GET")]
  22. [Route("/Videos/{Id}/stream.mpeg", "GET")]
  23. [Route("/Videos/{Id}/stream.avi", "GET")]
  24. [Route("/Videos/{Id}/stream.m2ts", "GET")]
  25. [Route("/Videos/{Id}/stream.3gp", "GET")]
  26. [Route("/Videos/{Id}/stream.wmv", "GET")]
  27. [Route("/Videos/{Id}/stream", "GET")]
  28. [Route("/Videos/{Id}/stream.ts", "HEAD")]
  29. [Route("/Videos/{Id}/stream.webm", "HEAD")]
  30. [Route("/Videos/{Id}/stream.asf", "HEAD")]
  31. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  32. [Route("/Videos/{Id}/stream.ogv", "HEAD")]
  33. [Route("/Videos/{Id}/stream.mp4", "HEAD")]
  34. [Route("/Videos/{Id}/stream.m4v", "HEAD")]
  35. [Route("/Videos/{Id}/stream.mkv", "HEAD")]
  36. [Route("/Videos/{Id}/stream.mpeg", "HEAD")]
  37. [Route("/Videos/{Id}/stream.avi", "HEAD")]
  38. [Route("/Videos/{Id}/stream.3gp", "HEAD")]
  39. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  40. [Route("/Videos/{Id}/stream.m2ts", "HEAD")]
  41. [Route("/Videos/{Id}/stream", "HEAD")]
  42. [Api(Description = "Gets a video stream")]
  43. public class GetVideoStream : VideoStreamRequest
  44. {
  45. }
  46. /// <summary>
  47. /// Class VideoService
  48. /// </summary>
  49. public class VideoService : BaseProgressiveStreamingService
  50. {
  51. public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
  52. : base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
  53. {
  54. }
  55. /// <summary>
  56. /// Gets the specified request.
  57. /// </summary>
  58. /// <param name="request">The request.</param>
  59. /// <returns>System.Object.</returns>
  60. public object Get(GetVideoStream request)
  61. {
  62. return ProcessRequest(request, false);
  63. }
  64. public object Head(GetVideoStream request)
  65. {
  66. return ProcessRequest(request, true);
  67. }
  68. /// <summary>
  69. /// Gets the command line arguments.
  70. /// </summary>
  71. /// <param name="outputPath">The output path.</param>
  72. /// <param name="state">The state.</param>
  73. /// <returns>System.String.</returns>
  74. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  75. {
  76. var video = (Video)state.Item;
  77. var probeSize = GetProbeSizeArgument(state.Item);
  78. // Get the output codec name
  79. var videoCodec = GetVideoCodec(state.VideoRequest);
  80. var format = string.Empty;
  81. var keyFrame = string.Empty;
  82. if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
  83. {
  84. format = " -f mp4 -movflags frag_keyframe+empty_moov";
  85. }
  86. var threads = videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase) ? 2 : 0;
  87. return string.Format("{0} {1} -i {2}{3}{4} {5} {6} -threads {7} {8}{9} \"{10}\"",
  88. probeSize,
  89. GetFastSeekCommandLineParameter(state.Request),
  90. GetInputArgument(video, state.IsoMount),
  91. GetSlowSeekCommandLineParameter(state.Request),
  92. keyFrame,
  93. GetMapArgs(state),
  94. GetVideoArguments(state, videoCodec),
  95. threads,
  96. GetAudioArguments(state),
  97. format,
  98. outputPath
  99. ).Trim();
  100. }
  101. /// <summary>
  102. /// Gets video arguments to pass to ffmpeg
  103. /// </summary>
  104. /// <param name="state">The state.</param>
  105. /// <param name="codec">The video codec.</param>
  106. /// <returns>System.String.</returns>
  107. private string GetVideoArguments(StreamState state, string codec)
  108. {
  109. var args = "-vcodec " + codec;
  110. // See if we can save come cpu cycles by avoiding encoding
  111. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  112. {
  113. return IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
  114. }
  115. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
  116. args += keyFrameArg;
  117. var request = state.VideoRequest;
  118. // Add resolution params, if specified
  119. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  120. {
  121. args += GetOutputSizeParam(state, codec);
  122. }
  123. if (request.Framerate.HasValue)
  124. {
  125. args += string.Format(" -r {0}", request.Framerate.Value);
  126. }
  127. // Add the audio bitrate
  128. var qualityParam = GetVideoQualityParam(request, codec);
  129. if (!string.IsNullOrEmpty(qualityParam))
  130. {
  131. args += " " + qualityParam;
  132. }
  133. args += " -vsync vfr";
  134. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  135. {
  136. args += " -profile:v " + state.VideoRequest.Profile;
  137. }
  138. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  139. {
  140. args += " -level " + state.VideoRequest.Level;
  141. }
  142. if (state.SubtitleStream != null)
  143. {
  144. // This is for internal graphical subs
  145. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  146. {
  147. args += GetInternalGraphicalSubtitleParam(state, codec);
  148. }
  149. }
  150. return args;
  151. }
  152. /// <summary>
  153. /// Gets audio arguments to pass to ffmpeg
  154. /// </summary>
  155. /// <param name="state">The state.</param>
  156. /// <returns>System.String.</returns>
  157. private string GetAudioArguments(StreamState state)
  158. {
  159. // If the video doesn't have an audio stream, return a default.
  160. if (state.AudioStream == null)
  161. {
  162. return string.Empty;
  163. }
  164. var request = state.Request;
  165. // Get the output codec name
  166. var codec = GetAudioCodec(request);
  167. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  168. {
  169. return "-acodec copy";
  170. }
  171. var args = "-acodec " + codec;
  172. // Add the number of audio channels
  173. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  174. if (channels.HasValue)
  175. {
  176. args += " -ac " + channels.Value;
  177. }
  178. if (request.AudioSampleRate.HasValue)
  179. {
  180. args += " -ar " + request.AudioSampleRate.Value;
  181. }
  182. if (request.AudioBitRate.HasValue)
  183. {
  184. args += " -ab " + request.AudioBitRate.Value;
  185. }
  186. var volParam = string.Empty;
  187. // Boost volume to 200% when downsampling from 6ch to 2ch
  188. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  189. {
  190. volParam = ",volume=2.000000";
  191. }
  192. args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
  193. return args;
  194. }
  195. /// <summary>
  196. /// Gets the video bitrate to specify on the command line
  197. /// </summary>
  198. /// <param name="request">The request.</param>
  199. /// <param name="videoCodec">The video codec.</param>
  200. /// <returns>System.String.</returns>
  201. private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
  202. {
  203. var args = string.Empty;
  204. // webm
  205. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  206. {
  207. args = "-quality realtime -profile:v 0 -slices 4";
  208. }
  209. // asf/wmv
  210. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  211. {
  212. args = "-g 100 -qmax 15";
  213. }
  214. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  215. {
  216. args = "-preset superfast";
  217. }
  218. else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
  219. {
  220. args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  221. }
  222. if (request.VideoBitRate.HasValue)
  223. {
  224. args += " -b:v " + request.VideoBitRate;
  225. }
  226. return args.Trim();
  227. }
  228. }
  229. }