VideoService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using System;
  4. using MediaBrowser.Controller.Library;
  5. using ServiceStack.ServiceHost;
  6. namespace MediaBrowser.Api.Playback.Progressive
  7. {
  8. /// <summary>
  9. /// Class GetAudioStream
  10. /// </summary>
  11. [Route("/Videos/{Id}/stream.ts", "GET")]
  12. [Route("/Videos/{Id}/stream.webm", "GET")]
  13. [Route("/Videos/{Id}/stream.asf", "GET")]
  14. [Route("/Videos/{Id}/stream.wmv", "GET")]
  15. [Route("/Videos/{Id}/stream.ogv", "GET")]
  16. [Route("/Videos/{Id}/stream.mp4", "GET")]
  17. [Route("/Videos/{Id}/stream.m4v", "GET")]
  18. [Route("/Videos/{Id}/stream.mkv", "GET")]
  19. [Route("/Videos/{Id}/stream.mpeg", "GET")]
  20. [Route("/Videos/{Id}/stream.avi", "GET")]
  21. [Route("/Videos/{Id}/stream", "GET")]
  22. public class GetVideoStream : StreamRequest
  23. {
  24. }
  25. /// <summary>
  26. /// Class VideoService
  27. /// </summary>
  28. public class VideoService : BaseProgressiveStreamingService
  29. {
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
  32. /// </summary>
  33. /// <param name="appPaths">The app paths.</param>
  34. public VideoService(IServerApplicationPaths appPaths, IUserManager userManager)
  35. : base(appPaths, userManager)
  36. {
  37. }
  38. /// <summary>
  39. /// Gets the specified request.
  40. /// </summary>
  41. /// <param name="request">The request.</param>
  42. /// <returns>System.Object.</returns>
  43. public object Get(GetVideoStream request)
  44. {
  45. return ProcessRequest(request);
  46. }
  47. /// <summary>
  48. /// Gets the command line arguments.
  49. /// </summary>
  50. /// <param name="outputPath">The output path.</param>
  51. /// <param name="state">The state.</param>
  52. /// <returns>System.String.</returns>
  53. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  54. {
  55. var video = (Video)state.Item;
  56. var probeSize = ServerKernel.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
  57. // Get the output codec name
  58. var videoCodec = GetVideoCodec(state.Request);
  59. var graphicalSubtitleParam = string.Empty;
  60. if (state.SubtitleStream != null)
  61. {
  62. // This is for internal graphical subs
  63. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  64. {
  65. graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(state, videoCodec);
  66. }
  67. }
  68. return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"",
  69. probeSize,
  70. GetFastSeekCommandLineParameter(state.Request),
  71. GetInputArgument(video, state.IsoMount),
  72. GetSlowSeekCommandLineParameter(state.Request),
  73. GetMapArgs(state),
  74. GetVideoArguments(state, videoCodec),
  75. graphicalSubtitleParam,
  76. GetAudioArguments(state),
  77. outputPath
  78. ).Trim();
  79. }
  80. /// <summary>
  81. /// Gets video arguments to pass to ffmpeg
  82. /// </summary>
  83. /// <param name="state">The state.</param>
  84. /// <param name="videoCodec">The video codec.</param>
  85. /// <returns>System.String.</returns>
  86. private string GetVideoArguments(StreamState state, string videoCodec)
  87. {
  88. var args = "-vcodec " + videoCodec;
  89. var request = state.Request;
  90. // If we're encoding video, add additional params
  91. if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  92. {
  93. // Add resolution params, if specified
  94. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  95. {
  96. args += GetOutputSizeParam(state, videoCodec);
  97. }
  98. if (request.Framerate.HasValue)
  99. {
  100. args += string.Format(" -r {0}", request.Framerate.Value);
  101. }
  102. // Add the audio bitrate
  103. var qualityParam = GetVideoQualityParam(request, videoCodec);
  104. if (!string.IsNullOrEmpty(qualityParam))
  105. {
  106. args += " " + qualityParam;
  107. }
  108. }
  109. else if (IsH264(state.VideoStream))
  110. {
  111. args += " -bsf h264_mp4toannexb";
  112. }
  113. return args;
  114. }
  115. /// <summary>
  116. /// Gets audio arguments to pass to ffmpeg
  117. /// </summary>
  118. /// <param name="state">The state.</param>
  119. /// <returns>System.String.</returns>
  120. private string GetAudioArguments(StreamState state)
  121. {
  122. // If the video doesn't have an audio stream, return a default.
  123. if (state.AudioStream == null)
  124. {
  125. return string.Empty;
  126. }
  127. var request = state.Request;
  128. // Get the output codec name
  129. var codec = GetAudioCodec(request);
  130. var args = "-acodec " + codec;
  131. // If we're encoding audio, add additional params
  132. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  133. {
  134. // Add the number of audio channels
  135. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  136. if (channels.HasValue)
  137. {
  138. args += " -ac " + channels.Value;
  139. }
  140. if (request.AudioSampleRate.HasValue)
  141. {
  142. args += " -ar " + request.AudioSampleRate.Value;
  143. }
  144. if (request.AudioBitRate.HasValue)
  145. {
  146. args += " -ab " + request.AudioBitRate.Value;
  147. }
  148. }
  149. return args;
  150. }
  151. /// <summary>
  152. /// Gets the video bitrate to specify on the command line
  153. /// </summary>
  154. /// <param name="request">The request.</param>
  155. /// <param name="videoCodec">The video codec.</param>
  156. /// <returns>System.String.</returns>
  157. private string GetVideoQualityParam(StreamRequest request, string videoCodec)
  158. {
  159. var args = string.Empty;
  160. // webm
  161. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  162. {
  163. args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0";
  164. }
  165. // asf/wmv
  166. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  167. {
  168. args = "-g 100 -qmax 15";
  169. }
  170. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  171. {
  172. args = "-preset superfast";
  173. }
  174. if (request.VideoBitRate.HasValue)
  175. {
  176. args += " -b:v " + request.VideoBitRate;
  177. }
  178. return args.Trim();
  179. }
  180. }
  181. }