ROS2 package to play and save videos
Use the installation script:
./install_description.sh
This will install all dependencies, including the FFMPEG.
For use the GPU for hardware acceleration during the encoding step, you will need a compatible GPU (e.g. NVIDIA GeForce 20-series and newer) and the correct drivers. For NVIDIA cards, install relevant drivers and CUDA using terminal:
sudo apt-get install nvidia-driver-VERSION
sudo apt-get install cuda-toolkit-VERSION
To check that FFMPEG now recognizes cuda
as a hardware acceleration option, run the following line in terminal and check that the output list contains cuda
:
ffmpeg -hwaccels
-
image_topic
: ROS topic to listen to for images to save [default: camera/image] -
output_fps
: playback FPS of the output video (not affected by FPS of incoming stream or ROS timestamps) [default: 60.0] -
record_every_nth_frame
: save only every n-th frame that is received by the ROS subscriber [default: 1] -
burn_timestamp
: burn the current timestamp and frame number on each frame [default: False] -
quit_after_s_seconds
: close this node after a given number of seconds (disabled if less than 0) [default: -1] -
codec
: codec to use for encoding the output video; common codecs (and their relative performance and resulting file size) include:raw
: each frame is saved as independent RAW; fastest, largestmjpeg
(default): each frame is saved as independent JPEG images with no timewise compression; fast, largeh264
: a very commonly used video compression standard; slow, smallh265
orhevc
: a commonly used high-efficiency video compression standard; average, small- Run:
ffmpeg -encoders
to check all codecs available for encoding on your computer
Note: to use GPU hardware acceleration, you must specify a compatible codec/encoder. For
-hwaccel cuda
, this is generally denoted with a prefixnvenc_
. For example, to useh265
compression with a CUDA-enabled GPU, specifynvenc_hevc
as your codec. -
quality
(only forh264
/h265
codecs): set quality factor for some compression algorithms. For example, forh265
compression, put18
for this parameter to use constant QP mode and set the compression QP to 18. Note that compression quality decreases with QP: qp=0 is lossless, qp=12-18 maintains high quality (visually lossless), qp=23-28 is most common/average compression, up to a max of qp=51. We use a default of qp=24, but for most other codecs, FFMPEG will default to qp=24. -
encoder_args
: any additional encoder options; run:ffmpeg -h encoder=CODEC
to check all available options for your codec of choiceNote: this MUST be provided in the form of a list
Tip: use this to control the compression quality for codecs that do not support constant QP (see above notes on
quality
. -
save_as_single_video
(only forburst_video_saver
): compile all burst save events as a single video stream, otherwise each burst record command will save their own partial video [default: True]
In order for the saved MPEG video file to be decoded and read properly in the future,
the container must include a moov atom that acts as a sort of table of contents.
This is generally added at the end of the file, so exiting an active stream
prematurely can result in an incomplete container. This can be remedied by
copying the atom from a video with similar structure. This can be achieved
using a library called untrunc
:
sudo snap install --edge untrunc-anthwlock
untrunc-anthwlock -s GOOD_VIDEO BROKEN_VIDEO
DEPRECATED: new examples are in progress
To run the basic example, type:
ros2 launch video_io example.launch.py
This will prompt you to pick an index for one of the examples. The examples provided are:
- Basic playing/saving of video
- Saving a (temporally) downsampled video, recording only ever nth frame
- Play a video at lower resolution (downsampled pixels) and record the video
- Play a video, and save the video only after receiving a command. This command contains a burst duration variable.
You can experiment with different parameters in the ./config/example.yaml file. Each example is contained in it's own namespace, as signified with it's YAML indent.
Currently this repository is only accessible for maimon lab members. We plan to release this repository in the future under the LGPLv3 license.