public static decimal DEFAULT_ASPECT_RATIO = (decimal)16 / 9; // most new material is 16:9 these days public static WebMediaInfo LoadMediaInfoOrSurrogate(MediaSource source) { WebMediaInfo info; try { info = MediaInfoWrapper.GetMediaInfo(source); if (info != null) { return(info); } } catch (Exception ex) { Log.Warn(String.Format("Failed to load MediaInfo for {0}", source.GetDebugName()), ex); } WebMediaInfo surr = new WebMediaInfo(); surr.Duration = 0; surr.SubtitleStreams = new List <WebSubtitleStream>(); surr.AudioStreams = new List <WebAudioStream>() { new WebAudioStream() { Channels = 2, Codec = "Unknown", ID = 1, Index = 0, Language = "und", // yes, that's valid ISO 639 (I think) LanguageFull = "Unknown", } }; surr.VideoStreams = new List <WebVideoStream>() { new WebVideoStream() { Codec = "Unknown", DisplayAspectRatio = DEFAULT_ASPECT_RATIO, DisplayAspectRatioString = "16:9", ID = 2, Index = 0, Height = 720, Width = 1280 } }; return(surr); }
public static WebMediaInfo LoadMediaInfoOrSurrogate(MediaSource source) { WebMediaInfo info = MediaInfoWrapper.GetMediaInfo(source); if (info != null) { return(info); } WebMediaInfo surr = new WebMediaInfo(); surr.Duration = 0; surr.SubtitleStreams = new List <WebSubtitleStream>(); surr.AudioStreams = new List <WebAudioStream>() { new WebAudioStream() { Channels = 2, Codec = "Unknown", ID = 1, Index = 0, Language = "und", // yes, that's valid ISO 639 (I think) LanguageFull = "Unknown", } }; surr.VideoStreams = new List <WebVideoStream>() { new WebVideoStream() { Codec = "Unknown", DisplayAspectRatio = 16 / 9, // this class is primarily used for TV data and that's mostly 16:9 these days afaik DisplayAspectRatioString = "16:9", ID = 2, Index = 0, Height = 1280, // gives this any problems? Width = 720 } }; return(surr); }
public Resolution CalculateSize(TranscoderProfile profile, MediaSource source, WebMediaInfo info = null) { if (!profile.HasVideoStream) { return(new Resolution(0, 0)); } decimal aspect = (decimal)16 / 9; // the default aspect ratio if (source.MediaType != WebStreamMediaType.TV && profile != null) { if (info == null) { info = MediaInfoWrapper.GetMediaInfo(source); } if (info.VideoStreams.Count > 0) { aspect = info.VideoStreams.First().DisplayAspectRatio; } } return(Resolution.Calculate(aspect, profile.MaxOutputWidth, profile.MaxOutputHeight, 2)); }
public static Stream ExtractImage(MediaSource source, long startPosition, int?maxWidth, int?maxHeight) { if (!source.Exists) { Log.Warn("ExtractImage: Source {0} (resolved to path {1}) doesn't exists", source.GetDebugName(), source.GetPath()); WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotFound); return(Stream.Null); } if (!source.SupportsDirectAccess) { Log.Warn("ExtractImage: Extracting images from remote sources isn't supported yet", source.MediaType, source.Id); WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotImplemented); return(Stream.Null); } // calculate size string ffmpegResize = ""; if (maxWidth != null || maxHeight != null) { try { decimal resolution = MediaInfoWrapper.GetMediaInfo(source).VideoStreams.First().DisplayAspectRatio; ffmpegResize = "-s " + Resolution.Calculate(resolution, maxWidth, maxHeight).ToString(); } catch (Exception ex) { Log.Error("Error while getting resolution of video stream, not resizing", ex); } } // get temporary filename string filename = String.Format("extract_{0}_{1}_{2}_{3}.jpg", source.GetUniqueIdentifier(), startPosition, maxWidth == null ? "null" : maxWidth.ToString(), maxHeight == null ? "null" : maxHeight.ToString()); string tempFile = cache.GetPath(filename); // maybe it exists in cache, return that then if (cache.Contains(filename)) { return(StreamImage(new ImageMediaSource(tempFile))); } // execute it ProcessStartInfo info = new ProcessStartInfo(); using (var impersonator = source.GetImpersonator()) { info.Arguments = String.Format("-ss {0} -i \"{1}\" {2} -vframes 1 -f image2 {3}", startPosition, source.GetPath(), ffmpegResize, tempFile); info.FileName = Configuration.Streaming.FFMpegPath; info.CreateNoWindow = true; info.UseShellExecute = false; Process proc = new Process(); proc.StartInfo = info; proc.Start(); proc.WaitForExit(); } // log when failed if (!File.Exists(tempFile)) { Log.Warn("Failed to extract image to temporary file {0} with command {1}", tempFile, info.Arguments); WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError); return(Stream.Null); } return(StreamImage(new ImageMediaSource(tempFile))); }