WCF Service to Create Video Thumbnails
-
22-07-2019 - |
Question
I am trying create a WCF service that leverages the WPF MediaPlayer on the server to generate thumbnails for a video that a user uploads. I found a lot oif info on how to render a frame and save it to a file. But the problem is the key event MediaOpened (actually none of the events) I need to tie into doesn't - EDIT fire.
Does anyone know if the WPF MediaPlayer events do not fire if used ion the context of a WCF service?
thanks Michael
Solution
I decided to try and use the Expression Media Encoder 2 SDK and it worked great.
Very little code to generate thumbnails from a video - here is a snippet
public void GenerateThumbnails(string fileName, int numberOfThumbnails)
{ Queue _positionsToThumbnail = new Queue(); Microsoft.Expression.Encoder.MediaItem video = new Microsoft.Expression.Encoder.MediaItem(fileName);
var totalMilliseconds = video.FileDuration.TotalMilliseconds;
//create a queue of timespans for the thumbnails
for (int i = 0; i < numberOfThumbnails; i++)
{
_positionsToThumbnail.Enqueue(TimeSpan.FromMilliseconds((((2 * i) + 1) * totalMilliseconds) / (2 * numberOfThumbnails)));
}
//create the thumbnails and save them to disk
while(_positionsToThumbnail.Count > 0)
{
Bitmap bitMap = video.GetThumbnail(_positionsToThumbnail.Dequeue(), new System.Drawing.Size(100,100));
bitMap.Save(@"F:\thumbs\" + _positionsToThumbnail.Count.ToString() + ".png", ImageFormat.Png);
}
}
OTHER TIPS
You will likely need to render the data on screen, for those events to be fired -- it's all tied to being part of the WPF visual tree; which when running as a service it is not.
There are many ways you could try to resolve this, all of which are convoluted, and likely not going to scale. I suggest using the normal windows media API's (from the Windows Media SDK) to get to the bottom of it.