Results 1 to 12 of 12

Thread: Record from webcam

  1. #1
    skizzik Guest

    Default Record from webcam

    Hi

    Is there an easy way that I can get my program to record video from a webcam and store it as a videofile? Any open source examples? I am programming in C++ with Qt Creator. I am first and foremost interesten in getting it to work on a PC, but Mac would be nice too. Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Record from webcam

    This is camera/driver specific, there is no general way to do this with any camera.
    So first you need to know which camera you are using, and usually it will come with a lib that will allow an application to interface with it, and operate it, including image acquisition.
    This is NOT a Qt issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    skizzik Guest

    Default Re: Record from webcam

    Quote Originally Posted by high_flyer View Post
    This is camera/driver specific, there is no general way to do this with any camera.
    So first you need to know which camera you are using, and usually it will come with a lib that will allow an application to interface with it, and operate it, including image acquisition.
    This is NOT a Qt issue.
    Um, thats why I posted it in the General Programming-forum, for "non-Qt programming issues".

    The problem is, I don't know what cameras the users will have. Are there no standard libraries that works with most of the common cameras? How do other programs make it? Do one have to write different code for every single webcam that the program must support?

    Thanks for the answer so far!

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record from webcam

    Which operating systems you want to support ? You can use OpenCV to capture and write frames to file, its cross-platform but AFAIK wont give you full control over camera parameters (like gain, exposure, zoom etc. ). On windows, you can use DirectShow, for linux there is v4l, I dont know about mac.
    Definitely you dont have to write separate code for every camera model or write a driver, this would be ridiculous, just use one of mentioned interfaces (DirectShow, OpenCV, v4l).

  5. #5
    skizzik Guest

    Default Re: Record from webcam

    Quote Originally Posted by stampede View Post
    Which operating systems you want to support ? You can use OpenCV to capture and write frames to file, its cross-platform but AFAIK wont give you full control over camera parameters (like gain, exposure, zoom etc. ). On windows, you can use DirectShow, for linux there is v4l, I dont know about mac.
    Definitely you dont have to write separate code for every camera model or write a driver, this would be ridiculous, just use one of mentioned interfaces (DirectShow, OpenCV, v4l).
    The most important systems to support is Windows XP/Vista/7. If i can get it to work on Mac too, that would be a big bonus! I will look into the OpenCV and DirectShow. Thanks very much for the answer!

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Record from webcam

    Which operating systems you want to support ? You can use OpenCV to capture and write frames to file, its cross-platform but AFAIK wont give you full control over camera parameters (like gain, exposure, zoom etc. ). On windows, you can use DirectShow, for linux there is v4l, I dont know about mac.
    Definitely you dont have to write separate code for every camera model or write a driver, this would be ridiculous, just use one of mentioned interfaces (DirectShow, OpenCV, v4l).
    To my knowledge, the quoted text is very simplistic in the the way it views the problem.
    But to put it in a bit more perspective what you can do, is read about v4l for linux and DirectShow/Media Foundtation for Windows, and see which cameras support both, and to how many cameras this amounts to.
    If you feel that the large spectrum of cameras that you intend your software for are supporting these technologies, then you could start with that.
    This will mean you will have to implement the windows code once for the windows version, and the v4l code once for linux.
    But be aware, that it will always be a very partial list of cameras you support, and better code some checking code, that will let the user know, if his camera is not supported (or that his camera does not support the media access technology you are working with).
    Generally, you will have to code support for each media technology you want to support, or, use libs that perhaps do that already, such as OpenCV and others.
    These too, support might suport several such technologies, but with various levels of features.
    Again, you will need to read the documentation for these libs, and see which one fits best you needs.
    There is not yet "one ring to rule them all" in this.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    skizzik Guest

    Default Re: Record from webcam

    I now use OpenCV, which almost works well. The problem now is that saving the video takes too long.

    Saving the video captured from the cam to a file was far to slow without encode it so I use the h264 encoding now. Initially this works but after about 20-30 second of recording, when the videofile is a few MB large, it become too slow again. The process of writing a frame with cvWriteFrame takes to long and some frames are dropped. Recording 5 sec results in a 5 sec videofile, but recording 60 sec results in a 45 sec videofile (or something). What can I do to save videos from the cam that are longer than 20 seconds?

    Thanks for all the help so far!

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record from webcam

    If you set, lets say, 30 fps for your camera, then you cannot assume that you will get exact 30 images each second. It depends on light conditions, camera driver, device settings, the device itself etc.
    So, the longer time of recording, the higher difference you'll experience.
    If you want to have smaller videos, use xvid for encoding the file, it creates really small videos with quite good quality, and you will be able to control quality/size factor.
    For a serious, high quality (or lossless) recordings you will have to use lossless codecs (like Lagarith) and really good (and expensive) cameras. Dont expect good results if you use cheap usb webcams (like the ones installed on notebooks).

  9. #9
    skizzik Guest

    Default Re: Record from webcam

    Thanks for the answer!

    I don't think xvid will solve the problem, cvWriteFrame is too slow as soon as the videofile is 2 MB or so. I use an usb cam but the quality isn't a big problem right now. The problem is that more and more fps are dropped as the videofile gets bigger. The only solution I can think of is to keep all frames in RAM until the recording is finished, and then write it to the file. But it is encoded to h264 with cvWriteFrame so if i save them in RAM, it is in the raw-format which taks a lot of memory even for, say 256*192 pixels.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record from webcam

    Yes, well you dont need to keep all images in memory, you can have an image queue to push incoming images at one end, and store them with cvWriteFrame at other, but you will eventually get overflow at some point, or will have to drop the frames from buffer yourself - if the capture speed will exceed the recording performance. But for short videos this can work well.
    Also, cvWriteFrame speed depends on the codec used for recording, so maybe you should try other codecs, there are plenty of them.

  11. #11
    skizzik Guest

    Default Re: Record from webcam

    When I start writing to a new videofile, capturing a frame and write it to the file with cvWriteFrame takes about 20 msec. After 20 sec, when the file is 2 MB or so, it takes about 40 msec. I will try your tips now, but it seems like it will be hard to make videos larger than a few MB. How do other programs, which can make "infinite" large files in realtime, solve it?

  12. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record from webcam

    I don't know a cross platform way to solve this. I'm working with an app where you need to deal with large, high-quality video files for medical analysis (walk/run phases), targeted on Windows systems. Recording was implemented using OpenCV, but we have switched to M$ Direct Show api, because it produced better results for high frequency cameras (~90 fps), with less resources consumption. Writing frames to the file is done behind the scenes, you need to pass only the end file path and codec you want to use. End result is that memory usage during creating the video is constant, so you can create as big videos as you like. It was very easy to implement (as camera capture interface uses Direct Show) and produces satisfying results.
    Of course it works only for Windows. I think to get the best results you will have to implement separate recording mechanisms for each platform, using "native" interfaces (like DShow on win, v4l on linux etc). To me, OpenCV recording mechanism seems suitable for tests and simple applications.

Similar Threads

  1. Best way to stream and record a webcam
    By ichigo in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2014, 23:51
  2. Replies: 7
    Last Post: 27th November 2010, 15:55
  3. Replies: 3
    Last Post: 26th March 2010, 04:32
  4. Webcam USB under Windows with qt4
    By Wickinger in forum General Programming
    Replies: 3
    Last Post: 18th March 2008, 00:10
  5. use Webcam
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 21st August 2007, 07:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.