2012-09-08 14:52:13 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.Composition;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
using System.IO;
|
2012-08-11 03:13:56 +00:00
|
|
|
|
using System.Linq;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System.Net;
|
2012-08-10 13:18:30 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-08-12 17:05:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
|
|
|
|
|
/// </summary>
|
2012-09-08 14:52:13 +00:00
|
|
|
|
[Export(typeof(BaseHandler))]
|
2012-08-11 18:07:07 +00:00
|
|
|
|
public class AudioHandler : BaseMediaHandler<Audio>
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
public override bool HandlesRequest(HttpListenerRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ApiService.IsApiUrlMatch("audio", request);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 15:41:40 +00:00
|
|
|
|
/// <summary>
|
2012-08-12 17:05:51 +00:00
|
|
|
|
/// Overriding to provide mp3 as a default, since pretty much every device supports it
|
2012-08-12 15:41:40 +00:00
|
|
|
|
/// </summary>
|
2012-08-12 17:05:51 +00:00
|
|
|
|
protected override IEnumerable<string> OutputFormats
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-12 17:05:51 +00:00
|
|
|
|
IEnumerable<string> vals = base.OutputFormats;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-12 17:05:51 +00:00
|
|
|
|
return vals.Any() ? vals : new string[] { "mp3" };
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-12 17:05:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// We can output these files directly, but we can't encode them
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new string[] { "wma", "aac" };
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
public IEnumerable<int> AudioBitRates
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-12 15:41:40 +00:00
|
|
|
|
string val = QueryString["audiobitrates"];
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return new int[] { };
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return val.Split(',').Select(v => int.Parse(v));
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
private int? GetMaxAcceptedBitRate(string audioFormat)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 18:12:34 +00:00
|
|
|
|
if (!AudioBitRates.Any())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 17:05:51 +00:00
|
|
|
|
int index = OutputFormats.ToList().IndexOf(audioFormat);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
2012-08-11 18:12:34 +00:00
|
|
|
|
return AudioBitRates.ElementAt(index);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether or not the original file requires transcoding
|
|
|
|
|
/// </summary>
|
2012-08-11 18:07:07 +00:00
|
|
|
|
protected override bool RequiresConversion()
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-12 17:05:51 +00:00
|
|
|
|
if (base.RequiresConversion())
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 17:05:51 +00:00
|
|
|
|
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
|
|
|
|
|
|
2012-08-10 15:17:52 +00:00
|
|
|
|
// If the bitrate is greater than our desired bitrate, we need to transcode
|
2012-08-11 18:07:07 +00:00
|
|
|
|
if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return true;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the number of channels is greater than our desired channels, we need to transcode
|
2012-08-11 18:07:07 +00:00
|
|
|
|
if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return true;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the sample rate is greater than our desired sample rate, we need to transcode
|
2012-08-11 18:07:07 +00:00
|
|
|
|
if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return true;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
2012-08-11 03:13:56 +00:00
|
|
|
|
|
2012-08-10 15:17:52 +00:00
|
|
|
|
// Yay
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
2012-08-12 04:03:19 +00:00
|
|
|
|
protected override string GetCommandLineArguments()
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
List<string> audioTranscodeParams = new List<string>();
|
|
|
|
|
|
2012-08-12 17:05:51 +00:00
|
|
|
|
string outputFormat = GetConversionOutputFormat();
|
2012-08-11 18:07:07 +00:00
|
|
|
|
|
|
|
|
|
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
|
|
|
|
|
|
|
|
|
|
if (bitrate.HasValue)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
audioTranscodeParams.Add("-ab " + bitrate.Value);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 20:18:02 +00:00
|
|
|
|
int? channels = GetNumAudioChannelsParam(LibraryItem.Channels);
|
2012-08-13 17:31:44 +00:00
|
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-13 17:31:44 +00:00
|
|
|
|
audioTranscodeParams.Add("-ac " + channels.Value);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 20:18:02 +00:00
|
|
|
|
int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate);
|
2012-08-13 17:31:44 +00:00
|
|
|
|
|
|
|
|
|
if (sampleRate.HasValue)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-13 17:31:44 +00:00
|
|
|
|
audioTranscodeParams.Add("-ar " + sampleRate.Value);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
audioTranscodeParams.Add("-f " + outputFormat);
|
2012-08-11 03:13:56 +00:00
|
|
|
|
|
|
|
|
|
return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
|
|
|
|
|
}
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|