Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Perl wrapper for v4l2-ctl

by stevieb (Abbot)
on May 14, 2017 at 19:54 UTC ( #1190276=perlquestion: print w/replies, xml ) Need Help??
stevieb has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

I'm considering writing a Perl XS wrapper for v4l2-ctl (v4l2-utils). Essentially, this is a command line application with an API that allows one to manipulate and use an attached video/picture camera.

The main purpose for this project is so that it will augment my Raspberry Pi work, although it'd work on any computing-type device that has an onboard or USB video camera. I am going to create a device with a 5" touchscreen (Tk) that allows one to mount it into a vehicle, and record my road trips. I know devices like this exist, but the cheap ones I've tried don't work barely at all, and I'm not going $600 CDN in for a Go Pro.

Here's a link that shows some example C API uses.

My question is, does anyone with more video/photography capturing experience with Perl know if there's an equivalent already written for Perl? My limited searching on the CPAN shows a couple of things, but either the docs lack, or they generally look sketchy.

All input welcome. If there's nothing already that will allow the use of all camera options etc, I'll go all in for the benefit of everyone.


Replies are listed 'Best First'.
Re: Perl wrapper for v4l2-ctl
by zentara (Archbishop) on May 15, 2017 at 17:36 UTC
    Hi stevieb, I played around with it awhile back and found a couple of problems which you maybe able to overcome. First with v4l2 I was only able to grab single pictures at a time, however, they could easily be assembled into a video. Second, I could not find an efficient way to display the video in reatime, and save it simultaneously. My advice would be to use mplayer or mencoder, which can be run in slave mode for scripting.

    I have a raspberry pi too, and I love it. If I were attempting it, I would use either mplayer or mencoder run by perl in slave mode, which you can see a sample of in ztk-v4l-video-bloger/recorder. That link shows the use of slave mode, but the v4l part is outdated. It may help you though. Basically you get a window id of the Tk container, the canvas is easiest, and pass that window id to mplayer in slave mode.

    I have 2 working mplayer snippets which you may find useful. First is just display the camera in realtime.

    mplayer -tv device=/dev/video0:fps=29.97 tv://
    The following nicely records video and audio
    mencoder \ + tv:// \ + -tv driver=v4l2:device=/dev/video0:fps=30:width=640:height=480:for +ceaudio:alsa:adevice=hw.0,0 \ -ovc lavc \ + -oac lavc \ + -ffourcc divx \ + -noskip -mc 0 -ofps 30 \ + -o record-compressed-good-sync.avi

    The problem is to get them to work simultaneously, so you can have a realtime Tk display and save to a file as you go. The mplayer and mencoder options show a -dumpstream and -dumpfile option, but I as yet have gotten them to work together. Some have suggested using a tee for output, but no luck there either. Maybe you will be the one to discover the coding trick needed.:-). So, I recommend using a c program to do the heavy video work, rather than Perl.

    Here is an informative link describing the problem in full. play record same time.

    I'm not really a human, but I play one on earth. ..... an animated JAPH

      Thanks to both of you for the replies.

      I didn't realize that streaming to two locations simultaneously (display and file) would be problematic.

      I ordered my camera from Amazon Canada this morning and it should be here in a couple of days. At that time I'll commence playing around with various options.

      On another note, my GPS receivers came in today, so I'll be picking that up after work, so hopefully I'll have a Perl module for the GPS side of things (wrapped C, of course). One will be used with the Pi for the same project (so I can embed the coordinates in the video directly), and the other for my Arduino Trinket Pro, as I'm making my own GPS device for when I'm out on my days-long mountain hikes (yes, I could buy one, but where's the fun in that ;)

        Hi, you know, in terms of development time and cost, the easiest solution would be to have 2 raspberry pi's and 2 cameras. One to record, and one for realtime display. :-) What does a Pi and a camera cost $50? Plus you would have a backup to record with if your main recorder failed. Is $50 worth all the head-scratching required to get just 1 Pi to do it all? Like a few weeks of time for $50? :-)

        I also came across something for you to google around for. It is claimed that Gstreamer, which has a Perl module, has the ability to duplex the video feed from one camera. That is what we need, take 1 camera output and split it into 2 identical streams. It dosn't seem it should be that hard, but it eludes us for now. ffmpeg seems to be able to do it, see ffmpeg multiple outputs. I'm going to play with that.

        UPDATE0 These 2 links are the most promising leads. Use a kernel module 2 split the /dev/video stream, then use whatever to display 1 stream and record another. v4l2loopback module and splitting /dev/video to 2 streams .

        UPDATE1It works! Here is how to split /dev0 into /dev/video1 and /dev/video2

        modprobe v4l2loopback devices=2 # as root # then ffmpeg -f v4l2 -i /dev/video0 -f v4l2 /dev/video1 -f v4l2 /dev/video2
        Now you can watch /dev/video1 in realtime, and record /dev/video2. :-)

        I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Perl wrapper for v4l2-ctl
by thanos1983 (Priest) on May 15, 2017 at 13:36 UTC

    Hello stevieb,

    I found an old article from 2000 (Capturing Video in Real Time) mentioning a few things interesting if you ask me. Maybe Video::Capture::V4l module? Try to give it a go based on the information of the article, maybe you can come in contact with the author of the module and the article, maybe they have some progress on that.

    I think that could be a a good starting point, hope this helps.

    Update: Maybe also take a look here (3 Ways to Capture Adobe Flash Videos on Ubuntu Linux - wikiHow), it is written in Perl.

    Update2: Have you read this (Perl and Video Capture), it has a code example and also some recommendations.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1190276]
Approved by LanX
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2017-12-14 19:46 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (408 votes). Check out past polls.