Moved more common code from audio/video handler down to the base class

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent 537b3553b8
commit 6fe8296266

@ -1,28 +1,36 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
/// </summary>
public class AudioHandler : BaseMediaHandler<Audio>
{
/// <summary>
/// Supported values: mp3,flac,ogg,wav,asf
/// Overriding to provide mp3 as a default, since pretty much every device supports it
/// </summary>
public IEnumerable<string> AudioFormats
protected override IEnumerable<string> OutputFormats
{
get
{
string val = QueryString["audioformats"];
IEnumerable<string> vals = base.OutputFormats;
if (string.IsNullOrEmpty(val))
{
return new string[] { "mp3" };
}
return vals.Any() ? vals : new string[] { "mp3" };
}
}
return val.Split(',');
/// <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" };
}
}
@ -48,7 +56,7 @@ namespace MediaBrowser.Api.HttpHandlers
return null;
}
int index = AudioFormats.ToList().IndexOf(audioFormat);
int index = OutputFormats.ToList().IndexOf(audioFormat);
return AudioBitRates.ElementAt(index);
}
@ -58,14 +66,13 @@ namespace MediaBrowser.Api.HttpHandlers
/// </summary>
protected override bool RequiresConversion()
{
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
// If it's not in a format the consumer accepts, return true
if (!AudioFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
if (base.RequiresConversion())
{
return true;
}
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
// If the bitrate is greater than our desired bitrate, we need to transcode
@ -90,14 +97,6 @@ namespace MediaBrowser.Api.HttpHandlers
return false;
}
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected override string GetOutputFormat()
{
return AudioFormats.First();
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
@ -105,7 +104,7 @@ namespace MediaBrowser.Api.HttpHandlers
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetOutputFormat();
string outputFormat = GetConversionOutputFormat();
int? bitrate = GetMaxAcceptedBitRate(outputFormat);

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
@ -15,6 +17,28 @@ namespace MediaBrowser.Api.HttpHandlers
public abstract class BaseMediaHandler<T> : BaseHandler
where T : BaseItem, new()
{
/// <summary>
/// Supported values: mp3,flac,ogg,wav,asf,wma,aac
/// </summary>
protected virtual IEnumerable<string> OutputFormats
{
get
{
return QueryString["outputformats"].Split(',');
}
}
/// <summary>
/// These formats can be outputted directly but cannot be encoded to
/// </summary>
protected virtual IEnumerable<string> UnsupportedOutputEncodingFormats
{
get
{
return new string[] { };
}
}
private T _LibraryItem;
/// <summary>
/// Gets the library item that will be played, if any
@ -71,7 +95,7 @@ namespace MediaBrowser.Api.HttpHandlers
{
get
{
return MimeTypes.GetMimeType("." + GetOutputFormat());
return MimeTypes.GetMimeType("." + GetConversionOutputFormat());
}
}
@ -97,8 +121,35 @@ namespace MediaBrowser.Api.HttpHandlers
}
protected abstract string GetCommandLineArguments();
protected abstract string GetOutputFormat();
protected abstract bool RequiresConversion();
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected virtual string GetConversionOutputFormat()
{
return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
}
protected virtual bool RequiresConversion()
{
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
if (OutputFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
// We can output these files directly, but we can't encode them
if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
return false;
}
}
else
{
// If it's not in a format the consumer accepts, return true
return true;
}
return false;
}
protected async override Task WriteResponseToOutputStream(Stream stream)
{

@ -5,36 +5,25 @@ using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
/// </summary>
class VideoHandler : BaseMediaHandler<Video>
{
private IEnumerable<string> UnsupportedOutputFormats = new string[] { "mp4" };
public IEnumerable<string> VideoFormats
/// <summary>
/// We can output these files directly, but we can't encode them
/// </summary>
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
{
get
{
return QueryString["videoformats"].Split(',');
return new string[] { "mp4", "wmv" };
}
}
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected override string GetOutputFormat()
{
return VideoFormats.First(f => !UnsupportedOutputFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
}
protected override bool RequiresConversion()
{
// If it's not in a format we can output to, return true
if (UnsupportedOutputFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
// If it's not in a format the consumer accepts, return true
if (!VideoFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
if (base.RequiresConversion())
{
return true;
}
@ -60,15 +49,18 @@ namespace MediaBrowser.Api.HttpHandlers
{
return "matroska";
}
else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
{
return "mpegts";
}
else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
{
return "ogg";
}
return outputFormat;
}
private int GetOutputAudioStreamIndex(string outputFormat)
{
return 0;
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
@ -76,31 +68,10 @@ namespace MediaBrowser.Api.HttpHandlers
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetOutputFormat();
int audioStreamIndex = GetOutputAudioStreamIndex(outputFormat);
string outputFormat = GetConversionOutputFormat();
List<string> maps = new List<string>();
// Add the video stream
maps.Add("-map 0:0");
// Add the audio stream
if (audioStreamIndex != -1)
{
maps.Add("-map 0:" + (1 + audioStreamIndex));
}
// Add all the subtitle streams
for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
{
maps.Add("-map 0:" + (1 + LibraryItem.AudioStreams.Count() + i));
}
return string.Format("-i \"{0}\" {1} {2} {3} -f {4} -",
return string.Format("-i \"{0}\" {1} {2} -f {3} -",
LibraryItem.Path,
string.Join(" ", maps.ToArray()),
GetVideoArguments(),
GetAudioArguments(),
GetFFMpegOutputFormat(outputFormat)
@ -109,29 +80,12 @@ namespace MediaBrowser.Api.HttpHandlers
private string GetVideoArguments()
{
return "-c:v copy";
return "-vcodec copy";
}
private string GetAudioArguments()
{
return "-c:a copy";
}
private string GetSubtitleArguments()
{
string args = "";
for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
{
if (i > 0)
{
args += " ";
}
args += "-c:s copy";
}
return args;
return "-acodec copy";
}
}
}

Loading…
Cancel
Save