Wednesday, December 4, 2013

Access and stream a webcam over ssh to vlc

Ever had the desire to see if someone was on your computer? Wished you could get on there and just see through your webcam? Me neither, but one day I got curious and wanted to see if/how it could be done. It's kind of a beast of a command, but I just put it in a script which makes it very easy to use.

What you need:
ffmpeg
vlc (or mplayer)

Here's the script.( I actually got it from here. Then just changed it to use vlc because it seems to work better for me.


#! /bin/bash

B="100K"
F="ogg"
ssh <remote_host> ffmpeg -b $B -an -f video4linux2 \
    -s 320x240 -r 10 -i /dev/video0 -b $B -f $F - \
    | vlc -


I would break it down for you but I haven't gotten familiar enough with the ffmpeg command. So all I really do is copy and paste, and I only have a general idea of what it is doing. It worked when I copied it so I didn't need to figure it out to change it. If it isn't working, the first thing I would check is the device name in case it's not /dev/video0
vlc -
just tells vlc to read from stdin.(the pipe)

In my script I substituted <remote_host> for $1 so from the command line I can just run
$./script_name <remote_host>
since I wanted to be able to use it on different computers/webcams instead of just one (if I get more than one webcam on one computer then I may also have /dev/video0 passed as a parameter.

You can change the size with the -s flag I changed mine from 320x240 to 640x480 and it streamed fine.

It may take a few seconds for the picture to actually come through and be received, (I know it does for me) so once you run it then give it a little minute to get that data through to vlc.

So there it is. Just for fun.

1 comment:

  1. ffmpeg is deprecated, use avconv instead (I think that package libav-tools needs to be installed)

    ReplyDelete