{"id":47,"date":"2011-01-04T20:50:05","date_gmt":"2011-01-04T18:50:05","guid":{"rendered":"https:\/\/www.melen.org\/u\/jan\/wp\/?p=47"},"modified":"2011-01-08T21:13:17","modified_gmt":"2011-01-08T19:13:17","slug":"motion-recording-webcam-script","status":"publish","type":"post","link":"https:\/\/www.melen.org\/u\/jan\/wp\/?p=47","title":{"rendered":"Motion recording webcam script"},"content":{"rendered":"<p>To do a motion recording camera with FreeBSD 8.1 you need to install  multimedia\/pwcbsd and  multimedia\/mencoder from ports. With these and a short perl script you can do a camera that records motion and splits them in to separate files when there is inactivity for 600s.<\/p>\n<pre>\r\n#!\/usr\/local\/bin\/perl\r\n#\r\n# Author: Jan Melen-jan(at)melen.org\r\n#\r\nuse POSIX qw(strftime);\r\nuse IO::Select;\r\n\r\nif (($#ARGV + 1) != 1) {\r\n    print \"    Usage: camera.pl &lt;Dir for storing mpeg4 files&gt;\\n\";\r\n    exit;\r\n}\r\n\r\n$dir = \"$ARGV[0]\/\";\r\n$sel = new IO::Select();\r\n\r\nsub open_pwcview {\r\n    open(PWCVIEW, \"\/usr\/local\/bin\/pwcview -hkr -s sif -f 5 |\")\r\n        or die \"Can't start pwcview: $!\";\r\n    binmode(PWCVIEW);\r\n    $sel-&gt;add(\\*PWCVIEW);\r\n}\r\n\r\nsub close_pwcview {\r\n    $sel-&gt;remove(\\*PWCVIEW);\r\n    close(PWCVIEW);\r\n}\r\n\r\nsub open_mencoder {\r\n    $now_string = strftime(\"%Y%m%d-%H%M%S\", localtime);\r\n    $filename = $dir. $now_string. \".avi\";\r\n    open(MENCODER, \"| \/usr\/local\/bin\/mencoder -demuxer rawvideo -rawvideo \".\r\n        \"fps=5:w=320:h=240:i420 - -ovc lavc -lavcopts vcodec=mpeg4 \".\r\n        \"-o \". $filename)\r\n        or die \"Can't start mencoder: $!\";\r\n    binmode(MENCODER);\r\n}\r\n\r\nsub close_mencoder {\r\n    close(MENCODER);\r\n}\r\n\r\nopen(PID, \"&gt;\". $dir. \"camera.pid\") or die \"Can't open pidfile: $!\";\r\nprint PID $$. \"\\n\";\r\nclose(PID);\r\n\r\nopen_pwcview();\r\nwhile (1) {\r\n    @ready = $sel-&gt;can_read(600);\r\n    if ($#ready &gt;= 0) {\r\n        if ($mcopen != 1) {\r\n            open_mencoder();\r\n            $mcopen = 1;\r\n        }\r\n        $bytes = ;\r\n        if (length($bytes) == 0) {\r\n            close_pwcview();\r\n            close_mencoder();\r\n            $mcopen=0;\r\n            open_pwcview();\r\n        } else {\r\n            print MENCODER $bytes;\r\n        }\r\n    } else {\r\n        close_mencoder();\r\n        $mcopen=0;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To do a motion recording camera with FreeBSD 8.1 you need to install multimedia\/pwcbsd and multimedia\/mencoder from ports. With these and a short perl script you can do a camera that records motion and splits them in to separate files when there is inactivity for 600s. #!\/usr\/local\/bin\/perl # # Author: Jan Melen-jan(at)melen.org # use POSIX [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/47"}],"collection":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":9,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.melen.org\/u\/jan\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}