Dan Newcome, blog

I'm bringing cyber back

Recording webcam videos with VLC Media Player

with 18 comments

I have been recording short videos using the webcam on my laptop using a trial version of some video software that I found on the net. I had also been using the free Yawcam to snap stills, but I didn’t figure out how to get it to record video. It apparently can periodically save still frames or stream over HTTP, but what I wanted in the end was an .mpg file. I searched around the net to find an open source program that would record video from my webcam but I came up empty. Cheese seems like a good option under Linux, but my laptop is running Windows right now, so that doesn’t help me. If anyone knows of something let me know in the comments. It’s probable that one of the open source nonlinear editing programs is able to do this, but I don’t know how to do it.

I’ve used VLC media player to play videos on Windows and Linux for a long time, and in my search for webcam software found that it can supposedly record video from a live source, so I decided to give it a try. The tutorials that I found were mostly outdated, so it turned out to be pretty frustrating to get working, which is the primary motivation for writing this post. Hopefully others will be able to get this working on the current version of VLC (1.0.3 at the time of this writing) more easily than I was able to.

Just a warning, I haven’t gotten this to fully work the way that I wanted using the GUI yet, so the final solution presented here will be a command line invocation of VLC. It turns out that this is more convenient since there are a lot of tedious steps to go through that are completely automated when using the command line.

Foreword on VLC

Unlike many video programs on the Windows platform, VLC does not use any external codecs or filters. It is completely self-contained. This provided a major source of confusion for me initially, as I was looking around endlessly for the Xvid codec that I wanted to use only to find that it was never detected by VLC.

Even though VLC is self contained, its functional elements are arranged into what the VLC authors call modules. This is important to understand when trying to chain together the functions that we want on the command line. The most helpful synopsis for me was found here, and I’ll put the general form inline here for reference:

% vlc input_stream --sout "#module1{option1=parameter1{parameter-option1},option2=parameter2}:module2{option1=...,option2=...}:..."

The commandline shown above is for Linux systems, but the important thing to notice is that the first module is referenced using #module and subsequent  modules are referenced using :module. Also, options to modules are enclosed in curly braces {…} and may be nested. Nesting will be important when we try to split the stream so that we can both record it to disk and monitor it on the screen during recording.

I noticed some inconsistency in the documentation that I found concerning the argument formats that are supported on various platforms. For example –option param syntax is not supposed to work on Windows, but it appears to in most cases.  We will adhere to the Windows –option=param form however.

VLC is also very flexible and consequently is complicated when it comes to setting up all of the options required to create a seemingly simple mpeg stream. I never knew about different mpeg container formats for network broadcast vs local media (PS vs TS) before this, and it is debatable that it is that useful unless you are into video pretty heavily. You won’t need to look at this to do follow what we are going to do here, but it was an issue when I was trying to figure this out, so if you go off the beaten path there may be more to figure out than you think.

Some of the codecs are very strict about the options that they will take, and you won’t get detailed information about what went wrong unless you have enabled detailed logging. This is covered in the first part of this tutorial. One such gotcha that hit me was that mpeg-2 only supports certain frame rates. The VLC codec adheres to these restrictions rigorously, and if a valid frame rate is not specified you will get a cryptic error about the codec not being able to be opened. Similarly, if no frame rate is specified VLC will not default to something that works, so you have to figure out what went wrong on your own.

Building the commandline

Invoking VLC is as simple as running vlc.exe. However we would like to turn on some extended logging while we are trying to get our options set up correctly. Otherwise issues such as the encoder failing to open will not be easily solved since we won’t know exactly what is going wrong.

The very first thing we should try is to make sure that we can open the webcam with extended logging enabled. The webcam device on my laptop is the default device, so we can open it using dshow:// as shown in the command below. We turn on logging using the –extrainf option with the maximum level of verbosity specified using the -vvv flag. A small warning: mute the microphone on your computer before running the following since you might get a feedback loop that is pretty loud. We will fix this later by using the noaudio option to the display module.


c:> vlc.exe dshow:// --extrainf logger -vvv

