Play embedded video resource as stream

2019-03-15 21:48发布

问题:

EDIT: I changed my question to better clarify the issue. How is it possible to play a video from a byte array (taken from embedded resource) using DirectShow.Net library?

Since I'm going to prevent users from accessing the video file, I need to embed the video file as resource and play it.

Thanks in advance.

回答1:

It's a bit non-standard, but you could use something like WCF to self-host an endpoint inside your desktop application. Then set the source of the video input to be the "URL" to your self-hosted endpoint. That would work for WPF or WinForms. Not sure about Silverlight though.

The self-hosted endpoint could pull the media from your embedded resources and stream it from there.



回答2:

It sounds to me like the problem is not so much how to use the DirectShow library (the `DirectShow.Net Forum is specifically designed for that), but rather how to use an embedded resource.

I ran into something similar a few years back on a contract job where an employer was worried that some customer might steal his proprietary information. My information was in hundreds of PDF documents, but the idea works the same for video files.

Here's how I tackled the problem:

  • First, place the video file in your list of resources: I use Visual Studio, so I go to the Project's Properties, click the Resources tab, select the Files option, then select Add Resource > Add Existing File...

  • Add the following two namespaces to the code file you will be using:

using System.IO;
using System.Diagnostics;
  • Finally, where you want to play your video file, just do something similar to the following:
 Process player = null;
 string tempFile = "~clip000.dat";
 try {
   File.WriteAllBytes(tempFile, Properties.Resources.MyMovie_AVI);
   player = Process.Start(tempFile);
   player.WaitForExit();
 } finally {
   File.Delete(tempFile);
 }

Most likely, you will not be calling the Process.Start method, but rather the appropriate DirectShow method. The idea is still the same: Extract your resources as a byte array, write them to a new, temporary file, use the file, then delete that file whenever you are done.

Be sure to put the Delete statement in the finally block so that if any errors occur or your user closes the program while the file is still playing, your application still cleans up the old file.

EDIT:

I think this might be a viable way of doing this:

using (MemoryStream ms = new MemoryStream(Properties.Resources.MyMovie_AVI)) {
  // Now you have to find a way in `DirectShow` to use a Stream
}


回答3:

Can you use a different library?

I used the WPF MediaKit to do some non-standard streaming of a secure, live h264 video stream. The developer (Jermiah Morill) was very responsive, and the customization I could perform was extensive (since you get the source).

At that point, you could embed the video as an embedded resource, load the byte array (perhaps either part of it at a time or the entire file) into memory, and play from memory.