/// <summary> /// Saves the video. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="profileFileName">Name of the profile file.</param> /// <param name="framesPerSecond">Frames per second.</param> /// <param name="bitmaps">Bitmaps.</param> public WmvEncoder(string profileFileName, ulong framesPerSecond) { try { WMEncoder encoder = new WMEncoder(); IWMEncProfile2 profile = WMEncProfile.LoadEncodingProfile(encoder, profileFileName); WMEncProfile.ReleaseEncoder(ref encoder); writer = new WmvWriter(); writer.SetProfile((IWMProfile)profile.SaveToIWMProfile()); viHeader = new VideoInfoHeader(); bool bret = writer.FindVideoInputFormat(0, MediaSubTypes.WMMEDIASUBTYPE_RGB24, ref viHeader, false); if (!bret) { throw new Exception("FindVideoInputFormat error"); } ; frame = new Bitmap((int)viHeader.bmiHeader.biWidth, (int)viHeader.bmiHeader.biHeight, PixelFormat.Format24bppRgb); fps = framesPerSecond; } catch (Exception) { // error handle throw; } }
/// <summary> /// Saves the video. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="profileFileName">Name of the profile file.</param> /// <param name="files">Files.</param> public static void SaveVideo(string fileName, string profileFileName, ulong framesPerSecond, string [] files) { WMEncoder encoder = new WMEncoder(); IWMEncProfile2 profile = WMEncProfile.LoadEncodingProfile(encoder, profileFileName); WMEncProfile.ReleaseEncoder(ref encoder); using (WmvWriter writer = new WmvWriter()) { writer.Initialize((IWMProfile)profile.SaveToIWMProfile(), fileName); VideoInfoHeader viHeader = new VideoInfoHeader(); writer.FindVideoInputFormat(0, MediaSubTypes.WMMEDIASUBTYPE_RGB24, ref viHeader, false); writer.Start(); INSSBuffer sample = null; int bmcount = 0; Bitmap frame = new Bitmap((int)viHeader.bmiHeader.biWidth, (int)viHeader.bmiHeader.biHeight, PixelFormat.Format24bppRgb);; ulong fps = framesPerSecond; for (int i = 0; i < files.Length; i++) { if (files[i] != null) { Bitmap bmp = new Bitmap(files[i]); using (Graphics g = Graphics.FromImage(frame)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(bmp, 0, 0, frame.Width, frame.Height); Console.Out.Write("."); } // Now render to the movie try { ulong time = ((ulong)bmcount++ *TimeSpan.TicksPerSecond) / fps; sample = writer.GetSampleFromBitmap(frame); writer.Writer.WriteSample(0, time, (uint)WMT_STREAM_SAMPLE_TYPE.WM_SF_CLEANPOINT, sample); } finally { Marshal.ReleaseComObject(sample); sample = null; bmp = null; GC.Collect(); } } } } }
/// <summary> /// Saves the video. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="profileFileName">Name of the profile file.</param> /// <param name="dr">Dr.</param> public static void SaveVideo(string fileName, string profileFileName, ulong framesPerSecond, SqlDataReader dr) { WMEncoder encoder = new WMEncoder(); IWMEncProfile2 profile = WMEncProfile.LoadEncodingProfile(encoder, profileFileName); WMEncProfile.ReleaseEncoder(ref encoder); using (WmvWriter writer = new WmvWriter()) { writer.Initialize((IWMProfile)profile.SaveToIWMProfile(), fileName); VideoInfoHeader viHeader = new VideoInfoHeader(); writer.FindVideoInputFormat(0, MediaSubTypes.WMMEDIASUBTYPE_RGB24, ref viHeader, false); writer.Start(); INSSBuffer sample = null; int bmcount = 0; Bitmap c = new Bitmap((int)viHeader.bmiHeader.biWidth, (int)viHeader.bmiHeader.biHeight, PixelFormat.Format24bppRgb);; ulong fps = viHeader.AvgTimePerFrame / 100000; while (dr.Read()) { Bitmap b = Image.FromStream(new MemoryStream(dr.GetValue(1) as byte[])) as Bitmap; using (Graphics g = Graphics.FromImage(c)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(b, 0, 0, c.Width, c.Height); } try { ulong time = ((ulong)bmcount++ *TimeSpan.TicksPerSecond) / fps; sample = writer.GetSampleFromBitmap(c); writer.Writer.WriteSample(0, time, (uint)WMT_STREAM_SAMPLE_TYPE.WM_SF_CLEANPOINT, sample); } finally { Marshal.ReleaseComObject(sample); sample = null; b = null; GC.Collect(); } } } }
/** * Constructor. */ public Encoder() { this.enableRecording = false; this.chopNumber = 0; this.chopLength = -1; this.choppingTimer = new System.Timers.Timer(); this.choppingTimer.Elapsed += this.OnRecordTimeElapsed; this.archiveState = WMENC_ARCHIVE_STATE.WMENC_ARCHIVE_STOPPED; this.stateChangeWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty); this.archiveStateChangeWaitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset, string.Empty); // Audio and video sources this.audioSource = null; this.videoSource = null; // Windows Media Encoder this.wmEncoder = new WMEncoderClass(); this.sourceGroup = (IWMEncSourceGroup2)this.wmEncoder.SourceGroupCollection.Add("SG_1"); this.wmEncoderProfile = new WMEncProfile2(); // For now, we only listen these events this.wmEncoder.OnStateChange += this.OnStateChange; this.wmEncoder.OnError += this.OnError; this.wmEncoder.OnArchiveStateChange += this.OnArchiveStateChange; // Set recording props. this.wmEncoder.EnableAutoArchive = false; this.wmEncoder.AutoIndex = false; // Set default profile values this.wmEncoderProfile.ValidateMode = true; this.wmEncoderProfile.ProfileName = "Windows Media Encoder Profile"; this.wmEncoderProfile.ContentType = 17; // Audio + Video this.wmEncoderAudience = this.wmEncoderProfile.AddAudience(100000); // Initial bitrate = 100 kbps, will change automatically // when user sets video and audio params. }