VideoService.cs 9.3 KB

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