123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- using MediaBrowser.Common.Net.Handlers;
- using MediaBrowser.Controller.Drawing;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Model.DTO;
- using MediaBrowser.Model.Entities;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.Drawing;
- 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))]
- class VideoHandler : BaseMediaHandler<Video, VideoOutputFormats>
- {
- public override bool HandlesRequest(HttpListenerRequest request)
- {
- return ApiService.IsApiUrlMatch("video", request);
- }
- /// <summary>
- /// We can output these files directly, but we can't encode them
- /// </summary>
- protected override IEnumerable<VideoOutputFormats> UnsupportedOutputEncodingFormats
- {
- get
- {
- // 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
- return new VideoOutputFormats[] { VideoOutputFormats.Mp4, VideoOutputFormats.ThreeGp, VideoOutputFormats.M4V, VideoOutputFormats.Mkv, VideoOutputFormats.Avi, VideoOutputFormats.Mov, VideoOutputFormats.Wmv };
- }
- }
- /// <summary>
- /// Determines whether or not we can just output the original file directly
- /// </summary>
- protected override bool RequiresConversion()
- {
- if (base.RequiresConversion())
- {
- return true;
- }
- // See if the video requires conversion
- if (RequiresVideoConversion())
- {
- return true;
- }
- // 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;
- }
- /// <summary>
- /// Translates the output file extension to the format param that follows "-f" on the ffmpeg command line
- /// </summary>
- private string GetFfMpegOutputFormat(VideoOutputFormats outputFormat)
- {
- if (outputFormat == VideoOutputFormats.Mkv)
- {
- return "matroska";
- }
- if (outputFormat == VideoOutputFormats.Ts)
- {
- return "mpegts";
- }
- if (outputFormat == VideoOutputFormats.Ogv)
- {
- return "ogg";
- }
- return outputFormat.ToString().ToLower();
- }
- /// <summary>
- /// Creates arguments to pass to ffmpeg
- /// </summary>
- protected override string GetCommandLineArguments()
- {
- VideoOutputFormats outputFormat = GetConversionOutputFormat();
- return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
- LibraryItem.Path,
- GetVideoArguments(outputFormat),
- GetAudioArguments(outputFormat),
- GetFfMpegOutputFormat(outputFormat)
- );
- }
- /// <summary>
- /// Gets video arguments to pass to ffmpeg
- /// </summary>
- private string GetVideoArguments(VideoOutputFormats outputFormat)
- {
- // Get the output codec name
- string codec = GetVideoCodec(outputFormat);
- string args = "-vcodec " + codec;
- // If we're encoding video, add additional params
- if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
- {
- // 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);
- }
- }
- return args;
- }
- /// <summary>
- /// Gets audio arguments to pass to ffmpeg
- /// </summary>
- private string GetAudioArguments(VideoOutputFormats outputFormat)
- {
- AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
- // If the video doesn't have an audio stream, return empty
- if (audioStream == null)
- {
- return string.Empty;
- }
- // Get the output codec name
- string codec = GetAudioCodec(audioStream, outputFormat);
- string args = "-acodec " + codec;
- // If we're encoding audio, add additional params
- if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
- {
- // Add the number of audio channels
- int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
- if (channels.HasValue)
- {
- args += " -ac " + channels.Value;
- }
- // Add the audio sample rate
- int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
- if (sampleRate.HasValue)
- {
- args += " -ar " + sampleRate.Value;
- }
- }
- return args;
- }
- /// <summary>
- /// Gets the name of the output video codec
- /// </summary>
- private string GetVideoCodec(VideoOutputFormats outputFormat)
- {
- // Some output containers require specific codecs
- if (outputFormat == VideoOutputFormats.Webm)
- {
- // Per webm specification, it must be vpx
- return "libvpx";
- }
- if (outputFormat == VideoOutputFormats.Asf)
- {
- return "wmv2";
- }
- if (outputFormat == VideoOutputFormats.Wmv)
- {
- return "wmv2";
- }
- if (outputFormat == VideoOutputFormats.Ogv)
- {
- return "libtheora";
- }
- // Skip encoding when possible
- if (!RequiresVideoConversion())
- {
- return "copy";
- }
- return "libx264";
- }
- /// <summary>
- /// Gets the name of the output audio codec
- /// </summary>
- private string GetAudioCodec(AudioStream audioStream, VideoOutputFormats outputFormat)
- {
- // Some output containers require specific codecs
- if (outputFormat == VideoOutputFormats.Webm)
- {
- // Per webm specification, it must be vorbis
- return "libvorbis";
- }
- if (outputFormat == VideoOutputFormats.Asf)
- {
- return "wmav2";
- }
- if (outputFormat == VideoOutputFormats.Wmv)
- {
- return "wmav2";
- }
- if (outputFormat == VideoOutputFormats.Ogv)
- {
- return "libvorbis";
- }
- // Skip encoding when possible
- if (!RequiresAudioConversion(audioStream))
- {
- return "copy";
- }
- return "libvo_aacenc";
- }
- /// <summary>
- /// Gets the number of audio channels to specify on the command line
- /// </summary>
- private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
- {
- if (libraryItemChannels > 2)
- {
- if (audioCodec.Equals("libvo_aacenc"))
- {
- // libvo_aacenc currently only supports two channel output
- return 2;
- }
- if (audioCodec.Equals("wmav2"))
- {
- // wmav2 currently only supports two channel output
- return 2;
- }
- }
- return GetNumAudioChannelsParam(libraryItemChannels);
- }
- /// <summary>
- /// Determines if the video stream requires encoding
- /// </summary>
- private bool RequiresVideoConversion()
- {
- // Check dimensions
- // If a specific width is required, validate that
- if (Width.HasValue)
- {
- if (Width.Value != LibraryItem.Width)
- {
- return true;
- }
- }
- // If a specific height is required, validate that
- if (Height.HasValue)
- {
- if (Height.Value != LibraryItem.Height)
- {
- return true;
- }
- }
- // If a max width is required, validate that
- if (MaxWidth.HasValue)
- {
- if (MaxWidth.Value < LibraryItem.Width)
- {
- return true;
- }
- }
- // If a max height is required, validate that
- if (MaxHeight.HasValue)
- {
- if (MaxHeight.Value < LibraryItem.Height)
- {
- return true;
- }
- }
- // 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;
- }
- /// <summary>
- /// Determines if the audio stream requires encoding
- /// </summary>
- private bool RequiresAudioConversion(AudioStream audio)
- {
- // If the input stream has more audio channels than the client can handle, we need to encode
- if (AudioChannels.HasValue)
- {
- if (audio.Channels > AudioChannels.Value)
- {
- return true;
- }
- }
- // 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;
- }
- if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
- {
- return false;
- }
- if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
- {
- return false;
- }
- return true;
- }
- /// <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);
- }
- }
- /// <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);
- }
- }
- /// <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);
- }
- }
- /// <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);
- }
- }
- }
- }
|