{"id":959,"date":"2010-11-28T09:48:57","date_gmt":"2010-11-28T08:48:57","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=959"},"modified":"2010-12-04T12:06:37","modified_gmt":"2010-12-04T11:06:37","slug":"create-images-from-webcam-images","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/11\/28\/create-images-from-webcam-images\/","title":{"rendered":"Create images from Webcam images"},"content":{"rendered":"<p>It is just a pain in the ass if you cannot get any work done because you have to constantly check if the image of a webcam has changed. Therefore I wrote a shell script that downloads the webcam images and puts them together as a video.<br \/>\n<!--more--><br \/>\nWhat the scrip does is the following:<\/p>\n<ol>\n<li>download an image from an url<\/li>\n<li>check if the image is the same as the last one (do not store duplicates)<\/li>\n<li>Create a video from all downloaded images<\/li>\n<\/ol>\n<p>Now let&#8217;s give some credit where it is due:<br \/>\nDownloading the image can be done with either wget or curl. However the file must be renamed do avoid overriding: <a href=\"http:\/\/hintsforums.macworld.com\/archive\/index.php\/t-31644.html\">Curl for grabbing webcam pics in succession?<\/a><br \/>\nBasically there are three ways to create a movie from images. Two are described in <a href=\"http:\/\/electron.mit.edu\/~gsteele\/ffmpeg\/\">Making movies from image files using ffmpeg\/mencoder<\/a>. The <a href=\"http:\/\/www.linux.com\/archive\/feature\/136606\">third<\/a> did not work for me but this may because I use Ubuntu Gnome. On a KDE desktop it might work. This solution however only works with a desktop an cannot be run on a server or a NAS.<br \/>\nThe iterative aspect of the execution of the script I solved with a <a href=\"https:\/\/help.ubuntu.com\/community\/CronHowto\">cron job<\/a>.<\/p>\n<p>I put the code into two functions. The first takes for arguments:<\/p>\n<ol>\n<li>The URL from where to download the image<\/li>\n<li>The path where the image is to be stored<\/li>\n<li>The name of the video file (without file extension)<\/li>\n<li>The file name of the downloaded image<\/li>\n<\/ol>\n<pre>\r\ndownloadAndCreateMovie(){\r\n\tSTORE_PATH=$2\r\n\tURL=$1\r\n\r\n\tif [ ! -d $STORE_PATH ]\r\n\tthen\r\n\t\tmkdir $STORE_PATH\r\n\tfi\r\n\tcd $STORE_PATH\r\n\tPREFIX=$3\r\n\tLASTFILE=`ls -Atr *.jpg | tail -1`\r\n\tFILENAME=$4\r\n\r\n\r\n#\tcurl -O $URL\r\n\twget -q $URL\r\n\r\n\tMD5LAST=`md5sum -b $LASTFILE | sed 's: :\\n:g' | head -1`\r\n\r\n\tMD5CUR=`md5sum -b  $FILENAME | sed 's: :\\n:g' | head -1`\r\n\r\n\tif test $MD5LAST != $MD5CUR\r\n\tthen\r\n\t\tcp -v $FILENAME `date +\"%y%m%d-%H%M%S\"`.jpg\r\n\t\timages2movie $PREFIX\r\n\tfi\r\n\trm $FILENAME\r\n}\r\n<\/pre>\n<p>First we make sure that the directory where we store the images exists. Then we figure out what the file name of the latest downloaded image is. We can then download the image with curl or wget. To compare the two files (the latest and the just downloaded) we compute the md5 checksum and compare them. If the do not match we rename the downloaded file by giving it a timestamp as a file name and then call the second function to create the video.<br \/>\nThis function has some drawbacks:<\/p>\n<ol>\n<li>The name of the downloaded image could be retrieved from the URL<\/li>\n<li>If the function is called upon an empty folder there is no last file and the function gets stuck in the computation of the md5 sum of &#8220;<\/li>\n<li>There is no exception handling on the download. I guess it will just time out the there will be no image<\/li>\n<\/ol>\n<p>Now the second function creates the video from the image files. It takes the name of the video file as an argument. I choose the ffmpeg approach since it did work better for me and might work better on a NAS.<\/p>\n<pre>\r\nimages2movie(){\r\n\tMOVIE_NAME=\"movie\"\r\n\tMOVIE_NAME=$1\r\n\tDURATION=3\r\n\tmkdir movie\r\n\tFILECOUNTER=`printf \"%05d\" 0`\r\n\tfor file in `ls`\r\n\tdo\r\n\t\tfor ((  i = 0 ;  i < = $DURATION;  i++  ))\r\n\t\tdo\r\n\t\t\tcp $file movie\/$FILECOUNTER.jpg\r\n\t\t\tFILECOUNTER=`expr $FILECOUNTER + 1`\r\n\t\t\tFILECOUNTER=`printf \"%05d\" $FILECOUNTER`\t\r\n\t\tdone\r\n\tdone\r\n\trm $MOVIE_NAME.mp4\r\n\tffmpeg -r 1 -b 1800  -s svga -i movie\/%05d.jpg $MOVIE_NAME.mp4\r\n\trm -rf movie\r\n}\r\n<\/pre>\n<p>The video is created with a frame rate of one frame per second. If you want to display the same image over several seconds you have to input it several times. The variable DURATION defines how many times. The images are copied into the temporary video directory. The file name is changed to consecutive numbering with five digits starting with 0. The tricky part is the counting: The file name is a string so you have to evaluate the expression of adding 1 to the numeric file name and then reformatting it as a five digit string.<br \/>\nThe next thing is to create the video from the sample images. The parameter -r specifies the frames per second. The parameter -b defines the bitrate. The parameter -s defines the output format. Here you will want to select a format that matches the format of the images for best quality. The parameter -i defines the input using the same expression as the file name. The last parameter is the output file name.<br \/>\nBasically that's it. Therefore the call would look like this:<\/p>\n<pre>\r\nSTORE_PATH=~\/Pictures\/kronplatz\/Marchner\r\nURL=http:\/\/www.kronplatz.com\/_webcam\/70\/webcam7.jpg\r\ndownloadAndCreateMovie $URL $STORE_PATH Marchner webcam7.jpg\r\n<\/pre>\n<p>To install this a a cronjob issue<\/p>\n<pre>\r\ncrontab -e\r\n<\/pre>\n<p>on the console and add this line<\/p>\n<pre>\r\n*\/10 *    * * * \/home\/andi\/bin\/downloadImage.sh > \/dev\/null 2>&1\r\n<\/pre>\n<p>You will want to apply the correct path to your script. This line executes the script every ten minutes and throws away all output. If the images on the URL are replaced regularly you are doing good in choosing the same interval here.<br \/>\nWhen closing the editor the temporary file will be stored and installed as cron job.<\/p>\n<p>The script can be downloaded:<br \/>\n<a href='http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2010\/11\/downloadImage.sh'>downloadImage.sh<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is just a pain in the ass if you cannot get any work done because you have to constantly check if the image of a webcam has changed. Therefore I wrote a shell script that downloads the webcam images and puts them together as a video.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,6],"tags":[169,113,63,302,167,166,168],"class_list":["post-959","post","type-post","status-publish","format-standard","hentry","category-linux","category-programmieren","tag-cron","tag-en","tag-image","tag-linux","tag-movie","tag-shell","tag-webcam"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=959"}],"version-history":[{"count":5,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":975,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/959\/revisions\/975"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}