1

I like in this article the different Python functions to control a Pi cameras. Can this be done with USB camera?

Or do I always need 3rd party softwares like fswebcam, ffmpeg, motion to do things with USB cameras?

(I really want a way to get to preview the photo I am taking. But motion doesn't have a that feature. So I want to try to make a Python program for that purpose.)

Here is my motion.conf for snapshot:

4
  • Suggest editing title to ask "How do I trigger on-demand phtoto from a USB video stream?" You've done the research. :D I can think of a horribly hacky way to get motion to do this, but it is truly horrible. Let's see what folks come up with.
    – OyaMist
    Jun 7, 2018 at 14:14
  • I actually found a link on taking snapshot on command using HTTP control but I don't understand how they input the command into the actual URL: lavrsen.dk/foswiki/bin/view/Motion/MotionHttpAPI Jun 7, 2018 at 18:49
  • I tried it and got a snapshot! OK. Let me write up an answer
    – OyaMist
    Jun 7, 2018 at 20:49
  • 1
    Yay! We did it? Congrats! :D
    – OyaMist
    Jun 8, 2018 at 20:40

1 Answer 1

4

The motion program can be used to capture a USB video-stream on a Raspberry Pi. It can also take snapshots on demand during a video-stream. Conceptually, motion provides two web services on separate ports that you configure in motion.conf. One web service provides the video stream. The other allows you to control motion itself.

Let's first edit motion.conf to specify a snapshot filename and location to take snapshots every hour. The timestamped snapshots will be files in /home/pi:

target_dir /home/pi
snapshot_filename %Y%m%d-%H%M%S-snap
snapshot_interval 3600
output_pictures off

Now we configure the two ports as follows in motion.conf:

stream_port 8081
stream_localhost off
webcontrol_port 8080
webcontrol_localhost off

Let's also assume that Pi is at IP address 192.X.X.X

With the above configuration, your videostream will be accessible from a browser at:

http://192.X.X.X:8081/

In addition, you can take a snapshot each time you fetch the following URL:

http://192.X.X.X:8080/1/action/snapshot

The above is the gist of what you need to do. Please take time to read the configuration parameters. They are quite numerous but you will soon find yourself experimenting with fun things like motion-triggered movies.

7
  • where is the snapshot stored inside the computer? What does 1 mean? Jun 8, 2018 at 13:28
  • The 1 in the url is the camera thread number. Pi can only really handle 1 web cam where other computers can handle more. So we always use 1.
    – OyaMist
    Jun 8, 2018 at 14:15
  • I tried it on the pi browser, because SSH PUTTY doesn't work for now. It said "Not Valid. The requested URL is not valid" Actually, there is a ""back"" button with an arrow. I clicked that. Then there is a "All" button. Then, "action"->"snapshot"->then it says "snapshot for thread 0 done." WHERE IS THE PHOTO? Jun 8, 2018 at 15:50
  • The timestamped photo should be on the Raspberry Pi. I've edited answer.
    – OyaMist
    Jun 8, 2018 at 16:04
  • I type 10.255.7.130:8080/0/action/snapshot. And then hundreds of snapshot start appearing in my folder, until I close the browser tab. How do I take 1 snapshot? Jun 8, 2018 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.