Geek Friday: Getting Wanscam AJ-C2WA-C198 to Stream Using either FFMPEG or AVCONV

Wanscam.jpg

[This is now an obsolete article.  Justin.tv changed their system so that this would not work any longer.  In addition, Ubuntu 14.04 broke AVCONV severely so that it would no longer reliably capture the camera streams.  Look for an updated article to appear shortly on that matter.]

First off, my recommendation against getting a Wanscam IP camera hasn’t changed any.  The atrocious documentation and lack of any real support for upgrading firmware are among my complaints.  I cannot speak to the other Foscam knockoffs, so YMMV.

BTW, it is noteworthy that they finally have provided some software updates  which is good considering the brand involved in the recent baby monitor hacking appears to be either a Foscam or a near knock-off.  I’ll have to let you know how that works out.

Still, in the ever elusive search for a better strategy (not to mention use a little less bandwidth and fewer kludges), I have continued to search for a better solution to broadcast the stream to Justin.tv, which was one of my goals in the beginning.  I mean, yeah, you could save everything to a local computer, taking pictures of the people who have stolen your computer and your IP camera both, and then what?  You’d better hope that those still shots are clear enough to make out who it is!

I never got XSplit to work other than to bring up the view in Internet Explorer and scrape the screen with XSplit, since XSplit will only use RTSP/RTP from IP cameras   VLC seemed to have issues with showing the screen sometimes at first, or ever even, and other flaky things.  Having a wonderfully flaky network, thanks to Time Warner, hasn’t helped.

There are other issues as well: Whenever I want to scan something in or do anything that I don’t want to broadcast to the world, I have to shut it down.  Of course, that also seems to be whenever someone wants to come to the front door. :)  In addition, this was really meant to be a multimedia machine, and since the lovely Wanscam ActiveX control likes to pop up windows when an alarm is triggered, some way to suppress that has to be done.  I really want to move this off of the machine, but I also don’t want to use up another Windows license, but XSplit doesn’t run in Linux.

So, if it isn’t one thing, it’s another.  What needs to be done is to tackle it one problem at a time.  Is there a way to do this and have it work with Windows and Ubuntu and not cost an arm and a leg?

The short answer is: Sort of.

Seemingly a long time ago, before purchasing a license for XSplit, I had briefly looked at FFMPEG.  It is not a program for the faint of heart, and most of that is because it technically is not a finished product.  Rather, it is a development toolkit so others can write their applications for streaming, saving and editing video.  It’s a bit like scaffolding in that you are handed the pieces, but it is up to you to put them together.  Only, FFMPEG is more complicated, if for no other reason than it requires a lot more pieces and not not all of them fit together.  There is a certain way you are expected to put the pieces together, which includes knowing which actually do fit together and when.

Perhaps the biggest hurdle is that you must have some understanding of video and audio, and I do not, at least not on an appropriate level to say for sure what everything does and why.  However, after I set aside some time to bang my head against the desk, I got something working.

First, though, I have to explain that there is FFMPEG and there is AVCONV.  What’s the difference?  Remember yesterday when I explained what a fork is?  Well, due to some sort of philosophical conflict, it seems that AVCONV is just a recent split off of FFMPEG.  Or, perhaps more appropriately, AVCONV is a fork of the LIBAV project, which FFMPEG is the result of.  Remember, FFMPEG is a developer toolkit, and LIBAV is the code behind it.

If you are on Ubuntu, there is no reason to prefer one over the other, at least not yet.  However, Canonical has gone with AVCONV, so FFMPEG is “deprecated”, at least as far as Canonical is concerned.

If I seem to switch between the two, that will become more evident in the Windows section, which I must warn you that it works but not without incident.  Since this is Geek Friday, I’m going to explain what I discovered more or less in the order I discovered it and supply a lot of details.

If you feel as though you’re drinking from a firehose, just think how I felt!

Testing Capturing and Dumping the Stream

The first thing to settle was whether or not I could even capture the output of the camera.  So, I input the command into a terminal:

avplay “http://192.168.xx.xx:xx/videostream.asf?user=<MyUserID>&pwd=<MyPassword>&resolution=64&rate=0″

Replace <MyUserID> and <MyPassword> with the appropriate information set up on your camera.  I should mention again that I got this info off of the iSpyConnect site on the page “Connecting to your Wanscam IP camera“.

Now that I know I can receive the output from the camera, can I capture it to a file using avconv/ffmpeg?  Running the following, substituting appropriate variavles, sends it to a file:

ffmpeg -i “$CAMADD?user=$CAMUSER&pwd=$CAMPWD&resolution=64&rate=0″ -acodec libmp3lame -ar 22050 -ab 64k -vcodec libx264 -crf 30 -f flv $FFDUMPFILE

Now, I can use VLC to view the FLV file.

OK, so far so good.

Streaming to Justin.tv in Ubuntu

There seems to be a couple of ways to do this, but the following is what worked for me:

avconv -i “$CAMADD?user=$CAMUSER&pwd=$CAMPWD&resolution=64&rate=0″  -acodec libmp3lame -ab 64k -ar 22050 -threads 0 -f flv “rtmp://live.justin.tv/app/$API_KEY”

Which, is all well and good, as it does the minimum, but what if I wanted to search for something?  How would I know what the time is the event occurred?  Better yet, I probably already know the time, as the Wanscam sends out emails, but I still don’t know what time in a day long recording I’m looking at when viewing the video.  A timestamp is practically required.

So, I found out you can use the drawtext library and display the current time easily.  Here is the final bash script (again, makes no difference whether you call ffmpeg or avconv):

#!/bin/bash
CAMADD=”http://192.168.xx.xx:xx/videostream.asf”
CAMUSER=”yyyyyy”
CAMPWD=”zzzzzz”
API_KEY=”live_ryryryryryryryryryry”
OUTRES=’1280×1024′
FFDUMPDIR=”$HOME/FFMpegWork”
FFDUMPFILE=`ls $FFDUMPDIR/dump* | tail -1`
avconv -i “$CAMADD?user=$CAMUSER&pwd=$CAMPWD&resolution=64&rate=0″ -vf “drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf: text=’%F_%T': fontcolor=white@0.8: x=7: y=460″ -acodec libmp3lame -ab 64k -ar 22050 -threads 0 -f flv “rtmp://live.justin.tv/app/$API_KEY”

This works beyond my expectations!

Streaming to Justin.tv in Windows

So, now all I have to do is download the current Windows build, 64 bit if available, and turn this bash script into a CMD batch file, and all will be well, right?

Yeah, that’s funny, I know.  Nothing ever works that well, and this is no exception.

The very first problem is that the AVCONV program is not compiled with the appropriate flag, --enable-libfreetype.  If you want to read more about this, I got it from Einars Blog article “Adding time stamp overlay to video stream using ffmpeg“.

So, I downloaded the Windows 64 bit program for FFMPEG, which took forever.  However, once I did, I ran ffmpeg -filters and found drawtext.  However, it does not work.

I’m pretty sure the problem is the %F and %T.  I tried various escape sequences to no avail. It always seemed to be picking it up as a variable or those flags mean nothing to the Windows version.

So, I went in search of anything else I could use.  I found out from StackExchange in “Adding a time stamp to a video” that you can use the timecode function in drawtext instead.

So, the following worked … sort of:

set THOUR=%TIME:~0,2%
set TMINUTE=%TIME:~3,2%
set TSECOND=%TIME:~6,2%
c:\ fmpeg-20130813\bin\ fmpeg.exe -i “%CAMADD%?user=%CAMUSER%&pwd=%CAMPWD%&resolution=64&rate=0″ -t 00:%HRSLEFT%:00 -vf “drawtext=fontfile=/Windows/Fonts/arialbd.ttf: text=’Began %DATE% .. ‘: timecode=’%THOUR%\:%TMINUTE%\:%TSECOND%\:00′: r=25: fontsize=23:fontcolor=white@0.8: x=7: y=460″ -acodec libmp3lame -ab 64k -ar 22050 -threads 0 -f flv “rtmp://live.justin.tv/app/%API_KEY%”

However, I noticed after running only a few minutes that the time was wildly off.  In fact, I totally misunderstood the hour:minute:second:millisecond format because it was so messed up!  Stranger yet, it doesn’t follow the documentation, as the milliseconds should not even be necessary, yet it seems to refuse to run without it.

I should add I tried playing with the ‘r’ framerate parameter, which initially seemed to have an effect.  I think, however, it was a placebo effect, as I was soon back to square one.  BTW, I do see it should be ’30’ for NTSC instead of 25, which seems to be for PAL.  Changing it had no effect, however.

So, yes it does work, but using the timestamp functionality with the Windows version is not helpful at all.  So, unless there is a way to get the Wanscam itself to supply the date-timestamp, it is less than useful.  However, it does stream to Justin.tv without screen scraping, so this exercise may help someone out there.

Website Apps