Example:
<launch>
<node name="h264_encoder" pkg="image_h264_encoder" type="image_h264_encoder_node" output="log">
<param name="fps" value="30" />
<param name="bitrate" value="10000" />
<param name="targetLocation" value="./h264Encoder_front.mp4" />
<param name="publishScaledImage" value="true"/>
<param name="imageScaleFactor" value="0.15"/>
<remap from="~image" to="/front_camera" />
</node>
</launch>
Make sure that your OpenCV build supports GStreamer.
Use python to querry the build configuration:
import cv2
print(cv2.getBuildInformation())
Check for GStreamer support:
GStreamer:
base: YES (ver 1.8.3)
video: YES (ver 1.8.3)
app: YES (ver 1.8.3)
riff: YES (ver 1.8.3)
pbutils: YES (ver 1.8.3)
To test if the encoder is installed properly:
gst-launch-1.0 filesrc location=./sample.mp4 ! qtdemux ! h264parse ! avdec_h264 ! nvh264enc rc-mode=2 bitrate=10000 ! h264parse ! mp4mux ! filesink location=./encoded.mp4
-
Download the Vision SDK from https://developer.nvidia.com/nvidia-video-codec-sdk/download
-
check if current nvidia driver is sufficent with
nvidia-smi
-
Install the SDK:
unzip Video_Codec_SDK.zip cd Video_Codec_SDK sudo cp include/* /usr/local/cuda/include sudo cp Lib/linux/stubs/x86_64/* /usr/local/cuda/lib64/stubs
-
Get GStreamer Bad plugins
sudo apt-get install gstreamer1.0-plugins-bad git clone https://github.com/GStreamer/gst-plugins-bad.git cd gst-plugins-bad git checkout $(gst-launch-1.0 --version | grep version | tr -s ' ' '\n' | tail -1) ./autogen.sh --disable-gtk-doc --noconfigure export NVENCODE_CFLAGS="-I/usr/local/cuda/include" ./configure --with-cuda-prefix="/usr/local/cuda"
-
Check output to confirm that nvenc, nvdec plugins is going to be build
-
Build and install nvenc
cd sys/nvenc make sudo make install cd ../..
-
Build and install nvdec
cd sys/nvdec make sudo make install cd ../..
-
Add GST_PLUGIN_PATH to bashrc
echo "export GST_PLUGIN_PATH=$GST_PLUGIN_PATH:/usr/local/lib/gstreamer-1.0/" >> ~/.bashrc source ~/.bashrc
In some cases catkin searches for opencv
, a symbolic link fixes the issue:
sudo ln -s /usr/include/opencv4/ /usr/include/opencv
If OpenCV is still not found, try to set the environment variable OpenCV_DIR
.
OpenCV changed the behavior of the videwriter class.
To enable support for OpenCV 4 adjust the OPENCVVERSION
to 4 in the encoder source file.