Click here to Skip to main content
15,894,896 members
Articles / Programming Languages / Visual Basic
Article

Capture Activities on Screen in a Movie

Rate me:
Please Sign up or sign in to vote.
4.81/5 (43 votes)
24 Nov 2004CPOL1 min read 611.4K   17K   147   181
An article on how to capture activities on screen as a movie file using Microsoft Windows Media Encoder 9.

Sample Image - CaptureScreenAsVideo.jpg

Introduction

bkniazi asked me a question on experts-exchange.com, about how to capture the activities going on on a machine as a movie. I found the question interesting, so looked for it, but could not find any good solution for it. Though, I found some software which do this kind of work. I found Windows Media Encoder 9 very interesting. While looking into it, I found its SDK and I started looking into it. It is quite interesting. So I made this small sample after looking into the API and seeing some samples.

Prerequisites

You will be needing Windows Media Encoder 9 Series and SDK.

Using the code

Best thing about this API is that it is totally interface based. WMEncoder is the main class. Set its parameters and start the encoder to start encoding. Stop it to save in file.

VB
Encoder = New WMEncoder

Create a Source Group named SG_1 in Encoder's Collection.

VB
Dim SrcGrp As IWMEncSourceGroup2
Dim SrcGrpColl As IWMEncSourceGroupCollection
SrcGrpColl = Encoder.SourceGroupCollection
SrcGrp = SrcGrpColl.Add("SG_1")

Specify the input types for Video and Audio. Specify the screen capture plug-in to set up the video source.

VB
SrcVid.SetInput("ScreenCap://ScreenCapture1")
SrcAud.SetInput("Device://Default_Audio_Device")

Find the profile and set Encoder's group profile from all the profiles available.

VB
ProColl = Encoder.ProfileCollection
lLength = ProColl.Count
For i = 0 To lLength - 1
    Pro = ProColl.Item(i)
    If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then
        SrcGrp.Profile = Pro
        Exit For
    End If
Next

Specify header information of the Encoder. I.e., description or attributes.

VB
Dim Descr As IWMEncDisplayInfo
Descr = Encoder.DisplayInfo
Descr.Author = "Armoghan Asif"
Descr.Copyright = "Copyright information"
Descr.Description = "Text description of encoded content"
Descr.Rating = "Rating information"
Descr.Title = "Title of encoded content"

' Add an attribute to the collection.
Dim Attr As IWMEncAttributes
Attr = Encoder.Attributes
Attr.Add("URL", "IP address")

You may do some cropping of the Encoder if required.

VB
' Crop 2 pixels from each edge of the video image.
SrcVid.CroppingBottomMargin = 2
SrcVid.CroppingTopMargin = 2
SrcVid.CroppingLeftMargin = 2
SrcVid.CroppingRightMargin = 2

Specify the Local File as output. You may also set additional fields of the file, like size of file, seconds of file etc.

VB
Dim File As IWMEncFile
File = Encoder.File
File.LocalFileName = "C:\OutputFile.avi"

Start the encoding process:

VB
' Start the encoding process.
Encoder.Start()

Stop the encoding process:

VB
' Stop the encoding process.
Encoder.Stop()

Points of Interest

No fancy things are placed in the demo. It just illustrates the screen capture. It can be easily added in any existing application. Nothing else :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Windows Media Encoder Controls Pin
Armoghan Asif11-Jul-05 23:55
Armoghan Asif11-Jul-05 23:55 
GeneralSpeed up my screen capture server application Pin
S!D6-Jul-05 21:39
S!D6-Jul-05 21:39 
GeneralRe: Speed up my screen capture server application Pin
Armoghan Asif7-Jul-05 6:12
Armoghan Asif7-Jul-05 6:12 
GeneralRe: Speed up my screen capture server application Pin
Animous13-Jul-05 2:09
sussAnimous13-Jul-05 2:09 
GeneralRe: Speed up my screen capture server application Pin
Armoghan Asif13-Jul-05 3:34
Armoghan Asif13-Jul-05 3:34 
GeneralRemote screen capturing Pin
S!D30-Jun-05 22:50
S!D30-Jun-05 22:50 
GeneralRe: Remote screen capturing Pin
Armoghan Asif30-Jun-05 23:15
Armoghan Asif30-Jun-05 23:15 
Generalerror creating the profile Pin
Member 161799910-Jun-05 5:43
Member 161799910-Jun-05 5:43 
Hi guys,

Basically i am creating an application which captures both screen movements
and the audio when i speak. the screen capture i have done it. Now when i
tried to add the audio, i am getting the error "the request is invalid in the
current state" when i set the set_AudioCodec() with "Windows Media Audio 9"
codec.

i am adding the above codec to the same audience.

why i am getting the error?

the code for creating the profile is -----------

WMEncProfile2 Pro = new WMEncProfile2();

