AudioService.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using MediaBrowser.Controller;
  2. using ServiceStack.ServiceHost;
  3. using System.Collections.Generic;
  4. namespace MediaBrowser.Api.Playback.Progressive
  5. {
  6. /// <summary>
  7. /// Class GetAudioStream
  8. /// </summary>
  9. [Route("/Audio/{Id}.mp3", "GET")]
  10. [Route("/Audio/{Id}.wma", "GET")]
  11. [Route("/Audio/{Id}.aac", "GET")]
  12. [Route("/Audio/{Id}.flac", "GET")]
  13. [Route("/Audio/{Id}.ogg", "GET")]
  14. [Route("/Audio/{Id}", "GET")]
  15. public class GetAudioStream : StreamRequest
  16. {
  17. }
  18. /// <summary>
  19. /// Class AudioService
  20. /// </summary>
  21. public class AudioService : BaseProgressiveStreamingService
  22. {
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
  25. /// </summary>
  26. /// <param name="appPaths">The app paths.</param>
  27. public AudioService(IServerApplicationPaths appPaths)
  28. : base(appPaths)
  29. {
  30. }
  31. /// <summary>
  32. /// Gets the specified request.
  33. /// </summary>
  34. /// <param name="request">The request.</param>
  35. /// <returns>System.Object.</returns>
  36. public object Get(GetAudioStream request)
  37. {
  38. return ProcessRequest(request);
  39. }
  40. /// <summary>
  41. /// Gets the command line arguments.
  42. /// </summary>
  43. /// <param name="outputPath">The output path.</param>
  44. /// <param name="state">The state.</param>
  45. /// <returns>System.String.</returns>
  46. /// <exception cref="System.InvalidOperationException">Only aac and mp3 audio codecs are supported.</exception>
  47. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  48. {
  49. var request = state.Request;
  50. var audioTranscodeParams = new List<string>();
  51. if (request.AudioBitRate.HasValue)
  52. {
  53. audioTranscodeParams.Add("-ab " + request.AudioBitRate.Value);
  54. }
  55. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  56. if (channels.HasValue)
  57. {
  58. audioTranscodeParams.Add("-ac " + channels.Value);
  59. }
  60. if (request.AudioSampleRate.HasValue)
  61. {
  62. audioTranscodeParams.Add("-ar " + request.AudioSampleRate.Value);
  63. }
  64. const string vn = " -vn";
  65. return string.Format("{0} -i {1}{2} -threads 0{5} {3} -id3v2_version 3 -write_id3v1 1 \"{4}\"",
  66. GetFastSeekCommandLineParameter(request),
  67. GetInputArgument(state.Item, state.IsoMount),
  68. GetSlowSeekCommandLineParameter(request),
  69. string.Join(" ", audioTranscodeParams.ToArray()),
  70. outputPath,
  71. vn).Trim();
  72. }
  73. }
  74. }