VideoService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Devices;
  5. using MediaBrowser.Controller.Dlna;
  6. using MediaBrowser.Controller.Drawing;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Model.IO;
  10. using ServiceStack;
  11. using System;
  12. using System.IO;
  13. namespace MediaBrowser.Api.Playback.Progressive
  14. {
  15. /// <summary>
  16. /// Class GetAudioStream
  17. /// </summary>
  18. [Route("/Videos/{Id}/stream.ts", "GET")]
  19. [Route("/Videos/{Id}/stream.webm", "GET")]
  20. [Route("/Videos/{Id}/stream.asf", "GET")]
  21. [Route("/Videos/{Id}/stream.wmv", "GET")]
  22. [Route("/Videos/{Id}/stream.ogv", "GET")]
  23. [Route("/Videos/{Id}/stream.mp4", "GET")]
  24. [Route("/Videos/{Id}/stream.m4v", "GET")]
  25. [Route("/Videos/{Id}/stream.mkv", "GET")]
  26. [Route("/Videos/{Id}/stream.mpeg", "GET")]
  27. [Route("/Videos/{Id}/stream.mpg", "GET")]
  28. [Route("/Videos/{Id}/stream.avi", "GET")]
  29. [Route("/Videos/{Id}/stream.m2ts", "GET")]
  30. [Route("/Videos/{Id}/stream.3gp", "GET")]
  31. [Route("/Videos/{Id}/stream.wmv", "GET")]
  32. [Route("/Videos/{Id}/stream.wtv", "GET")]
  33. [Route("/Videos/{Id}/stream.mov", "GET")]
  34. [Route("/Videos/{Id}/stream", "GET")]
  35. [Route("/Videos/{Id}/stream.ts", "HEAD")]
  36. [Route("/Videos/{Id}/stream.webm", "HEAD")]
  37. [Route("/Videos/{Id}/stream.asf", "HEAD")]
  38. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  39. [Route("/Videos/{Id}/stream.ogv", "HEAD")]
  40. [Route("/Videos/{Id}/stream.mp4", "HEAD")]
  41. [Route("/Videos/{Id}/stream.m4v", "HEAD")]
  42. [Route("/Videos/{Id}/stream.mkv", "HEAD")]
  43. [Route("/Videos/{Id}/stream.mpeg", "HEAD")]
  44. [Route("/Videos/{Id}/stream.mpg", "HEAD")]
  45. [Route("/Videos/{Id}/stream.avi", "HEAD")]
  46. [Route("/Videos/{Id}/stream.3gp", "HEAD")]
  47. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  48. [Route("/Videos/{Id}/stream.wtv", "HEAD")]
  49. [Route("/Videos/{Id}/stream.m2ts", "HEAD")]
  50. [Route("/Videos/{Id}/stream.mov", "HEAD")]
  51. [Route("/Videos/{Id}/stream", "HEAD")]
  52. [Api(Description = "Gets a video stream")]
  53. public class GetVideoStream : VideoStreamRequest
  54. {
  55. }
  56. /// <summary>
  57. /// Class VideoService
  58. /// </summary>
  59. public class VideoService : BaseProgressiveStreamingService
  60. {
  61. public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IImageProcessor imageProcessor, IHttpClient httpClient) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, imageProcessor, httpClient)
  62. {
  63. }
  64. /// <summary>
  65. /// Gets the specified request.
  66. /// </summary>
  67. /// <param name="request">The request.</param>
  68. /// <returns>System.Object.</returns>
  69. public object Get(GetVideoStream request)
  70. {
  71. return ProcessRequest(request, false);
  72. }
  73. /// <summary>
  74. /// Heads the specified request.
  75. /// </summary>
  76. /// <param name="request">The request.</param>
  77. /// <returns>System.Object.</returns>
  78. public object Head(GetVideoStream request)
  79. {
  80. return ProcessRequest(request, true);
  81. }
  82. protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
  83. {
  84. // Get the output codec name
  85. var videoCodec = state.OutputVideoCodec;
  86. var format = string.Empty;
  87. var keyFrame = string.Empty;
  88. if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
  89. {
  90. // Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js
  91. format = " -f mp4 -movflags frag_keyframe+empty_moov";
  92. }
  93. var threads = GetNumberOfThreads(state, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
  94. var inputModifier = GetInputModifier(state);
  95. return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -threads {5} {6}{7} -y \"{8}\"",
  96. inputModifier,
  97. GetInputArgument(state),
  98. keyFrame,
  99. GetMapArgs(state),
  100. GetVideoArguments(state, videoCodec),
  101. threads,
  102. GetAudioArguments(state),
  103. format,
  104. outputPath
  105. ).Trim();
  106. }
  107. /// <summary>
  108. /// Gets video arguments to pass to ffmpeg
  109. /// </summary>
  110. /// <param name="state">The state.</param>
  111. /// <param name="codec">The video codec.</param>
  112. /// <returns>System.String.</returns>
  113. private string GetVideoArguments(StreamState state, string codec)
  114. {
  115. var args = "-codec:v:0 " + codec;
  116. if (state.EnableMpegtsM2TsMode)
  117. {
  118. args += " -mpegts_m2ts_mode 1";
  119. }
  120. // See if we can save come cpu cycles by avoiding encoding
  121. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  122. {
  123. return state.VideoStream != null && IsH264(state.VideoStream) && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) ?
  124. args + " -bsf:v h264_mp4toannexb" :
  125. args;
  126. }
  127. var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
  128. 5.ToString(UsCulture));
  129. args += keyFrameArg;
  130. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
  131. // Add resolution params, if specified
  132. if (!hasGraphicalSubs)
  133. {
  134. args += GetOutputSizeParam(state, codec);
  135. }
  136. var qualityParam = GetVideoQualityParam(state, codec, false);
  137. if (!string.IsNullOrEmpty(qualityParam))
  138. {
  139. args += " " + qualityParam.Trim();
  140. }
  141. // This is for internal graphical subs
  142. if (hasGraphicalSubs)
  143. {
  144. args += GetGraphicalSubtitleParam(state, codec);
  145. }
  146. return args;
  147. }
  148. /// <summary>
  149. /// Gets audio arguments to pass to ffmpeg
  150. /// </summary>
  151. /// <param name="state">The state.</param>
  152. /// <returns>System.String.</returns>
  153. private string GetAudioArguments(StreamState state)
  154. {
  155. // If the video doesn't have an audio stream, return a default.
  156. if (state.AudioStream == null && state.VideoStream != null)
  157. {
  158. return string.Empty;
  159. }
  160. // Get the output codec name
  161. var codec = state.OutputAudioCodec;
  162. var args = "-codec:a:0 " + codec;
  163. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  164. {
  165. return args;
  166. }
  167. // Add the number of audio channels
  168. var channels = state.OutputAudioChannels;
  169. if (channels.HasValue)
  170. {
  171. args += " -ac " + channels.Value;
  172. }
  173. var bitrate = state.OutputAudioBitrate;
  174. if (bitrate.HasValue)
  175. {
  176. args += " -ab " + bitrate.Value.ToString(UsCulture);
  177. }
  178. args += " " + GetAudioFilterParam(state, false);
  179. return args;
  180. }
  181. }
  182. }