AudioHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Model.Entities;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.Composition;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. namespace MediaBrowser.Api.HttpHandlers
  9. {
  10. /// <summary>
  11. /// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
  12. /// </summary>
  13. [Export(typeof(BaseHandler))]
  14. public class AudioHandler : BaseMediaHandler<Audio>
  15. {
  16. public override bool HandlesRequest(HttpListenerRequest request)
  17. {
  18. return ApiService.IsApiUrlMatch("audio", request);
  19. }
  20. /// <summary>
  21. /// Overriding to provide mp3 as a default, since pretty much every device supports it
  22. /// </summary>
  23. protected override IEnumerable<string> OutputFormats
  24. {
  25. get
  26. {
  27. IEnumerable<string> vals = base.OutputFormats;
  28. return vals.Any() ? vals : new string[] { "mp3" };
  29. }
  30. }
  31. /// <summary>
  32. /// We can output these files directly, but we can't encode them
  33. /// </summary>
  34. protected override IEnumerable<string> UnsupportedOutputEncodingFormats
  35. {
  36. get
  37. {
  38. return new string[] { "wma", "aac" };
  39. }
  40. }
  41. public IEnumerable<int> AudioBitRates
  42. {
  43. get
  44. {
  45. string val = QueryString["audiobitrates"];
  46. if (string.IsNullOrEmpty(val))
  47. {
  48. return new int[] { };
  49. }
  50. return val.Split(',').Select(v => int.Parse(v));
  51. }
  52. }
  53. private int? GetMaxAcceptedBitRate(string audioFormat)
  54. {
  55. if (!AudioBitRates.Any())
  56. {
  57. return null;
  58. }
  59. int index = OutputFormats.ToList().IndexOf(audioFormat);
  60. return AudioBitRates.ElementAt(index);
  61. }
  62. /// <summary>
  63. /// Determines whether or not the original file requires transcoding
  64. /// </summary>
  65. protected override bool RequiresConversion()
  66. {
  67. if (base.RequiresConversion())
  68. {
  69. return true;
  70. }
  71. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  72. int? bitrate = GetMaxAcceptedBitRate(currentFormat);
  73. // If the bitrate is greater than our desired bitrate, we need to transcode
  74. if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
  75. {
  76. return true;
  77. }
  78. // If the number of channels is greater than our desired channels, we need to transcode
  79. if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
  80. {
  81. return true;
  82. }
  83. // If the sample rate is greater than our desired sample rate, we need to transcode
  84. if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
  85. {
  86. return true;
  87. }
  88. // Yay
  89. return false;
  90. }
  91. /// <summary>
  92. /// Creates arguments to pass to ffmpeg
  93. /// </summary>
  94. protected override string GetCommandLineArguments()
  95. {
  96. List<string> audioTranscodeParams = new List<string>();
  97. string outputFormat = GetConversionOutputFormat();
  98. int? bitrate = GetMaxAcceptedBitRate(outputFormat);
  99. if (bitrate.HasValue)
  100. {
  101. audioTranscodeParams.Add("-ab " + bitrate.Value);
  102. }
  103. int? channels = GetNumAudioChannelsParam(LibraryItem.Channels);
  104. if (channels.HasValue)
  105. {
  106. audioTranscodeParams.Add("-ac " + channels.Value);
  107. }
  108. int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate);
  109. if (sampleRate.HasValue)
  110. {
  111. audioTranscodeParams.Add("-ar " + sampleRate.Value);
  112. }
  113. audioTranscodeParams.Add("-f " + outputFormat);
  114. return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
  115. }
  116. }
  117. }