Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Noguai
Last active September 7, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noguai/f35547fec818da52f2be28b820f9cf8e to your computer and use it in GitHub Desktop.
Save Noguai/f35547fec818da52f2be28b820f9cf8e to your computer and use it in GitHub Desktop.
MJPEG Webcam Streaming with FFmpeg and FFserver
#/etc/systemd/system/ffmpeg.service
[Unit]
Description=FFMPEG transcoder service
After=ffserver.service
[Service]
User=pi
# -video_size and -framerate should match the settings in ffserver.conf
ExecStart=/usr/local/bin/ffmpeg -framerate 20 -video_size 1280x720 -input_format mjpeg -i /dev/video0 -codec copy -threads: 2 -thread_type slice -nostats -loglevel panic http://localhost:8080/camera.ffm
[Install]
WantedBy=multi-user.target
#/etc/ffserver.conf
HTTPPort 8080
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 100
MaxClients 100
MaxBandwidth 102400
CustomLog -
# Suppress that if you want to launch ffserver as a daemon.
#NoDaemon
<Feed camera.ffm>
File /vidtmp/camera.ffm
FileMaxSize 100M
ACL allow 127.0.0.1
ACL allow 192.168.0.0 192.168.255.255
</Feed>
<Stream camera.mjpg>
NoDefaults
Format mpjpeg
Feed camera.ffm
# Make sure frame rate and size
# match those passed to ffmpeg
VideoFrameRate 20
Videosize 1280x720
VideoBitRate 15360
# VideoBufferSize 102400
VideoQMin 3
VideoQMax 3
NoAudio
Strict -1
ACL allow 127.0.0.1
ACL allow 192.168.0.0 192.168.255.255
</Stream>
<Stream stat.html>
Format status
ACL allow localhost
ACL allow 192.168.0.0 192.168.255.255
</Stream>
#/etc/systemd/system/ffserver.service
[Unit]
Description=FFMPEG streaming server (ffserver) service
[Service]
User=pi
ExecStart=/usr/local/bin/ffserver -f /etc/ffserver.conf
[Install]
WantedBy=multi-user.target
$ ffmpeg -framerate 24 -video_size 1280x720 -input_format mjpeg -i /dev/video0 -codec copy -threads: 2 -thread_type slice http://localhost:8080/camera.ffm
ffmpeg version 3.4.6 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 8 (Raspbian 8.3.0-6+rpi1)
configuration: --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree --enable-omx --enable-omx-rpi
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
[mjpeg @ 0x3798610] EOI missing, emulating
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 35503.960220, bitrate: N/A
Stream #0:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 24 fps, 24 tbr, 1000k tbn, 1000k tbc
[tcp @ 0x379b780] Connection to tcp://localhost:8080 failed (Connection refused), trying next address
[tcp @ 0x37a7840] Connection to tcp://localhost:8080 failed (Connection refused), trying next address
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[mjpeg @ 0x3799860] overread 8
[mjpeg @ 0x3799860] EOI missing, emulating
Output #0, ffm, to 'http://localhost:8080/camera.ffm':
Metadata:
creation_time : now
encoder : Lavf57.83.100
Stream #0:0: Video: mjpeg, yuvj422p(pc), 1280x720, q=3-3, 15360 kb/s, 24 fps, 1000k tbn, 20 tbc
Metadata:
encoder : Lavc57.107.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/15360000 buffer size: 0 vbv_delay: -1
Past duration 0.998528 too large
frame= 12 fps=0.0 q=1.6 size= 232kB time=00:00:00.55 bitrate=3455.5kbits/s dup=0 drop=2 speed=1.09x
frame= 22 fps= 22 q=1.6 size= 424kB time=00:00:01.05 bitrate=3308.0kbits/s dup=0 drop=4 speed=1.03x
frame= 33 fps= 22 q=1.6 size= 636kB time=00:00:01.60 bitrate=3256.3kbits/s dup=0 drop=6 speed=1.06x
frame= 43 fps= 21 q=1.6 size= 828kB time=00:00:02.10 bitrate=3230.0kbits/s dup=0 drop=8 speed=1.04x
frame= 53 fps= 21 q=1.6 size= 1020kB time=00:00:02.60 bitrate=3213.8kbits/s dup=0 drop=10 speed=1.02x
frame= 63 fps= 21 q=1.6 size= 1212kB time=00:00:03.10 bitrate=3202.8kbits/s dup=0 drop=12 speed=1.02x
frame= 73 fps= 21 q=1.6 size= 1404kB time=00:00:03.60 bitrate=3194.9kbits/s dup=0 drop=14 speed=1.01x
frame= 84 fps= 21 q=1.6 size= 1612kB time=00:00:04.15 bitrate=3182.0kbits/s dup=0 drop=16 speed=1.02x
frame= 94 fps= 21 q=1.6 size= 1804kB time=00:00:04.65 bitrate=3178.1kbits/s dup=0 drop=18 speed=1.01x
frame= 104 fps= 20 q=1.6 size= 1996kB time=00:00:05.15 bitrate=3175.0kbits/s dup=0 drop=20 speed=1.01x
frame= 114 fps= 20 q=1.6 size= 2188kB time=00:00:05.65 bitrate=3172.4kbits/s dup=0 drop=22 speed=1.01x
frame= 116 fps= 20 q=1.6 Lsize= 2232kB time=00:00:05.75 bitrate=3179.9kbits/s dup=0 drop=23 speed=1.01x
video:2216kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.728503%
Exiting normally, received signal 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment