AudioHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Model.DTO;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.Composition;
  7. using System.IO;
  8. using System.Net;
  9. namespace MediaBrowser.Api.HttpHandlers
  10. {
  11. /// <summary>
  12. /// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
  13. /// </summary>
  14. [Export(typeof(BaseHandler))]
  15. public class AudioHandler : BaseMediaHandler<Audio, AudioOutputFormats>
  16. {
  17. public override bool HandlesRequest(HttpListenerRequest request)
  18. {
  19. return ApiService.IsApiUrlMatch("audio", request);
  20. }
  21. /// <summary>
  22. /// We can output these formats directly, but we cannot encode to them.
  23. /// </summary>
  24. protected override IEnumerable<AudioOutputFormats> UnsupportedOutputEncodingFormats
  25. {
  26. get
  27. {
  28. return new AudioOutputFormats[] { AudioOutputFormats.Aac, AudioOutputFormats.Flac, AudioOutputFormats.Wav, AudioOutputFormats.Wma };
  29. }
  30. }
  31. private int? GetMaxAcceptedBitRate(AudioOutputFormats audioFormat)
  32. {
  33. return GetMaxAcceptedBitRate(audioFormat.ToString());
  34. }
  35. private int? GetMaxAcceptedBitRate(string audioFormat)
  36. {
  37. if (audioFormat.Equals("mp3", System.StringComparison.OrdinalIgnoreCase))
  38. {
  39. return 320000;
  40. }
  41. return null;
  42. }
  43. /// <summary>
  44. /// Determines whether or not the original file requires transcoding
  45. /// </summary>
  46. protected override bool RequiresConversion()
  47. {
  48. if (base.RequiresConversion())
  49. {
  50. return true;
  51. }
  52. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  53. int? bitrate = GetMaxAcceptedBitRate(currentFormat);
  54. // If the bitrate is greater than our desired bitrate, we need to transcode
  55. if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
  56. {
  57. return true;
  58. }
  59. // If the number of channels is greater than our desired channels, we need to transcode
  60. if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
  61. {
  62. return true;
  63. }
  64. // If the sample rate is greater than our desired sample rate, we need to transcode
  65. if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
  66. {
  67. return true;
  68. }
  69. // Yay
  70. return false;
  71. }
  72. /// <summary>
  73. /// Creates arguments to pass to ffmpeg
  74. /// </summary>
  75. protected override string GetCommandLineArguments()
  76. {
  77. List<string> audioTranscodeParams = new List<string>();
  78. AudioOutputFormats outputFormat = GetConversionOutputFormat();
  79. int? bitrate = GetMaxAcceptedBitRate(outputFormat);
  80. if (bitrate.HasValue)
  81. {
  82. audioTranscodeParams.Add("-ab " + bitrate.Value);
  83. }
  84. int? channels = GetNumAudioChannelsParam(LibraryItem.Channels);
  85. if (channels.HasValue)
  86. {
  87. audioTranscodeParams.Add("-ac " + channels.Value);
  88. }
  89. int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate);
  90. if (sampleRate.HasValue)
  91. {
  92. audioTranscodeParams.Add("-ar " + sampleRate.Value);
  93. }
  94. audioTranscodeParams.Add("-f " + outputFormat);
  95. return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
  96. }
  97. }
  98. }