使用 dspack2.0
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DSPack, DSUtil, DirectShow, ComCtrls, ExtCtrls;
type
TMainForm = class(TForm)
CaptureGraph: TFilterGraph;
VideoWindow: TVideoWindow;
VideoCapFilters: TListBox;
VideoSourceFilter: TFilter;
StartButton: TButton;
CapFileButton: TButton;
SaveDialog: TSaveDialog;
StopButton: TButton;
VideoFormats: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure VideoCapFiltersClick(Sender: TObject);
procedure StartButtonClick(Sender: TObject);
procedure CapFileButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
CapEnum: TSysDevEnum;
VideoMediaTypes: TEnumMediaType;
CapFile: WideString = 'c:/capture.avi';
implementation
{$R *.dfm}
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
begin
CapEnum := TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
VideoCapFilters.Items.Add(CapEnum.Filters[0].FriendlyName);
CapEnum.SelectGUIDCategory(CLSID_AudioInputDeviceCategory);
VideoMediaTypes := TEnumMediaType.Create;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
CapEnum.Free;
VideoMediaTypes.Free;
end;
// Select the video Source
procedure TMainForm.VideoCapFiltersClick(Sender: TObject);
var
PinList: TPinList;
begin
CapEnum.SelectGUIDCategory(CLSID_VideoInputDeviceCategory);
begin
VideoSourceFilter.BaseFilter.Moniker := CapEnum.GetMoniker(0);
VideoSourceFilter.FilterGraph := CaptureGraph;
CaptureGraph.Active := true;
PinList := TPinList.Create(VideoSourceFilter as IBaseFilter);
VideoFormats.Clear;
VideoMediaTypes.Assign(PinList.First);
VideoFormats.Items.Add(VideoMediaTypes.MediaDescription[0]);
CaptureGraph.Active := false;
PinList.Free;
StartButton.Enabled := true;
end;
end;
// Start Capture
procedure TMainForm.StartButtonClick(Sender: TObject);
var
multiplexer: IBaseFilter;
Writer: IFileSinkFilter;
PinList: TPinList;
begin
VideoCapFiltersClick(Sender);
// Activate the filter graph, at this stage the source filters are added to the graph
CaptureGraph.Active := true;
// configure output Video media type
if VideoSourceFilter.FilterGraph <> nil then
begin
PinList := TPinList.Create(VideoSourceFilter as IBaseFilter);
if VideoFormats.ItemIndex <> -1 then
with (PinList.First as IAMStreamConfig)do
SetFormat(VideoMediaTypes.Items[VideoFormats.ItemIndex].AMMediaType^);
PinList.Free;
end;
// now render streams
with CaptureGraph as IcaptureGraphBuilder2do
begin
// set the output filename
SetOutputFileName(MEDIASUBTYPE_Avi, PWideChar(CapFile), multiplexer, Writer);
// Connect Video preview (VideoWindow)
if VideoSourceFilter.BaseFilter.DataLength > 0 then
RenderStream(@PIN_CATEGORY_PREVIEW, nil, VideoSourceFilter as IBaseFilter,
nil , VideoWindow as IBaseFilter);
// Connect Video capture streams
if VideoSourceFilter.FilterGraph <> nil then
RenderStream(@PIN_CATEGORY_CAPTURE, nil, VideoSourceFilter as IBaseFilter,
nil, multiplexer as IBaseFilter);
// Connect Audio capture streams
end;
CaptureGraph.Play;
StopButton.Enabled := true;
StartButton.Enabled := false;
VideoFormats.Enabled := false;
VideoCapFilters.Enabled := false;
end;
// Select the Ouput file
procedure TMainForm.CapFileButtonClick(Sender: TObject);
begin
if SaveDialog.Execute then
CapFile := SaveDialog.FileName;
end;
// Stop Capture
procedure TMainForm.StopButtonClick(Sender: TObject);
begin
StopButton.Enabled := false;
StartButton.Enabled := true;
CaptureGraph.Stop;
CaptureGraph.Active := False;
VideoFormats.Enabled := true;
VideoCapFilters.Enabled := true;
end;
end.