Saturday, June 21, 2008

Webcam and MPlayer

Ubuntu really shocked me today when it recognized my integrated webcam automatically, which I discovered via Ekiga application.. Ok so drivers set, what I need now is record videos and take snapshots.. Mplayer as the title indicates does this all, the versatile player which can load video devices too. Lets begin with our task :


Viewing :


Fireup this command to bring up your video streamer:
$ mplayer -fps 15 tv:// -tv  driver=v4l2:device=/dev/video0

You can replace your video fps(frames per second), device and driver value as per your convenience.

Specify the video dimensions :
$ mplayer -fps 15 tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0

Horizontally inverse the stream i.e. mirror effect :
$ mplayer -vf mirror -fps 15 tv:// -tv  driver=v4l2:device=/dev/video0

View Fullscreen :
$ mplayer -aspect 16:10 -fps 15 tv:// -tv  driver=v4l2:device=/dev/video0
$ mplayer -aspect 16:10 -vf mirror -fps 15 tv:// -tv driver=v4l2:device=/dev/video0

Then press 'f' key to enter Fullscreen. NOTE: Enter aspect value as per your screen resolution ratio eg. 4:3, 16:9, 1:1


Snapshots:


Take snapshots while streaming by pressing 's' :
$ mplayer -vf screenshot -fps 15 tv:// -tv  driver=v4l2:device=/dev/video0

Mirrored :
$ mplayer -vf mirror,screenshot -fps 15 tv:// -tv  driver=v4l2:device=/dev/video0

The files will be saved in your current directory with name "shot####.png", where #### is for index number.


Video Capturing:


For video capturing you will need a companion of mplayer named "mencoder", install it if you don't have one. So here is the way you start your recording:

Without sound:
$ mencoder tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0 -nosound -ovc lavc -o filename.avi

With sound:
$ mencoder tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0:forceaudio:adevice=/dev/dsp -ovc lavc -oac mp3lame -lameopts cbr:br=64:mode=3 -o filename.avi

You can combine other subtags like vf, aspect, fps to enhance your video.

This is all, you can try a different short program too for video/photo capturing : VideoView.

This post took from me a lot of manual reading and googling but am happy to present it before you. Here are the major sources : Gentoo Wiki, Mencoder -vf and mplayer manuals.

6 comments:

Kaustav Acharya said...

Super Post dude... Hats off to you!

Unknown said...

Hi Anant,

Thanks for this article.

You made me leap frog, and I finally got the captured video on screen. gqcam, xawtv, v4lctl, kopete, ... nothing worked. I only knew that the camera and its chipset is supposed to be supported with the uvcvideo driver. The videos captured with ffmpeg where horrible and I didn't even get the camera to work properly with WinXP (well it's certified for Vista. That might say someting.)

I am using mplayer/mencoder a lot, but didn't think it could help with me with the camera.

Do you know of any way, how I can have the video displayed onscreen while capturing to a file at the same time. I might be able to figure it out, but I am not sure.

Yours
John W. Furst

P.S. It's so good I had to digg it.
P.P.S. I am on openSuSE-Linux

Anant Khaitan said...

@ John
First of all, thanks digging my post and am glad for your visit/reaction.
I am not sure if mplayer/memcoder allows capturing a video to file along with streaming but yes I will try, in the meanwhile if you figure it out.. do mention it.. Till then try VideoView

Unknown said...

Well, I tried VideoView. It's kind of okay, but I still don't get the sound from the camera via USB.

... and the camera is not recognized by browser plugins (like on YouTube or Facebook) anyway.

Too much hassle to go through. I just
ignore all this now.

Yours
John
Email Marketing Tips Blog Carnival

best articles blogs said...

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Ainsley

charlos john said...

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. Eduardo