CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Dynamically Generate Windows Media Videos in .NET

I recently worked on a project here at the port archiving our gate camera images in SQL Server.  The cool thing about having these images archived is that we can do all sorts of cool things with the raw bytes, such as stream them as  slideshow, pull up a specific image from a given date, or combine multiple frames to dynamically create a video from a given time period.

Combining images into a WMV movie from .NET turned out to be quite an interesting project.  Kirk Marple, expert in all things media,graciously donated a bunch of wrapper code to the WMF SDK,  which he says he took inspiration from this article here at CodeProject. 

I’ve taken a lot of this code and put together a library which you can use to generate WMV videos on the fly, from a variety of different input types 1) A list of image files 2) An array of Bitmap objects and 3) A SQL DataReader containing the raw image bytes.  I’ve put together a small console app that can be used as a utility (MovieMaker.exe) for combining multiple files into a WMV movie, it can be run via the command line, like so:

MovieMaker.exe profile_file_name output_file_name [input filter ex: *.bmp] [input directory]

Here’s an example movie assembled from 5 bitmap files.

The only tricky part of this is creating the appropriate Windows Media Encoder Profile (prx) file.  To do this, you’ll need to download the free Microsoft Windows Media - Windows Media Encoder 9 Series. You’ll also need this installed on any machine running this, since the code uses the WMEncoder to extract the profile information it needs to write the video.

The console app demonstrates how to quickly write images to a WMV file, but you’ll probably find the WMVLib class itself more useful in your applications.  Using the WMVLib class is easy,  the most straight-forward usage is to call SaveVideo with the output file name, profile file name, and list of bitmap files to write to the video:

public static void SaveVideo(
   string fileName,
   string profileFileName,
   string[] files
);

Here’s a snipped of code from the MovieMaker.exe app that shows how to encode a directory of files, given an output and profile file name.

    // Find the input files

    string [] files = Directory.GetFiles(inputDirectory, fileFilter);

 

    // Throw exceptions if we have any missing info

    if(files.Length < 1)

      throw new Exception("No JPEG files found!");

    if(outputFileName.Length == 0)

      throw new ArgumentException("No output file specified!");

    if(prxFileName.Length == 0)

      throw new ArgumentException("No profile file specified!");

 

    // Finally save the video

    WMVService.SaveVideo(outputFileName, prxFileName, files);

 

    Console.WriteLine();

    Console.WriteLine(String.Format("{0} frames written to file {1}", files.Length, outputFileName)); 

The WMVLib’s SaveVideo method is also overloaded for source input types, such as a Bitmap array and DataReader.

Download the following applications to play around with this stuff or integrate it into your own applications. 

You also may want to look into the following resources when digging into this stuff:

Good luck, and if you have questions, topics or code to share relating to windows media, feel free to post them in our Windows Media and GDI+ forum.

-Brendan



Comments

Brendan Tompkins said:

Mahmoud,

There's an overload which you can use to pass the framesPerSecond, a lower fps will make a slower movie. It defaults to 30.

Not sure about watermark and sound, this is only for raw bitmaps currently.
# July 5, 2005 7:24 AM

Mr Cheese said:

Many thanks Brendan for making this available, after a whiile trawling around the internet I found this and it does exactly what I need it to do!

Today is a good internet day :-)
# May 24, 2006 7:48 AM

How to add audio (wave file to wmv file) said:

Hi,

I have a wmv file created by my application. Now I want to audio data stored in a wave file to wmv file. How can I achive this?

Regards,

Hemant.

# October 31, 2006 7:00 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News

MVP
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.