Looking for a fun software development project? Want to build your own YouTube? Ya me either, but how the heck do you convert videos to flash or Silverlight? It’s not that hard actually as long as you use FFmpeg and some c# code. FFmpeg is a free video and audio converter. What I like about Silverlight is it will just take the WMV format, and stream it for you. So if your video is already in WMV, your are done. If not, you can convert i to WMV, including the HD formats. In flash, you will have to write some extra code to get it converted. Check out the HD Halo Trailer in Silverlight by the way.
The first thing you will want to do is download FFmpeg. There will be a few different files in there. Make sure you put FFMPEG.exe and FLVTOOL.exe in your application directory. Next you will want set the correct permissions on your web server.
The code below is fairly basic, but using other FFmpeg commands you can easily create thumbnails from the video, and save them to a location on your web server. This code for example would create some image thumbnails for you quit easily.
startInfo.Arguments = string.Format(“-i ”{0}” -f image2 -ss 1 -vframes 1
-s 80×60 -an ”{1}””, srcURL, destURL);
If you are using Silverlight, you can just keep the file in wmv, or convert it to wmv, and just run the command above against the video file to capture the image screenshot.
Anyway here is the sample code for you to try out to convert the video file to flash. Silverlight is cool because you don’t have to do that and it can just play the wmv in a streaming format automagically.
Sample Code
private void ConvertVideo(string srcURL, string destURL) { string ffmpegURL = “~/project/tools/ffmpeg.exe”; DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName (Server.MapPath(ffmpegURL))); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = Server.MapPath(ffmpegURL); startInfo.Arguments = string.Format(“-i ”{0}” -acodec mp3 -ab 64k -ac 2 -ar 44100 -f flv -deinterlace -nr 500 -croptop 4 -cropbottom 8 -cropleft 8 -cropright 8 -s 640x480 -aspect 4:3 -r 25 -b 650k -me_range 25 -i_qfactor 0.71 -g 500 ”{1}”", srcURL, destURL); startInfo.WorkingDirectory = directoryInfo.FullName; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardError = true; using (Process process = new Process()) { process.StartInfo = startInfo; try { process.Start(); StreamReader standardOutput = process.StandardOutput; StreamWriter standardInput = process.StandardInput; StreamReader standardError = process.StandardError; process.WaitForExit(); lblError.Text = standardError.ReadToEnd(); lblOutput.Text = standardOutput.ReadToEnd(); } catch (Exception ex) { Response.Write(ex.ToString()); } } }
Recent Comments