If all goes well you should see a VLC window showing the output of your webcam. The only thing left now is to transcode the video stream into mpeg-2 and save it to a file (all while showing a preview window), which turns out to require some VLC module gymnastics.

Transcoding

The main task that we are trying to accomplish is actually transcoding the stream, which is the term for encoding the stream as mpeg to be saved to a file. The output of the webcam is in an uncompressed format, so we need to run it through a codec before we can save it to disk. The following command uses two different modules: transcode and standard. Transcode lets us create an mpeg stream and standard lets us package it into a container and save it to disk. This seems pretty straightforward, but there are some voodoo options here that I saw in the examples online but didn’t find very good explanations for. Setting audio-sync for example. Do we ever want un-synced audio? The important part that seems to be left out of many examples is the setting of the frame rate and the size. Failing to set the frame rate using the fps option caused the encoder to fail for me. Failing to set the width caused problems later when I tried to preview the video stream during recording.


c:> vlc.exe dshow:// --sout=#transcode{vcodec=mp2v,vb=1024,fps=30,width=320,acodec=mp2a,ab=128,scale=1,channels=2,deinterlace,audio-sync}:standard{access=file,mux=ps,dst="C:\Users\dan\Desktop\Output.mpg"} --extraintf=logger -vvv

Monitoring the stream

Using what we have so far will get us a stream on disk, but we can’t see what we are doing on the screen. Fortunately VLC has a module called display that will let us pipe the output to the screen. Unfortunately we can’t do that without also using the duplicate module to split the stream first. Using duplicate isn’t too complicated, but it took me a little while to find out how to use the nesting syntax that is needed to get it to work. The general form of the duplicate module is:


duplicate{dst=destination1,dst=destination2}

Where destination1 and destination2 are the module sections that we want to send the stream to.  The only confusing part is that we have to move our standard module declaration inside of the duplicate module definition like this:


duplicate{dst=standard{...}}

Once we have this form, we can add other destinations like this:


duplicate{dst=standard{...},dst=display{noaudio}}

We have added a second destination to show the stream on the screen. We have given the option noaudio in order to prevent a feedback loop since by default display will monitor the audio.

My final command looked like this:


c:> vlc.exe dshow:// --sout=#transcode{vcodec=mp2v,vb=1024,fps=30,width=320,acodec=mp2a,ab=128,scale=1,channels=2,deinterlace,audio-sync}:duplicate{dst=standard{access=file,mux=ps,dst="C:\Users\dan\Desktop\Output.mpg"},dst=display{noaudio}} --extraintf=logger -vvv

I put the command into a batch file, and now I can create an .mpg file by running the batch file. Some possible improvements could be to parameterize the file name and perhaps allow for setting the bitrate, but for now this suits my needs perfectly.

About these ads

Written by newcome

January 17, 2010 at 12:05 pm

Posted in Uncategorized

18 Responses

