Module for taking still images from RTSP video streams and USB webcams (V4L2), using FFmpeg.
On Linux, install the packages ffmpeg
and v4l-utils
:
$ sudo apt-get install ffmpeg v4l-utils
List connected USB cameras:
$ v4l2-ctl --list-devices
UVC Camera (046d:0825) (usb-0000:00:1d.7-1):
/dev/video0
/dev/video1
/dev/media0
The following example captures an image from an attached USB webcam at
/dev/video0
and adds a timestamp in ISO 8601 format to it using
GraphicsMagick:
character(len=*), parameter :: IMAGE_PATH = '/tmp/image.jpg'
integer :: rc
type(camera_type) :: camera
type(gm_text_box_type) :: text_box
camera = camera_type(input = '/dev/video0', &
device = CAMERA_DEVICE_V4L2, &
width = 1280, &
height = 720)
rc = dm_camera_capture(camera, IMAGE_PATH)
if (dm_is_error(rc)) call dm_error_out(rc)
text_box = gm_text_box_type(font='DroidSansMono', font_size=16)
rc = dm_gm_add_text_box(IMAGE_PATH, text=dm_time_now(), text_box=text_box)
if (dm_is_error(rc)) call dm_error_out(rc)
The camera must support the resolution of 1280×720 in this case. If no resolution is specified, the camera default is used. Run ffmpeg(1) to list the supported output dimensions:
$ ffmpeg -f v4l2 -list_formats all -i /dev/video0
RTSP streams are always captured in the stream resolution:
camera = camera_type(input='rtsp://10.10.10.15:8554/camera1', device=CAMERA_DEVICE_RTSP)
The attribute input
must be set to the stream URL and may include user
name and password.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | parameter | :: | CAMERA_DEVICE_NONE | = | 0 |
No device selected. |
integer, | public, | parameter | :: | CAMERA_DEVICE_RTSP | = | 1 |
RTSP stream. |
integer, | public, | parameter | :: | CAMERA_DEVICE_V4L2 | = | 2 |
USB webcam via Video4Linux2. |
integer, | public, | parameter | :: | CAMERA_DEVICE_LAST | = | 2 |
Never use this. |
integer, | public, | parameter | :: | CAMERA_DEVICE_NAME_LEN | = | 4 | |
character(len=*), | public, | parameter | :: | CAMERA_DEVICE_NAMES(CAMERA_DEVICE_NONE:CAMERA_DEVICE_LAST) | = | [character(len=CAMERA_DEVICE_NAME_LEN)::'none', 'rtsp', 'v4l2'] |
Camera device names. |
Camera settings type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=FILE_PATH_LEN), | public | :: | input | = | ' ' |
Input device path ( |
|
integer, | public | :: | device | = | CAMERA_DEVICE_NONE |
Input device. |
|
integer, | public | :: | width | = | 0 |
Camera stream width in pixels (optional). |
|
integer, | public | :: | height | = | 0 |
Camera stream height in pixels (optional). |
Captures a single frame from a V4L2 device or RTSP stream with
FFmpeg, and optionally adds a timestamp with GraphicsMagick. If the
input is an RTSP stream, the URL must start with rtsp://
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(camera_type), | intent(in) | :: | camera |
Camera type. |
||
character(len=*), | intent(in) | :: | output |
Output file. |
||
character(len=:), | intent(out), | optional, | allocatable | :: | command |
Executed command. |
Returns device enumerator from name. On error, the result is
CAMERA_DEVICE_NONE
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | name |
Device name. |
Returns .true.
if device enumerator is valid. The device
CAMERA_DEVICE_NONE
is invalid.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | device |
Camera device enumerator. |