Just moozing

Before you can check your notes, you must make them…

gstreamer sources

with 3 comments

I am working with gstreamer these days. This blog post is about sources.

gstreamer-logoIn my project, I need two video sources: the USB camera and the desktop


USB camera

On modern Linux boxes, we use v4l2 (video for linux 2). This is the same system that is for TV card, frame grabbers and the other video devices, so USB cameras should be exactly the same as the other devices.

From an example I found

$ gst-launch v4l2src ! video/x-raw-yuv,width=320,height=240,framerate=\(fraction\)5/1 ! ffmpegcolorspace ! xvimagesink
ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Could not negotiate format


The “Could not negotiate format” error means that the format requested (ie. 320×240 5 fps) is not supported by the camera.

$ gst-launch v4l2src ! video/x-raw-yuv,width=320,height=240,framerate=\(fraction\)5/1 ! ffmpegcolorspace ! xvimagesink --gst-debug-level=3
...a lot :-)...


It includes a long line with info like “video/x-raw-yuv, format=(fourcc)YUY2, width=(int)960, height=(int)540, interlaced=(boolean)false, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 10/1 };”.

On my USB camera, I have video/x-raw-yuv, image/jpeg and video/x-raw-rgb. This is different data format that the camera supports. This corresponds to what v4l2 reports.

$ v4l2-ctl --list-formats
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUV 4:2:2 (YUYV)

Index : 1
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : MJPEG


I am not sure what happened to the rgb modes.

So in conclusion, I asked for a frame rate that wasn’t permitted. Changing it to 15, fixed my issue.

$ gst-launch v4l2src ! video/x-raw-yuv,width=320,height=240,framerate=\(fraction\)15/1 ! ffmpegcolorspace ! xvimagesink



Recording the desktop

gstreamer support pulling pixels from the screen. This makes screen recording trivial. The source you need is xvimagesrc.

gst-launch -v ximagesrc startx=100 endx=200 starty=100 endy=200 ! video/x-raw-rgb,framerate=5/1 ! ffmpegcolorspace ! xvimagesink


This pipeline will open a window showing the content of the square (100,100)->(200,200). If you don’t specific a size, it will collect from the entire screen, and open a window showing that. This will fail, since the resulting window will always be bigger than the screen.

ximagesrc can do another need trick, and that is to record from a specific windows.

$ xwininfo -tree -root -all | grep Keynote
        0x5849085 "▶ The Lifecycle of a Revolution (Keynote) - YouTube - Iceweasel": ("Navigator" "Iceweasel")  648x716+1+22  +718+26
$ WINDOW_ID="0x5849085"
$ gst-launch -v ximagesrc xid=$WINDOW_ID ! video/x-raw-rgb,framerate=5/1 ! ffmpegcolorspace ! xvimagesink

The “use-damage=false” is needed, otherwise the pipe will stall until something changes in the window.

BTW, “The Lifecycle of a Revolution (Keynote)” is the keynote speech from blackhat 2015.




Written by moozing

August 15, 2015 at 12:00

Posted in Tech

Tagged with ,

3 Responses

Subscribe to comments with RSS.

  1. […] Continuing my gstreamer project I need two sinks, one to show stuff on the screen, i.e. xvimagesink, and one to save it to file, i.e. filesink. […]

  2. […] like video sources and sinks, gstreamer has audio sources and […]

  3. […] storing to file and splitting streams are discussed in my previous posts here and […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: