Video clips built on the fly by passing bitmap handles, BMP or JPEG files
A video clip can be created on the fly by directly passing either memory bitmap handles or the file names of BMP or JPEG files.
This feature is activated when VideoSource is set to vs_JPEGsOrBitmaps.
The frames are passed by invoking SendImageToVideoFromBitmaps, which receives as parameter:
- either the path to one of the BMP or JPEG files (ImageFilePath parameter),
- or a bitmap handle (BitmapHandle parameter, see remark b below).
Building a video clip by passing images when they are received, in real time
This applies e.g. when the images are received from another live source at 15 fps.
- Set VideoSource = vs_JPEGsOrBitmaps.
- Set FrameRate to the actual frame rate.
- Invoke SendImageToVideoFromBitmaps once beforehand so that the component can learn the video format (if this step is omitted, the next step fails).
- Invoke StartPreview or StartRecording.
- Then periodically invoke SendImageToVideoFromBitmaps or SendImageToVideoFromBitmaps2 as soon as new frames are available, to pass them to the component.
Building a video clip from an existing set of images
This applies e.g. to a set of JPEG files located in a folder.
When StartPreview or StartRecording is invoked, the OnVideoFromBitmapsNextFrameNeeded event occurs periodically (depending on the FrameRate property), requesting the next image that will be used as a video frame to build the video stream.
- Set VideoSource = vs_JPEGsOrBitmaps.
- Set FrameRate to the desired frame rate.
- Create the OnVideoFromBitmapsNextFrameNeeded event, and put in it the code required to pass the bitmap handles or the image files.
- From the OnVideoFromBitmapsNextFrameNeeded event, invoke SendImageToVideoFromBitmaps or SendImageToVideoFromBitmaps2 and pass as parameter either the file name or the bitmap handle of the next frame.
- Invoke StartPreview or StartRecording.
Sample code is available in the MainDemo project included in the package.
Remarks about SendImageToVideoFromBitmaps
a) BitmapHandle and ImageFilePath are mutually exclusive:
- when passing a file path to ImageFilePath, pass 0 to the BitmapHandle parameter,
- when passing a bitmap handle to the BitmapHandle parameter, pass an empty string to the ImageFilePath parameter.
b) When passing a BitmapHandle, enable CanFreeBitmapHandle if TVideoGrabber must free the bitmap when it is no longer needed, or disable CanFreeBitmapHandle to reuse the bitmap later.
c) All the images passed to SendImageToVideoFromBitmaps must have the same format.
Other remarks:
- If needed, the on the fly compression can be applied before invoking StartRecording.
- The FirstSample parameter of the OnVideoFromBitmapsNextFrameNeeded event is true for the first frame, and false for the others. It can be tested e.g. to restart from the first bitmap.
- Set EndOfData to true to notify the end of stream. This is equivalent to invoking StopRecording or StopPreview.
- When passing a bitmap handle, do not release it. If the bitmap handle is picked up from a TBitmap object (by passing TBitmap.Handle), release the handle before freeing the TBitmap.
E.g.:
procedure TForm1.VideoGrabberVideoFromBitmapsNextFrameNeeded(Sender: TObject; FirstSample: Boolean);
var
Bitmap: TBitmap;
BitmapHandle: HBITMAP;
begin
Bitmap:= TBitmap.Create;
Bitmap.Assign (Image1.Picture);
BitmapHandle:= Bitmap.Handle;
VideoGrabber.SendImageToVideoFromBitmaps ('', BitmapHandle, true, false);
Bitmap.ReleaseHandle;
Bitmap.Free;
end;
Remark about the frame rate
When recording without audio to an AVI file, the recording can run as fast as possible: set a very high frame rate (e.g. FrameRate = 200), then set the final frame rate from the OnAVIDurationUpdated event when the recording ends.