home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 2
Plugin Development
Featured Plugins
MP2Extended
Plugin: MP2Extended
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="FreakyJ" data-source="post: 1158877" data-attributes="member: 106003"><p>In fact it does: </p><p></p><p>[CODE=C#]public static Stream ExtractImage(MediaSource source, long position, int? maxWidth, int? maxHeight, string borders, string format)</p><p> {</p><p> if (!source.Exists)</p><p> {</p><p> Log.Warn("ExtractImage: Source {0} (resolved to path {1}) doesn't exists", source.GetDebugName(), source.GetPath());</p><p> WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotFound);</p><p> return Stream.Null;</p><p> }</p><p></p><p> if (!source.SupportsDirectAccess)</p><p> {</p><p> Log.Warn("ExtractImage: Extracting images from remote sources isn't supported yet", source.MediaType, source.Id);</p><p> WCFUtil.SetResponseCode(System.Net.HttpStatusCode.NotImplemented);</p><p> return Stream.Null;</p><p> }</p><p></p><p> // get temporary filename</p><p> string filename = String.Format("extract_{0}_{1}.jpg", source.GetUniqueIdentifier(), position);</p><p> string tempFile = cache.GetPath(filename);</p><p></p><p> // maybe it exists in cache, return that then</p><p> if (cache.Contains(filename))</p><p> {</p><p> // if source is newer, cached image needs to be recreated</p><p> if (source.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(tempFile))</p><p> {</p><p> cache.Invalidate(filename);</p><p> }</p><p> else</p><p> {</p><p> return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format);</p><p> }</p><p> }</p><p></p><p> // We need to impersonate to access the network drive and check if the thumbnail already exists. However, because</p><p> // impersonation is lost when starting a process, we can't start ffmpeg from in here. We need to check whether the</p><p> // file is accessible from outside the impersonation context again, and start the ffmpeg process as a different user</p><p> // if that is the case.</p><p> string fullPath;</p><p> using (var context = source.CreateNetworkContext())</p><p> {</p><p> fullPath = context.RewritePath(source.GetPath());</p><p></p><p> // stream a pre-existing thumbnail, if possible</p><p> if (source.MediaType == WebMediaType.Recording && Path.GetExtension(fullPath).ToLower() == ".ts")</p><p> {</p><p> var thumbnailFileInfo = new FileInfo(Path.ChangeExtension(fullPath, ".jpg"));</p><p> if (thumbnailFileInfo.Exists && thumbnailFileInfo.Length > 0)</p><p> return StreamPostprocessedImage(new ImageMediaSource(thumbnailFileInfo.FullName), maxWidth, maxHeight, borders, format);</p><p> }</p><p> }</p><p></p><p> // finally, extract the image with ffmpeg if everything else has failed</p><p> bool extractResult = ExecuteFFMpegExtraction(source, fullPath, position, tempFile);</p><p> if (!extractResult)</p><p> {</p><p> WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError);</p><p> return Stream.Null;</p><p> }</p><p></p><p> return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format);</p><p> }</p><p></p><p> private static bool ExecuteFFMpegExtraction(MediaSource source, string path, long position, string tempFile)</p><p> {</p><p> var info = new ProcessStartInfo();</p><p> info.Arguments = String.Format("-ss {0} -i \"{1}\" -vframes 1 -vf \"yadif,scale=ih*dar:ih\" -f image2 \"{2}\"", position, path, tempFile);</p><p> info.FileName = Configuration.StreamingProfiles.FFMpegPath;</p><p> info.CreateNoWindow = true;</p><p> info.UseShellExecute = false;</p><p> info.RedirectStandardError = Log.IsEnabled(LogLevel.Trace);</p><p> info.RedirectStandardOutput = Log.IsEnabled(LogLevel.Trace);</p><p></p><p> TranscoderProcess proc = new TranscoderProcess();</p><p> proc.StartInfo = info;</p><p> if (source.NeedsImpersonation)</p><p> proc.StartAsUser(Configuration.Services.NetworkImpersonation.Domain, Configuration.Services.NetworkImpersonation.Username, Configuration.Services.NetworkImpersonation.GetPassword());</p><p> else</p><p> proc.Start();</p><p></p><p> if (Log.IsEnabled(LogLevel.Trace))</p><p> {</p><p> Log.Trace("ExtractImage: executing path={0} arguments={1}", info.FileName, info.Arguments);</p><p> StreamCopy.AsyncStreamRead(proc.StandardError, l => Log.Trace("ExtractImage: stderr: {0}", l));</p><p> StreamCopy.AsyncStreamRead(proc.StandardOutput, l => Log.Trace("ExtractImage: stdout: {0}", l));</p><p> }</p><p> proc.WaitForExit();</p><p></p><p> if (!File.Exists(tempFile))</p><p> {</p><p> Log.Warn("Failed to extract image from {0} with {1} {2}", source.GetDebugName(), info.FileName, info.Arguments);</p><p> return false;</p><p> }</p><p></p><p> return true;</p><p> }[/CODE]</p></blockquote><p></p>
[QUOTE="FreakyJ, post: 1158877, member: 106003"] In fact it does: [CODE=C#]public static Stream ExtractImage(MediaSource source, long position, int? maxWidth, int? maxHeight, string borders, string format) { 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; } // get temporary filename string filename = String.Format("extract_{0}_{1}.jpg", source.GetUniqueIdentifier(), position); string tempFile = cache.GetPath(filename); // maybe it exists in cache, return that then if (cache.Contains(filename)) { // if source is newer, cached image needs to be recreated if (source.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(tempFile)) { cache.Invalidate(filename); } else { return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format); } } // We need to impersonate to access the network drive and check if the thumbnail already exists. However, because // impersonation is lost when starting a process, we can't start ffmpeg from in here. We need to check whether the // file is accessible from outside the impersonation context again, and start the ffmpeg process as a different user // if that is the case. string fullPath; using (var context = source.CreateNetworkContext()) { fullPath = context.RewritePath(source.GetPath()); // stream a pre-existing thumbnail, if possible if (source.MediaType == WebMediaType.Recording && Path.GetExtension(fullPath).ToLower() == ".ts") { var thumbnailFileInfo = new FileInfo(Path.ChangeExtension(fullPath, ".jpg")); if (thumbnailFileInfo.Exists && thumbnailFileInfo.Length > 0) return StreamPostprocessedImage(new ImageMediaSource(thumbnailFileInfo.FullName), maxWidth, maxHeight, borders, format); } } // finally, extract the image with ffmpeg if everything else has failed bool extractResult = ExecuteFFMpegExtraction(source, fullPath, position, tempFile); if (!extractResult) { WCFUtil.SetResponseCode(System.Net.HttpStatusCode.InternalServerError); return Stream.Null; } return StreamPostprocessedImage(new ImageMediaSource(tempFile), maxWidth, maxHeight, borders, format); } private static bool ExecuteFFMpegExtraction(MediaSource source, string path, long position, string tempFile) { var info = new ProcessStartInfo(); info.Arguments = String.Format("-ss {0} -i \"{1}\" -vframes 1 -vf \"yadif,scale=ih*dar:ih\" -f image2 \"{2}\"", position, path, tempFile); info.FileName = Configuration.StreamingProfiles.FFMpegPath; info.CreateNoWindow = true; info.UseShellExecute = false; info.RedirectStandardError = Log.IsEnabled(LogLevel.Trace); info.RedirectStandardOutput = Log.IsEnabled(LogLevel.Trace); TranscoderProcess proc = new TranscoderProcess(); proc.StartInfo = info; if (source.NeedsImpersonation) proc.StartAsUser(Configuration.Services.NetworkImpersonation.Domain, Configuration.Services.NetworkImpersonation.Username, Configuration.Services.NetworkImpersonation.GetPassword()); else proc.Start(); if (Log.IsEnabled(LogLevel.Trace)) { Log.Trace("ExtractImage: executing path={0} arguments={1}", info.FileName, info.Arguments); StreamCopy.AsyncStreamRead(proc.StandardError, l => Log.Trace("ExtractImage: stderr: {0}", l)); StreamCopy.AsyncStreamRead(proc.StandardOutput, l => Log.Trace("ExtractImage: stdout: {0}", l)); } proc.WaitForExit(); if (!File.Exists(tempFile)) { Log.Warn("Failed to extract image from {0} with {1} {2}", source.GetDebugName(), info.FileName, info.Arguments); return false; } return true; }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 2
Plugin Development
Featured Plugins
MP2Extended
Plugin: MP2Extended
Contact us
RSS
Top
Bottom