// Verify profile settings immediately as they are set.
Pro.ValidateMode = true;

// Provide a name and description.
Pro.ProfileName = "Vellum Screen Capture Profile";
Pro.ProfileDescription = "A screen capture profile.";

// Specify video content.
Pro.ContentType = 16;

// Specify constant bit rate (CBR) mode.
Pro.set_VBRMode(WMENC_SOURCE_TYPE.WMENC_VIDEO, 0,
WMENC_PROFILE_VBR_MODE.WMENC_PVM_NONE);

// Add audiences for 400 Kbps.
Pro.AddAudience((int) CaptureParam.BitRate);

object vVidCodecName;
int lVid4cc = -1;
int x = 0;
bool success = false;
string codec = "Windows Media Video 9 Screen";

for ( x = 0; x < Pro.VideoCodecCount && !success; x++ )
{
lVid4cc = Pro.EnumVideoCodec(x, out vVidCodecName);
if ( vVidCodecName.ToString().Equals(codec) )
{
success = true;
x--;
}
//Console.WriteLine( vVidCodecName.ToString() );
}


IWMEncAudienceObj Audnc = Pro.get_Audience(0);
// The Windows Media 9 Screen codec is used by default, but you can change
// it as follows. Be sure to make this change for each audience.
Audnc.set_VideoCodec(0, x);

// Make the video output size match the input size by setting
// the height and width to 0.
Audnc.set_VideoHeight(0, CaptureParam.CompressHeight);
Audnc.set_VideoWidth(0, CaptureParam.CompressWidth);

// Change the buffer size to 5 seconds. By default, the end user's
// default setting is used.
Audnc.set_VideoBufferSize(0, -1);//1000
Audnc.set_VideoImageSharpness( 0, 100 );
Audnc.set_VideoFPS( 0, CaptureParam.FramesPerSec * 1000 );

if(CaptureParam.IsAudioAllowed && CaptureParam.AudioDevice != null)
{
// set the audio device settings
string audioCodec = "Windows Media Audio 9";
object audioCodecName;
for(int i = 0; i < Pro.AudioCodecCount; i++)
{
Pro.EnumAudioCodec(i, out audioCodecName);
if(audioCodecName.ToString().Equals(audioCodec))
{
Audnc.set_AudioCodec(0, i);
Audnc.SetAudioConfig(0, 1, 22050, 2500, 16);
break;
}
}
}
// Validate the settings to make sure the profile has no errors.
Pro.Validate();

----------------
GeneralRe: error creating the profile Pin
Armoghan Asif12-Jun-05 19:55
Armoghan Asif12-Jun-05 19:55 
Generalvideo quality Pin
jfaulkner1-Jun-05 6:37
jfaulkner1-Jun-05 6:37 
GeneralRe: video quality Pin
jfaulkner1-Jun-05 18:13
jfaulkner1-Jun-05 18:13 
GeneralRe: video quality Pin
Armoghan Asif1-Jun-05 19:45
Armoghan Asif1-Jun-05 19:45 
GeneralRe: video quality Pin
jfaulkner1-Jun-05 21:28
jfaulkner1-Jun-05 21:28 
GeneralRe: video quality Pin
Armoghan Asif1-Jun-05 19:41
Armoghan Asif1-Jun-05 19:41 
GeneralCan't capture audio Pin
jfaulkner21-May-05 17:45
jfaulkner21-May-05 17:45 
GeneralRe: Can't capture audio Pin
Armoghan Asif23-May-05 23:15
Armoghan Asif23-May-05 23:15 
QuestionHow can I read the screen? Pin
gregory_may12-May-05 13:28
gregory_may12-May-05 13:28 
AnswerRe: How can I read the screen? Pin
Armoghan Asif16-May-05 6:52
Armoghan Asif16-May-05 6:52 
AnswerRe: How can I read the screen? Pin
Uraan Software Solutions10-Oct-05 22:51
Uraan Software Solutions10-Oct-05 22:51 
Generallive AVI file encoding Pin
Member 50789112-Apr-05 22:04
Member 50789112-Apr-05 22:04 
GeneralRe: live AVI file encoding Pin
Armoghan Asif13-Apr-05 5:04
Armoghan Asif13-Apr-05 5:04 
GeneralLive Stream Capture Stand-Alone Pin
Knut Ole17-Mar-05 3:40
sussKnut Ole17-Mar-05 3:40 
GeneralRe: Live Stream Capture Stand-Alone Pin
Armoghan Asif17-Mar-05 17:27
Armoghan Asif17-Mar-05 17:27 
QuestionHow do you capture part of the screen in vb.net :P Pin
Anonymous27-Feb-05 9:20
Anonymous27-Feb-05 9:20 
AnswerRe: How do you capture part of the screen in vb.net :P Pin
Armoghan Asif27-Feb-05 22:05
Armoghan Asif27-Feb-05 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.