AudioHandler.cs 4.1 KB

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