VideoService.cs 7.1 KB

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