HlsVideoPlaylistHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Controller.Entities;
  3. using System;
  4. using System.Net;
  5. namespace MediaBrowser.Api.Streaming
  6. {
  7. /// <summary>
  8. /// Class HlsVideoPlaylistHandler
  9. /// </summary>
  10. public class HlsVideoPlaylistHandler : BaseHlsPlaylistHandler<Video>
  11. {
  12. /// <summary>
  13. /// Handleses the request.
  14. /// </summary>
  15. /// <param name="request">The request.</param>
  16. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  17. public override bool HandlesRequest(HttpListenerRequest request)
  18. {
  19. return ApiService.IsApiUrlMatch("video.m3u8", request);
  20. }
  21. /// <summary>
  22. /// Gets the segment file extension.
  23. /// </summary>
  24. /// <value>The segment file extension.</value>
  25. protected override string SegmentFileExtension
  26. {
  27. get { return ".ts"; }
  28. }
  29. /// <summary>
  30. /// Gets the video arguments.
  31. /// </summary>
  32. /// <returns>System.String.</returns>
  33. protected override string GetVideoArguments()
  34. {
  35. var codec = GetVideoCodec();
  36. // Right now all we support is either h264 or copy
  37. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase) && !codec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  38. {
  39. codec = "libx264";
  40. }
  41. // See if we can save come cpu cycles by avoiding encoding
  42. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  43. {
  44. return IsH264(VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  45. }
  46. var args = "-codec:v:0 " + codec + " -preset superfast";
  47. if (VideoBitRate.HasValue)
  48. {
  49. args += string.Format(" -b:v {0}", VideoBitRate.Value);
  50. }
  51. // Add resolution params, if specified
  52. if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
  53. {
  54. args += GetOutputSizeParam(codec);
  55. }
  56. // Get the output framerate based on the FrameRate param
  57. double framerate = FrameRate ?? 0;
  58. // We have to supply a framerate for hls, so if it's null, account for that here
  59. if (framerate.Equals(0))
  60. {
  61. framerate = VideoStream.AverageFrameRate ?? 0;
  62. }
  63. if (framerate.Equals(0))
  64. {
  65. framerate = VideoStream.RealFrameRate ?? 0;
  66. }
  67. if (framerate.Equals(0))
  68. {
  69. framerate = 23.976;
  70. }
  71. args += string.Format(" -r {0}", framerate);
  72. // Needed to ensure segments stay under 10 seconds
  73. args += string.Format(" -g {0}", framerate);
  74. return args;
  75. }
  76. /// <summary>
  77. /// Gets the audio arguments to pass to ffmpeg
  78. /// </summary>
  79. /// <returns>System.String.</returns>
  80. protected override string GetAudioArguments()
  81. {
  82. if (!AudioCodec.HasValue)
  83. {
  84. return "-codec:a:0 copy";
  85. }
  86. var codec = GetAudioCodec();
  87. var args = "-codec:a:0 " + codec;
  88. if (AudioStream != null)
  89. {
  90. var channels = GetNumAudioChannelsParam();
  91. if (channels.HasValue)
  92. {
  93. args += " -ac " + channels.Value;
  94. }
  95. var sampleRate = GetSampleRateParam();
  96. if (sampleRate.HasValue)
  97. {
  98. args += " -ar " + sampleRate.Value;
  99. }
  100. if (AudioBitRate.HasValue)
  101. {
  102. args += " -ab " + AudioBitRate.Value;
  103. }
  104. return args;
  105. }
  106. return args;
  107. }
  108. }
  109. }