UPDATE: Read Zloop's comment below for an easier/better solution.
--
Hello everyone
I have been playing around with USB webcams on my OpenWRT devices lately and wanted to share my adventure of compiling a custom "motion" package with ffmpeg support.
You can read about Motion here, but be warned that this website is a little out of date:
http://www.lavrsen.dk/foswiki/bin/view/Motion/WebHome
The regular motion package which is found in OpenWRT is not compiled with ffmpeg support. That means all of the ffmpeg config options, including any ability to record video, is absent. You can only save still picture files when events are detected. And, while that still-picture support is pretty good, I wanted to try out movies too.
Just a note to the OpenWRT devs: Might I suggest marking the package description on motion to reflect the fact that it's built without ffmpeg support? That would be nice. I didn't realize ffmpeg support was missing until after I started trying to figure out why my movie files were not being written.
Before I start, I need to explain some of the problems I have encountered while testing motion.
My setup is that I have a GL-iNet 6416A, a small USB2 hub, a 16GB flash USB storage device, and a Genius Wideview F100 webcam.
The use of the GL-iNet 6416A is notable because it has 16MB of flash, 64MB of RAM, and a 400Mhz AR9331 SOC CPU.
The flash space is important because the ffmpeg package and it's libraries take up almost 8MB of space. You can always install it on your USB flash storage though, I suppose.
32MB of RAM is probably sufficient to run motion. On my system, free is showing about 30MB of used RAM even after clearing caches, but that's with Luci and a few other non-critical things also running.
The 400Mhz CPU is not enough. Motion is a CPU intensive software package and if I end up running it on this little box, the CPU is going to be at 100% usage 100% of the time and motion will still be dropping frames.
The issue is motion's detection logic, so this isn't an ffmpeg problem, though I'm sure that is making it worse. It takes a lot of CPU cycles to take an image, compare it to the next image, delta that, and figure out if enough pixel changes exist to warrant an event trigger.
If you want to run motion, you will need a strong CPU or a low resolution.
With motion configured for 320x240 at 30fps, I am seeing less than 10fps. 640x480 at 1fps can barely keep up and frames drop occasionally. 800x600 at 1fps drops 2-3 frames out of every 1 frame shown.
I will say that motion is handling frame drops very gracefully in my testing, so that's good, but the CPU can't keep up.
Also note that having a good camera input is important. Make sure to get a webcam that offloads the MJPG encoding rather than one of those cheap YUV-output cams like I tried with first. Test with mjpg-streamer first to see how much CPU your webcam requires at different resolutions. For me, an 800x600 30fps mjpg-stream required about 18% of my 400Mhz CPU.
Now, with all of that said, let's get back to the subject at hand.
I build my own OpenWRT from fresh sources, so this HOWTO reflects that. If you want to grab a particular release version and build for it, you need to make sure to grab the right sources.
# Clone the repository
mkdir ~/openwrt-custom ; cd ~/openwrt-custom
git clone git://git.openwrt.org/openwrt.git .
# Get the package feeds
# http://wiki.openwrt.org/doc/devel/feeds
./scripts/feeds update -a
./scripts/feeds install -a
# Do prerequisite tool builds
make tools/install
make toolchain/install
# Build our ffmpeg package, which we need prior to making the motion package
PACKAGE="ffmpeg"
make package/$PACKAGE/compile && make package/$PACKAGE/install
Now comes the custom part. We need to change the motion Makefile.
vi package/feeds/packages/motion/Makefile
Edit the Makefile as follows:
Add " +ffmpeg" to the end of the "DEPENDS:=+libjpeg +libpthread" line. This will add ffmpeg as a dependancy package to motion.
On the "TITLE:=" line, add ", with ffmpeg support" to the end
And the really important part is under the "CONFIGURE_ARGS" section.
Change the "--without-ffmpeg" part to be something like:
--with-ffmpeg=/home/USER/openwrt-custom/build_dir/target-mips_34kc_uClibc-0.9.33.2/ffmpeg-full/ffmpeg-2.6.3/ipkg-install/usr/
Be sure to change USER to your own home directory name and to retain the \ newline characters.
Also note that for different versions and targets, you will need to adjust the above line to match your particular environment.
If in doubt, search for this directory. It should already be there since you just built ffmpeg.
# Finally, build motion
# Note that we output some debugging text files here to help later if there are problems.
PACKAGE="motion"
make V=s package/$PACKAGE/compile &> motion_debug-compile.txt && make V=s package/$PACKAGE/install &> motion_debug-install.txt
less motion_debug-*.txt
Look for "ffmpeg". If your debug txt files have things like "<-... not found" and "ALL FFMPEG FEATURES DISABLED", then it failed. Check that "--with-ffmpeg" line to make sure it's correct. Use a fully qualified absolute path there.
Woopie. If you did it right, your new motion .ipk will be under the bin/ directory somewhere, along with all of the dependancy packages you need. Copy them to your device and install with opkg.
Edit your /etc/motion.conf file as you see fit.
There is no init script with motion at this time for me, so manually start it.
# Start motion
motion -c /etc/motion.conf
# Stop motion
if test -f /var/run/motion/motion.pid ; then kill $(cat /var/run/motion/motion.pid ) ; fi
Have fun
Please post corrections, questions, and feedback below. I will try to update this post as needed.
(Last edited by jmomo on 14 Mar 2016, 23:51)