jellyfin/MediaBrowser.Api/HttpHandlers/VideoHandler.cs

437 lines
14 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Drawing;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller.Entities;
2012-09-09 15:15:22 +00:00
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
/// </summary>
[Export(typeof(BaseHandler))]
2012-09-09 15:15:22 +00:00
class VideoHandler : BaseMediaHandler<Video, VideoOutputFormats>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("video", request);
}
2012-09-09 13:56:04 +00:00
/// <summary>
/// We can output these files directly, but we can't encode them
/// </summary>
2012-09-09 15:15:22 +00:00
protected override IEnumerable<VideoOutputFormats> UnsupportedOutputEncodingFormats
{
get
{
2012-08-14 02:24:35 +00:00
// mp4, 3gp, mov - muxer does not support non-seekable output
// avi, mov, mkv, m4v - can't stream these when encoding. the player will try to download them completely before starting playback.
// wmv - can't seem to figure out the output format name
2012-09-09 15:15:22 +00:00
return new VideoOutputFormats[] { VideoOutputFormats.Mp4, VideoOutputFormats.ThreeGP, VideoOutputFormats.M4v, VideoOutputFormats.Mkv, VideoOutputFormats.Avi, VideoOutputFormats.Mov, VideoOutputFormats.Wmv };
}
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Determines whether or not we can just output the original file directly
/// </summary>
protected override bool RequiresConversion()
{
2012-08-13 02:14:39 +00:00
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
// For now we won't allow these to pass through.
// Later we'll add some intelligence to allow it when possible
2012-08-13 02:22:34 +00:00
if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
2012-08-13 02:14:39 +00:00
{
return true;
}
if (base.RequiresConversion())
{
return true;
}
2012-09-09 15:15:22 +00:00
// See if the video requires conversion
if (RequiresVideoConversion())
{
return true;
}
2012-09-09 15:15:22 +00:00
// See if the audio requires conversion
AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
if (audioStream != null)
{
if (RequiresAudioConversion(audioStream))
{
return true;
}
}
// Yay
return false;
}
2012-08-13 02:14:39 +00:00
/// <summary>
2012-09-09 15:15:22 +00:00
/// Translates the output file extension to the format param that follows "-f" on the ffmpeg command line
2012-08-13 02:14:39 +00:00
/// </summary>
2012-09-09 15:15:22 +00:00
private string GetFFMpegOutputFormat(VideoOutputFormats outputFormat)
{
2012-09-09 15:15:22 +00:00
if (outputFormat == VideoOutputFormats.Mkv)
{
return "matroska";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Ts)
{
return "mpegts";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Ogv)
{
return "ogg";
}
2012-09-09 15:15:22 +00:00
return outputFormat.ToString().ToLower();
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
protected override string GetCommandLineArguments()
{
List<string> audioTranscodeParams = new List<string>();
2012-09-09 15:15:22 +00:00
VideoOutputFormats outputFormat = GetConversionOutputFormat();
2012-08-13 02:14:39 +00:00
return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
LibraryItem.Path,
2012-08-13 02:14:39 +00:00
GetVideoArguments(outputFormat),
GetAudioArguments(outputFormat),
GetFFMpegOutputFormat(outputFormat)
);
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
private string GetVideoArguments(VideoOutputFormats outputFormat)
2012-08-13 02:14:39 +00:00
{
2012-09-09 15:15:22 +00:00
// Get the output codec name
2012-08-13 02:14:39 +00:00
string codec = GetVideoCodec(outputFormat);
string args = "-vcodec " + codec;
2012-09-09 15:15:22 +00:00
// If we're encoding video, add additional params
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
2012-09-09 15:15:22 +00:00
// Add resolution params, if specified
if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
{
Size size = DrawingUtils.Resize(LibraryItem.Width, LibraryItem.Height, Width, Height, MaxWidth, MaxHeight);
args += string.Format(" -s {0}x{1}", size.Width, size.Height);
}
}
2012-08-13 02:14:39 +00:00
return args;
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets audio arguments to pass to ffmpeg
/// </summary>
private string GetAudioArguments(VideoOutputFormats outputFormat)
2012-08-13 02:14:39 +00:00
{
AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
2012-09-09 15:15:22 +00:00
// If the video doesn't have an audio stream, return empty
if (audioStream == null)
{
return string.Empty;
}
2012-09-09 15:15:22 +00:00
// Get the output codec name
string codec = GetAudioCodec(audioStream, outputFormat);
2012-08-13 02:14:39 +00:00
string args = "-acodec " + codec;
2012-09-09 15:15:22 +00:00
// If we're encoding audio, add additional params
2012-08-13 02:14:39 +00:00
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
2012-09-09 15:15:22 +00:00
// Add the number of audio channels
int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
2012-08-13 02:14:39 +00:00
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
2012-09-09 15:15:22 +00:00
// Add the audio sample rate
int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
if (sampleRate.HasValue)
2012-08-13 02:14:39 +00:00
{
args += " -ar " + sampleRate.Value;
2012-08-13 02:14:39 +00:00
}
}
return args;
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the name of the output video codec
/// </summary>
private string GetVideoCodec(VideoOutputFormats outputFormat)
2012-08-13 02:14:39 +00:00
{
2012-09-09 15:15:22 +00:00
// Some output containers require specific codecs
if (outputFormat == VideoOutputFormats.Webm)
2012-08-13 02:14:39 +00:00
{
// Per webm specification, it must be vpx
return "libvpx";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Asf)
2012-08-13 02:14:39 +00:00
{
return "wmv2";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Wmv)
2012-08-14 02:24:35 +00:00
{
return "wmv2";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Ogv)
2012-08-14 02:24:35 +00:00
{
return "libtheora";
}
2012-08-13 02:14:39 +00:00
2012-09-09 15:15:22 +00:00
// Skip encoding when possible
if (!RequiresVideoConversion())
{
return "copy";
}
return "libx264";
2012-08-13 02:14:39 +00:00
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the name of the output audio codec
/// </summary>
private string GetAudioCodec(AudioStream audioStream, VideoOutputFormats outputFormat)
{
2012-09-09 15:15:22 +00:00
// Some output containers require specific codecs
if (outputFormat == VideoOutputFormats.Webm)
2012-08-13 02:14:39 +00:00
{
// Per webm specification, it must be vorbis
return "libvorbis";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Asf)
2012-08-14 02:24:35 +00:00
{
return "wmav2";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Wmv)
2012-08-14 02:24:35 +00:00
{
return "wmav2";
}
2012-09-09 15:15:22 +00:00
else if (outputFormat == VideoOutputFormats.Ogv)
2012-08-14 02:24:35 +00:00
{
return "libvorbis";
}
2012-09-09 15:15:22 +00:00
// Skip encoding when possible
if (!RequiresAudioConversion(audioStream))
2012-08-13 02:14:39 +00:00
{
return "copy";
2012-08-13 02:14:39 +00:00
}
return "libvo_aacenc";
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the number of audio channels to specify on the command line
/// </summary>
private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
{
2012-08-14 02:24:35 +00:00
if (libraryItemChannels > 2)
2012-08-13 02:14:39 +00:00
{
2012-08-14 02:24:35 +00:00
if (audioCodec.Equals("libvo_aacenc"))
{
// libvo_aacenc currently only supports two channel output
return 2;
}
else if (audioCodec.Equals("wmav2"))
{
// wmav2 currently only supports two channel output
return 2;
}
2012-08-13 02:14:39 +00:00
}
return GetNumAudioChannelsParam(libraryItemChannels);
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Determines if the video stream requires encoding
/// </summary>
private bool RequiresVideoConversion()
{
// Check dimensions
2012-09-09 15:15:22 +00:00
// If a specific width is required, validate that
if (Width.HasValue)
{
if (Width.Value != LibraryItem.Width)
{
return true;
}
}
2012-09-09 15:15:22 +00:00
// If a specific height is required, validate that
if (Height.HasValue)
{
if (Height.Value != LibraryItem.Height)
{
return true;
}
}
2012-09-09 15:15:22 +00:00
// If a max width is required, validate that
if (MaxWidth.HasValue)
{
if (MaxWidth.Value < LibraryItem.Width)
{
return true;
}
}
2012-09-09 15:15:22 +00:00
// If a max height is required, validate that
if (MaxHeight.HasValue)
{
if (MaxHeight.Value < LibraryItem.Height)
{
return true;
}
}
2012-09-09 15:15:22 +00:00
// If the codec is already h264, don't encode
if (LibraryItem.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || LibraryItem.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
return false;
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Determines if the audio stream requires encoding
/// </summary>
private bool RequiresAudioConversion(AudioStream audio)
{
2012-09-09 15:15:22 +00:00
// If the input stream has more audio channels than the client can handle, we need to encode
2012-08-14 02:24:35 +00:00
if (AudioChannels.HasValue)
{
2012-08-14 02:24:35 +00:00
if (audio.Channels > AudioChannels.Value)
{
return true;
2012-08-14 02:24:35 +00:00
}
}
2012-09-09 15:15:22 +00:00
// Aac, ac-3 and mp3 are all pretty much universally supported. No need to encode them
if (audio.Codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
2012-09-09 15:15:22 +00:00
if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
2012-09-09 15:15:22 +00:00
if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
return true;
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the fixed output video height, in pixels
/// </summary>
private int? Height
{
get
{
string val = QueryString["height"];
if (string.IsNullOrEmpty(val))
{
return null;
}
return int.Parse(val);
}
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the fixed output video width, in pixels
/// </summary>
private int? Width
{
get
{
string val = QueryString["width"];
if (string.IsNullOrEmpty(val))
{
return null;
}
return int.Parse(val);
}
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the maximum output video height, in pixels
/// </summary>
private int? MaxHeight
{
get
{
string val = QueryString["maxheight"];
if (string.IsNullOrEmpty(val))
{
return null;
}
return int.Parse(val);
}
}
2012-09-09 15:15:22 +00:00
/// <summary>
/// Gets the maximum output video width, in pixels
/// </summary>
private int? MaxWidth
{
get
{
string val = QueryString["maxwidth"];
if (string.IsNullOrEmpty(val))
{
return null;
}
return int.Parse(val);
}
}
}
}