jellyfin-server/MediaBrowser.Api/HttpHandlers/AudioHandler.cs

120 lines
3.8 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller.Entities;
2012-09-09 13:56:04 +00:00
using MediaBrowser.Model.DTO;
using System.Collections.Generic;
using System.ComponentModel.Composition;
2012-08-10 15:17:52 +00:00
using System.IO;
using System.Net;
2012-08-10 13:18:30 +00:00
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
/// </summary>
[Export(typeof(BaseHandler))]
2012-09-09 13:56:04 +00:00
public class AudioHandler : BaseMediaHandler<Audio, AudioOutputFormats>
2012-08-10 13:18:30 +00:00
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("audio", request);
}
/// <summary>
2012-09-09 13:56:04 +00:00
/// We can output these formats directly, but we cannot encode to them.
/// </summary>
2012-09-09 13:56:04 +00:00
protected override IEnumerable<AudioOutputFormats> UnsupportedOutputEncodingFormats
2012-08-10 15:17:52 +00:00
{
get
{
2012-09-09 15:15:22 +00:00
return new AudioOutputFormats[] { AudioOutputFormats.Aac, AudioOutputFormats.Flac, AudioOutputFormats.Wma };
2012-08-10 15:17:52 +00:00
}
}
2012-09-09 13:56:04 +00:00
private int? GetMaxAcceptedBitRate(AudioOutputFormats audioFormat)
2012-08-10 15:17:52 +00:00
{
2012-09-09 13:56:04 +00:00
return GetMaxAcceptedBitRate(audioFormat.ToString());
2012-08-10 13:18:30 +00:00
}
2012-08-10 15:17:52 +00:00
private int? GetMaxAcceptedBitRate(string audioFormat)
2012-08-10 15:17:52 +00:00
{
2012-09-09 13:56:04 +00:00
if (audioFormat.Equals("mp3", System.StringComparison.OrdinalIgnoreCase))
2012-08-11 18:12:34 +00:00
{
2012-09-09 13:56:04 +00:00
return 320000;
2012-08-11 18:12:34 +00:00
}
2012-09-09 13:56:04 +00:00
return null;
2012-08-10 15:17:52 +00:00
}
/// <summary>
/// Determines whether or not the original file requires transcoding
/// </summary>
protected override bool RequiresConversion()
2012-08-10 15:17:52 +00:00
{
if (base.RequiresConversion())
2012-08-10 15:17:52 +00:00
{
return true;
}
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
2012-09-09 13:56:04 +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
if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
2012-08-10 15:17:52 +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
if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
2012-08-10 15:17:52 +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
if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
2012-08-10 15:17:52 +00:00
{
return true;
2012-08-10 15:17:52 +00:00
}
2012-08-10 15:17:52 +00:00
// Yay
return false;
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
protected override string GetCommandLineArguments()
2012-08-10 15:17:52 +00:00
{
2012-09-11 18:20:12 +00:00
var audioTranscodeParams = new List<string>();
2012-08-10 15:17:52 +00:00
2012-09-09 13:56:04 +00:00
AudioOutputFormats outputFormat = GetConversionOutputFormat();
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
if (bitrate.HasValue)
2012-08-10 15:17:52 +00:00
{
audioTranscodeParams.Add("-ab " + bitrate.Value);
2012-08-10 15:17:52 +00:00
}
int? channels = GetNumAudioChannelsParam(LibraryItem.Channels);
if (channels.HasValue)
2012-08-10 15:17:52 +00:00
{
audioTranscodeParams.Add("-ac " + channels.Value);
2012-08-10 15:17:52 +00:00
}
int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate);
if (sampleRate.HasValue)
2012-08-10 15:17:52 +00:00
{
audioTranscodeParams.Add("-ar " + sampleRate.Value);
2012-08-10 15:17:52 +00:00
}
audioTranscodeParams.Add("-f " + outputFormat);
return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
2012-08-10 13:18:30 +00:00
}
}