Skip to content

Commit

Permalink
Various tweaks to make it work on Linux (Debian)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Cozic committed Jan 26, 2015
1 parent fb1ecc1 commit 078f4d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pmcam - poor man's video capture with motion detection in Bash

This simple Bash script can be used to capture images from a webcam, and will provide motion detection support. There's Yawcam on Windows which is good, but I couldn't find anything free on OS X. So I put together this quick and dirty solution, which does the job surprisingly well.
This simple Bash script captures images from a webcam with motion detection support. I wanted to find out what my dog was up to when I'm not at home, but couldn't find any free video capture software on OS X. I put together this quick and dirty solution, which does the job surprisingly well.

Frames are captured at regular intervals using `ffmpeg`. Then ImageMagick's `compare` tool is used to check if this frame is similar to the previous one. If the frames are different enough, they are kept, otherwise they are deleted. This provide very simple motion detection and avoids filling up the hard drive with duplicate frames.

Expand All @@ -14,17 +14,15 @@ Frames are captured at regular intervals using `ffmpeg`. Then ImageMagick's `com

### Linux

apt-get install ffmpeg
apt-get install imagemagick
sudo apt-get install ffmpeg
# or: sudo apt-get install libav-tools
sudo apt-get install imagemagick
curl -O https://raw.github.com/laurent22/pmcam/master/pmcam.sh

I could not test on Linux (feedback would be welcome) but according to the [ffmpeg documentation](https://trac.ffmpeg.org/wiki/Capture/Webcam) the ffmpeg command might need to be changed as follow:

- Set `-f v4l2`
- Set `-i /dev/video0`

### Windows

(Not tested)

* Install [Cygwin](https://www.cygwin.com/) or [MinGW](http://www.mingw.org/)
* Install [ffmpeg](http://ffmpeg.zeranoe.com/builds/)
* Install [ImageMagick](http://www.imagemagick.org/script/binary-releases.php)
Expand All @@ -37,13 +35,14 @@ The script will use the default webcam to capture frames. To capture using a dif

A frame will then be saved approximately every 1 second to the "images" folder next to the Bash script. Both delay and target folder can be changed in the script.

To stop the script, type Ctrl + C.
To stop the script, press Ctrl + C.

## TODO

* Check if the script is working on Linux. If necessary, provide alternative ffmpeg command depending on the OS - https://trac.ffmpeg.org/wiki/Capture/Webcam
* Allow specifying the video capture source and format (curently hardcoded)
* Command line argument to change frame directory.
* Command line argument to change interval between frame captures.
* Command line argument to specify the threshold for a frame to be kept.

## License

Expand Down
23 changes: 18 additions & 5 deletions pmcam.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OUTPUT_DIR="$SCRIPT_DIR/images"

CAPTURE_INTERVAL="1" # in seconds
FFMPEG=ffmpeg
command -v $FFMPEG >/dev/null 2>&1 || { FFMPEG=avconv ; }
DIFF_RESULT_FILE=$OUTPUT_DIR/diff_results.txt

fn_cleanup() {
rm -f diff.png $DIFF_RESULT_FILE
}

fn_terminate_script() {
fn_cleanup
echo "SIGINT caught."
exit 0
}
Expand All @@ -16,16 +25,20 @@ while true ; do
FILENAME="$OUTPUT_DIR/$(date +"%Y%m%dT%H%M%S").jpg"
echo "-----------------------------------------"
echo "Capturing $FILENAME"
ffmpeg -loglevel fatal -f avfoundation -i "" -r 1 -t 0.0001 $FILENAME
if [[ "$OSTYPE" == "linux-gnu" ]]; then
$FFMPEG -loglevel fatal -f video4linux2 -i /dev/video0 -r 1 -t 0.0001 $FILENAME
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
$FFMPEG -loglevel fatal -f avfoundation -i "" -r 1 -t 0.0001 $FILENAME
fi

if [[ "$PREVIOUS_FILENAME" != "" ]]; then
# For some reason, `compare` outputs the result to stderr so
# it's not possibly to directly get the result. It needs to be
# redirected to a temp file first.
OUT_FILE=$(mktemp -t diff)
compare -fuzz 20% -metric ae $PREVIOUS_FILENAME $FILENAME diff.png 2> $OUT_FILE
DIFF="$(cat $OUT_FILE)"
rm -f diff.png $OUT_FILE
compare -fuzz 20% -metric ae $PREVIOUS_FILENAME $FILENAME diff.png 2> $DIFF_RESULT_FILE
DIFF="$(cat $DIFF_RESULT_FILE)"
fn_cleanup
if [ "$DIFF" -lt 20 ]; then
echo "Same as previous image: delete (Diff = $DIFF)"
rm -f $FILENAME
Expand Down

0 comments on commit 078f4d5

Please sign in to comment.