OpenCV 3.3 - gstreamer and libva error

asked 2017-08-22 07:05:41 -0600

simozz gravatar image

updated 2017-08-24 09:43:25 -0600

Hello, I am trying to use H264 codec using the following initialization for VideoWriter:

VideoWriter writer = VideoWriter("file.mp4", CV_FOURCC('H', '2', '6', '4'), 15, Size(300, 500));

and I run into the following exception:

(myProg:20371): GStreamer-CRITICAL **: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed
OpenCV Error: Unspecified error (GStreamer: cannot link elements
) in CvVideoWriter_GStreamer::open, file /path/to/opencv/sources/opencv-3.3/modules/videoio/src/cap_gstreamer.cpp, line 1626
VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps, frameSize, is_color)): raised OpenCV exception:

/path/to/opencv/sources/opencv-3.3/modules/videoio/src/cap_gstreamer.cpp:1626: error: (-2) GStreamer: cannot link elements
 in function CvVideoWriter_GStreamer::open

The video file is neither created.
The command line I use to compile OpenCV libraries is the following:

cmake   -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/gnu.toolchain.cmake \
        -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ \
        -D WITH_JPEG=OFF \
        ../ && make -j 8

As I understand after googling a while, it could be an error related to the filename, but I cannot understand what's really wrong with my code..

EDIT The gstream error seems to be corrected with the following VideoWriter initialization:

VideoWriter writer = VideoWriter("appsrc ! autovideoconvert ! v4l2video1h264enc extra-controls=\"encode,h264_level=10,h264_profile=4,frame_level_rate_control_enable=1,video_bitrate=2000000\" ! h264parse ! rtph264pay config-interval=1 pt=96 ! filesink location=file.mp4", CV_FOURCC('H', '2', '6', '4'), 15, Size(300, 500));

but now I fall into libva error:

error: XDG_RUNTIME_DIR not set in the environment.
libva info: VA-API version 0.36.0
libva info: va_getDriverName() returns -1
libva error: va_getDriverName() failed with unknown libva error,driver_name=(null)

What is the correct way to initialize VideoWriter object for H264 ?

edit retag flag offensive close merge delete



Looks like you are trying to run X-app on a machine without X-server. And there is no actual VAAPI driver installed (try to run vainfo).

mshabunin gravatar imagemshabunin ( 2017-08-22 23:35:49 -0600 )edit

Hello, actually vainfo returns the same error. Looking for vaapi drivers, I can see as these are installed.

simozz gravatar imagesimozz ( 2017-08-23 07:56:20 -0600 )edit

Did you try to run same gstreamer pipeline using the command line?

mshabunin gravatar imagemshabunin ( 2017-08-23 09:24:06 -0600 )edit

No. Sorry but actually I am noob with gstreamer & I don't know how to execute it (complete cmd syntax). What is the cmd I have to try ?

simozz gravatar imagesimozz ( 2017-08-23 11:56:12 -0600 )edit

Command line tool is gst-launch: I'm not a gstreamer expert too, but probably you need to change source bin from appsrc to something else. And looks like your libva installation is not complete, otherwise vainfo would show correct information.

Why don't you try FFMPEG?

mshabunin gravatar imagemshabunin ( 2017-08-25 01:29:33 -0600 )edit

Do you mean to try FFMPEG as an external program ? I tried and it generates the video, but I don't have enough space to store some GB of images to make a video of a bunch of MB. This is not the best solution. Perhaps using libav directly from the code leads to better results..

simozz gravatar imagesimozz ( 2017-08-25 02:33:30 -0600 )edit

No I mean using libav/ffmpeg backend in OpenCV. VideoWriter will use it to encode the video if you compile OpenCV with proper development libraries.

mshabunin gravatar imagemshabunin ( 2017-08-25 03:03:05 -0600 )edit

Good to know it. I am recompiling it. Do you know if I have to use a pipeline as gstreamer ?

simozz gravatar imagesimozz ( 2017-08-25 03:13:19 -0600 )edit