VideoService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 format = string.Empty;
  76. var keyFrame = string.Empty;
  77. if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
  78. {
  79. format = " -f mp4 -movflags frag_keyframe+empty_moov";
  80. }
  81. return string.Format("{0} {1} -i {2}{3}{4} -threads 0 {5} {6} {7}{8} \"{9}\"",
  82. probeSize,
  83. GetFastSeekCommandLineParameter(state.Request),
  84. GetInputArgument(video, state.IsoMount),
  85. GetSlowSeekCommandLineParameter(state.Request),
  86. keyFrame,
  87. GetMapArgs(state),
  88. GetVideoArguments(state, videoCodec),
  89. GetAudioArguments(state),
  90. format,
  91. outputPath
  92. ).Trim();
  93. }
  94. /// <summary>
  95. /// Gets video arguments to pass to ffmpeg
  96. /// </summary>
  97. /// <param name="state">The state.</param>
  98. /// <param name="codec">The video codec.</param>
  99. /// <returns>System.String.</returns>
  100. private string GetVideoArguments(StreamState state, string codec)
  101. {
  102. var args = "-vcodec " + codec;
  103. // See if we can save come cpu cycles by avoiding encoding
  104. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  105. {
  106. return IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
  107. }
  108. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
  109. args += keyFrameArg;
  110. var request = state.VideoRequest;
  111. // Add resolution params, if specified
  112. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  113. {
  114. args += GetOutputSizeParam(state, codec);
  115. }
  116. if (request.Framerate.HasValue)
  117. {
  118. args += string.Format(" -r {0}", request.Framerate.Value);
  119. }
  120. // Add the audio bitrate
  121. var qualityParam = GetVideoQualityParam(request, codec);
  122. if (!string.IsNullOrEmpty(qualityParam))
  123. {
  124. args += " " + qualityParam;
  125. }
  126. args += " -vsync vfr";
  127. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  128. {
  129. args += " -profile:v" + state.VideoRequest.Profile;
  130. }
  131. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  132. {
  133. args += " -level 3" + state.VideoRequest.Level;
  134. }
  135. if (state.SubtitleStream != null)
  136. {
  137. // This is for internal graphical subs
  138. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  139. {
  140. args += GetInternalGraphicalSubtitleParam(state, codec);
  141. }
  142. }
  143. return args;
  144. }
  145. /// <summary>
  146. /// Gets audio arguments to pass to ffmpeg
  147. /// </summary>
  148. /// <param name="state">The state.</param>
  149. /// <returns>System.String.</returns>
  150. private string GetAudioArguments(StreamState state)
  151. {
  152. // If the video doesn't have an audio stream, return a default.
  153. if (state.AudioStream == null)
  154. {
  155. return string.Empty;
  156. }
  157. var request = state.Request;
  158. // Get the output codec name
  159. var codec = GetAudioCodec(request);
  160. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  161. {
  162. return "-acodec copy";
  163. }
  164. var args = "-acodec " + codec;
  165. // Add the number of audio channels
  166. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  167. if (channels.HasValue)
  168. {
  169. args += " -ac " + channels.Value;
  170. }
  171. if (request.AudioSampleRate.HasValue)
  172. {
  173. args += " -ar " + request.AudioSampleRate.Value;
  174. }
  175. if (request.AudioBitRate.HasValue)
  176. {
  177. args += " -ab " + request.AudioBitRate.Value;
  178. }
  179. var volParam = string.Empty;
  180. // Boost volume to 200% when downsampling from 6ch to 2ch
  181. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  182. {
  183. volParam = ",volume=2.000000";
  184. }
  185. args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
  186. return args;
  187. }
  188. /// <summary>
  189. /// Gets the video bitrate to specify on the command line
  190. /// </summary>
  191. /// <param name="request">The request.</param>
  192. /// <param name="videoCodec">The video codec.</param>
  193. /// <returns>System.String.</returns>
  194. private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
  195. {
  196. var args = string.Empty;
  197. // webm
  198. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  199. {
  200. args = "-quality realtime -profile:v 1 -slices 4";
  201. }
  202. // asf/wmv
  203. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  204. {
  205. args = "-g 100 -qmax 15";
  206. }
  207. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  208. {
  209. args = "-preset superfast";
  210. }
  211. if (request.VideoBitRate.HasValue)
  212. {
  213. args += " -b:v " + request.VideoBitRate;
  214. }
  215. return args.Trim();
  216. }
  217. }
  218. }