VideoService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. var request = state.VideoRequest;
  118. // If we're encoding video, add additional params
  119. if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  120. {
  121. // Add resolution params, if specified
  122. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  123. {
  124. args += GetOutputSizeParam(state, videoCodec);
  125. }
  126. if (request.Framerate.HasValue)
  127. {
  128. args += string.Format(" -r {0}", request.Framerate.Value);
  129. }
  130. // Add the audio bitrate
  131. var qualityParam = GetVideoQualityParam(request, videoCodec);
  132. if (!string.IsNullOrEmpty(qualityParam))
  133. {
  134. args += " " + qualityParam;
  135. }
  136. args += " -vsync vfr";
  137. }
  138. else if (IsH264(state.VideoStream))
  139. {
  140. // FFmpeg will fail to convert and give h264 bitstream malformated error if it isn't used when converting mp4 to transport stream.
  141. args += " -bsf h264_mp4toannexb";
  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. var args = "-acodec " + codec;
  161. // If we're encoding audio, add additional params
  162. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  163. {
  164. // Add the number of audio channels
  165. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  166. if (channels.HasValue)
  167. {
  168. args += " -ac " + channels.Value;
  169. // Boost volume to 200% when downsampling from 6ch to 2ch
  170. if (channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  171. {
  172. args += " -vol 512";
  173. }
  174. }
  175. if (request.AudioSampleRate.HasValue)
  176. {
  177. args += " -ar " + request.AudioSampleRate.Value;
  178. }
  179. if (request.AudioBitRate.HasValue)
  180. {
  181. args += " -ab " + request.AudioBitRate.Value;
  182. }
  183. }
  184. return args;
  185. }
  186. /// <summary>
  187. /// Gets the video bitrate to specify on the command line
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <param name="videoCodec">The video codec.</param>
  191. /// <returns>System.String.</returns>
  192. private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
  193. {
  194. var args = string.Empty;
  195. // webm
  196. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  197. {
  198. args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0";
  199. }
  200. // asf/wmv
  201. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  202. {
  203. args = "-g 100 -qmax 15";
  204. }
  205. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  206. {
  207. args = "-preset superfast";
  208. }
  209. if (request.VideoBitRate.HasValue)
  210. {
  211. args += " -b:v " + request.VideoBitRate;
  212. }
  213. return args.Trim();
  214. }
  215. }
  216. }