Subscribe to comments with RSS.

  1. This can be a I enjoy examples of articles which have been written, and especially the comments posted! I’ll come back!

    Lena Furman

    February 4, 2010 at 6:00 am

  2. I enjoy examples of articles which were written, and especially the comments posted! This is a

    Lloyd Elefritz

    February 7, 2010 at 6:45 am

  3. An useful review

    DPG Converter

    May 16, 2010 at 8:30 pm

  4. If my initial searches would have found this article I would have saved the hours of time trying to decipher the multiple versions of alleged VLC command line docs. Thank you! Job well done! PS: I’m finally rid of the horrid Linksys software thanks to you.

    Mike Robert

    July 2, 2010 at 8:39 am

  5. Glad it helped you out Mike. I can relate to the crappy OEM software.

    newcome

    July 2, 2010 at 10:01 am

  6. Here is my script to record 2fps h264 video from my ip security camera. You can adjust the “stop-time” for recording length(seconds), or drop it for endless recording. The resulting output file is extremely small because it records at 2 fps.

    “C:\Program Files\VideoLAN\VLC\vlc.exe” “your stream address here” –sout=”#transcode{vcodec=h264,noaudio,fps=2,width=640}:standard{access=file,mux=mp4,dst=”D:\temp\vlc_cmdline_2fps.mp4″}” –stop-time=15 vlc://quit

    jerry

    September 5, 2010 at 11:09 am

  7. @jerry – thanks for posting your script. It’s cool that you can set the frame rate so low and still have a video stream instead of having to use a tool that does still frames.

    newcome

    September 6, 2010 at 7:27 pm

  8. OMG! I love you man!

    Just recorded smooth 320×240 WITH AUDIO from my webcam!

    Looks great sounds great and I can see what I am recording…keep up the great work! Thanks amigo.

    neoNiV

    October 23, 2010 at 12:51 pm

  9. set /p foo = bar.txt

    vlc.exe dshow:// –sout=#transcode{vcodec=mp2v,vb=1024,fps=30,width=320,acodec=mp2a,ab=128,scale=1,channels=2,deinterlace,audio-sync}:duplicate{dst=standard{access=file,mux=ps,dst=”C:\vidcaps\Output%foo%.mpg”},dst=display{noaudio}} –extraintf=logger -vvv

    Place the above into batch file (i.e. capture.bat)

    When you run the capture.bat file it will auto-increment the output filename (output1.mpg, output2.mpg etc.)

    This way you do not overwrite your previous captures.

    Line one reads bar.txt (counter file)
    Line two increments counter
    Line three writes the new value

    value is put into filename using %foo%

    Cool eh?

    neoNiV

    neoNiV

    October 23, 2010 at 1:51 pm

  10. sry fer dbl posting…line 2 and 3 did not show:

    set /A foo += 1
    echo %foo% > bar.txt

    hope is does this time.

    email me if not and I will send the complete .bat file.

    neoNiV

    October 23, 2010 at 1:54 pm

  11. sry fer dbl posting…line 2 and 3 did not show:

    set /A foo += 1
    echo %foo% > bar.txt

    hope is does this time.

    email me if not and I will send the complete .bat file.

    neoNiV

    October 23, 2010 at 1:58 pm

  12. Dan! awesome post!
    But I have one question, how can one hide the VLC window from appearing? I removed the debug option and the replicate option and still the VLC window appears!
    Any idea?

    Greg C

    March 11, 2011 at 8:57 am

  13. Thanks! One of the better entries on usage of VLC for this simple task. I needed a way to monitor my dog for separation anxiety while we were out, and rather than go with an off the shelf product, went with VLC. For me the following worked very well. My requirements were decent audio, in sync poor to medium video, but most important, I needed to be able to quickly skip parts and video needed to be as smooth as possible, quality was unimportant.. Note that I had multiple devices for capture. I also have a dual core HT processor thus I gave it 8 threads(2 per virtual processor)

    C:\Program Files (x86)\VideoLAN\VLC>vlc.exe dshow:// “–dshow-vdev=Integrated Camera” “–dshow-adev=Internal Microphone (Conexant 2″ –sout=#transcode{vcodec=mp4v,acodec=mp2a,threads=8,scale=1}:std{access=file,mux=avi,dst=”C:\Output\stream.mp4″}}

    cy43rguru

    August 8, 2011 at 8:57 am

  14. @cy43rguru – Cool use case, thanks for sharing your settings.

    newcome

    August 8, 2011 at 9:17 am

  15. does vlc support RTMP Straming

    Sreerag

    August 17, 2011 at 2:25 am

  16. A nicely written artcle. Unfortunatly VLC has never worked for encoding for me. It’s always thrown out audio but no video no matter what settings I choose. I’ve tried this for years and for some reason I’m the only person who can’t do it. If anyone has any help they can offer I’d gladly accept it, but VLC just seems to buggy for me.

    chriswere

    September 24, 2011 at 12:10 pm

  17. Greg C, there was something with -dummy in the beginning that should stop vlc from appearing.
    You’ll find an example on google.
    Now working on a guestbook, hope it’ll work

    Timothy

    November 11, 2011 at 5:22 am

  18. [...] [...]


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 )

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: