Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active November 2, 2024 10:33
Show Gist options
  • Save endolith/2052778 to your computer and use it in GitHub Desktop.
Save endolith/2052778 to your computer and use it in GitHub Desktop.
How to stream a webcam to a web browser in Ubuntu

Grr this took hours to figure out. I was trying to install MJPG-streamer and running VLC command lines and all this crap but nothing worked.

First install motion:

~> sudo apt-get install motion

Then create a config file:

~> mkdir ~/.motion
~> nano ~/.motion/motion.conf

In it, the bare minimum to run a web server and view it on other computers:

webcam_port 8081
webcam_localhost off

Then run motion:

~> motion

Now you can view the webcam at http://hostname:8081 If it doesn't work, try rebooting between steps or something.

Isn't that easy? >:(

See also: How to run webcam software only when I am not home

@Ashok12698
Copy link

Hi @EVINK, thanks for your reply. I can see the stream log open at Board like this...
image
but when I try to see it using "http://xxx.xxx.x.xx:8080/", nothing gets open. Please suggest.

@beersteiner
Copy link

Anyone know how to make motion serve up single snapshots, on-demand, via URL? host:port/0/action/snapshot successfully results in generating the snapshot, saving it in /var/lib/motion, but it doesn't seem to actually serve up the resulting image (i.e. lastsnap.jpg) to the browser. This seems like a pretty basic feature, but I've searched a couple hours now and can't seem to find any evidence it is supported in motion.

@Barry-IA
Copy link

Barry-IA commented Jun 4, 2021

but when I try to see it using "http://xxx.xxx.x.xx:8080/", nothing gets open. Please suggest.

Ash-S:
Hopefully you have figured this out by now, but in case and for others I've found the port sometimes needs a 'slash_1' added, so :8080/1. I have one Motion device with two cameras and it needs :8081/1 and the other just wants :8082.

@tsyber1an
Copy link

tsyber1an commented Nov 6, 2021

as @EVINK said:

for motion's latest version for Ubuntu (Version 4.3.2), this is relevant configuration:

stream_port 8080
stream_localhost off

see more at: https://motion-project.github.io/motion_config.html

in addition, if you want to enable a web cam control:

webcontrol_port 8080
webcontrol_localhost off

also, configuring camera to its default resolution:

width 2560
height 1440

useful option, running in background

motion -b

all at ones for my camera:

stream_port 8081
stream_localhost off
webcontrol_port 8080
webcontrol_localhost off
width 2560 
height 144

@abdoaboganima
Copy link

stream_quality 98
stream_maxrate 5
stream_port 8080
stream_localhost off
output_pictures off
framerate 30
ffmpeg_video_codec mpeg4
http://192.168.0.34:8080/ozzz1.cgi
width 320
height 240
auto_brightness off
contrast 0
saturation 0

Thanks that works for me

@rivermonk
Copy link

rivermonk commented Sep 6, 2022

helpful discussion. using new lubuntu 22.04LTS install, got local motion recording to work properly (after setting some chown directory issues). also can access live video in browser with localhost:8081 and localhost:8080. what I want to do is access the USB cam live video remotely. I know I'll need to find the IP address and somehow set a password, but no idea how to do either. any clues? thanks

@assadollahi
Copy link

if you don't want to video write files on the harddrive, you should switch the ffmpeg_output_moives off. i also increased the framerate to 10 (for watching my 3D printer), and increased the resolution, works with Ubuntu 22.04.1 LTS (Jammy Jellyfish):

stream_quality 98
stream_maxrate 10
stream_port 8080
stream_localhost off
output_pictures off
ffmpeg_output_movies off
framerate 30
ffmpeg_video_codec mpeg4
width 1024
height 768

@Juan013
Copy link

Juan013 commented Oct 21, 2022

How can I change to /dev/video1?

@Barry-IA
Copy link

Barry-IA commented Oct 22, 2022 via email

@skwzrd
Copy link

skwzrd commented Jun 6, 2023

After messing about with VLC, go2rtc, and webcamd for days, motion simply just works with MINIMAL fuss.

Here is my motion.conf file:

stream_quality 98
stream_maxrate 5
stream_port 1984
stream_localhost off
output_pictures off
framerate 30
ffmpeg_video_codec mpeg
width 640
height 480
auto_brightness off
contrast 0
saturation 0

Here is my NGINX server

server {
    server_name DOMAIN;
    root /var/www/html;

    auth_basic "Admin"; # password protected domain
    auth_basic_user_file /etc/apache2/.htpasswd;

    location / {
        proxy_pass http://127.0.0.1:1984/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Prefix /;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/DOMAIN/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/DOMAIN/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment