Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active October 21, 2024 03:35
Show Gist options
  • Save peterhellberg/ebfc72147c2009ee720aafe57ce9c141 to your computer and use it in GitHub Desktop.
Save peterhellberg/ebfc72147c2009ee720aafe57ce9c141 to your computer and use it in GitHub Desktop.
MJPEG stream from Webcam using FFServer and FFMpeg
HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 200
MaxClients 100
MaxBandWidth 500000
CustomLog -
<Feed camera.ffm>
File /tmp/camera.ffm
FileMaxSize 200M
</Feed>
<Stream camera.mjpeg>
Feed camera.ffm
Format mpjpeg
VideoFrameRate 15
VideoIntraOnly
VideoBitRate 4096
VideoBufferSize 4096
VideoSize 640x480
VideoQMin 5
VideoQMax 51
NoAudio
Strict -1
</Stream>
@peterhellberg
Copy link
Author

Starting the server

/usr/local/bin/ffserver -f ffserver.conf

Starting FFMpeg (under macOS)

ffmpeg -f avfoundation -framerate 15 -pixel_format yuyv422 -i "0" http://localhost:8090/camera.ffm

Play in VLC (⌘N – Open Network)

http://localhost:8090/camera.mjpeg

@junjuew
Copy link

junjuew commented Dec 14, 2017

Starting ffmpeg under linux:
ffmpeg -f video4linux2 -s 640x480 -r 15 -i /dev/video0 http://localhost:8090/camera.ffm

@khazzz
Copy link

khazzz commented Jan 22, 2018

Thank you for posting your ffserver.conf file. May I ask you a question please:

How did you decide to use a value for MaxBandWidth set to 500000? And how does it relate to:
VideoBitRate 4096
VideoBufferSize 4096

As I understand all these values are connected and can be tuned. So why did you choose these particular values?
Thank you in advance!

@imnx
Copy link

imnx commented Mar 26, 2018

How did you decide to use a value for MaxBandWidth set to 500000?

Try this:
man ffmpeg or try man ffserver
... ;)

Pay it forward.

@imnx
Copy link

imnx commented Mar 26, 2018

A little bash script that does the job for me, for your consideration...

#!/bin/bash
echo 'HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 200
MaxClients 100
MaxBandWidth 500000
CustomLog -

<Feed camera.ffm>
   File /tmp/camera.ffm
   FileMaxSize 200M
</Feed>

<Stream camera.mpeg>
Feed camera.ffm
Format mpeg
VideoFrameRate 25
#VideoIntraOnly
VideoBitRate 4096
VideoBufferSize 4096
VideoSize hd720
VideoQMin 5
VideoQMax 51
#NoAudio
Strict -1
</Stream>' > ${PWD}/.ffserver.conf
(ffserver -f ${PWD}/.ffserver.conf > /dev/null)&
(ffmpeg -s `1280x720 -f` v4l2 -i /dev/video0 -ac 1 -f alsa -i hw:2,0 http://localhost:8090/camera.ffm)&
(rm ${PWD}/.ffserver.conf)&

Just do a chmod +x script.file and ./script.file .

@alexschwarz89
Copy link

alexschwarz89 commented Dec 30, 2018

I can't get audio to work. There is some noise for about 1 second and then it stops. And video does have a huge delay (30sec+?)

Device is an raspberry pi with an external logitech webcam.

Would be very thankful for any idea why.

I try to play it via VLC player

arecord -l
**** Liste der Hardware-Geräte (CAPTURE) **** Karte 1: U0x46d0x825 [USB Device 0x46d:0x825], Gerät 0: USB Audio [USB Audio] Sub-Geräte: 1/1 Sub-Gerät #0: subdevice #0

ffmpeg call
ffmpeg -f alsa -ac 1 -i hw:1,0 -f v4l2 -s 1280x720 -i /dev/video0 http://localhost:8090/camera.ffm

ffserver.conf

# port from your standard HTTP web server if it is running on the same
# computer.
HTTPPort 8090

# Address on which the server is bound. Only useful if you have
# several network interfaces.
HTTPBindAddress 0.0.0.0

# Number of simultaneous HTTP connections that can be handled. It has
# to be defined *before* the MaxClients parameter, since it defines the
# MaxClients maximum limit.
MaxHTTPConnections 2000

# Number of simultaneous requests that can be handled. Since FFServer
# is very fast, it is more likely that you will want to leave this high
# and use MaxBandwidth, below.
MaxClients 100

# This the maximum amount of kbit/sec that you are prepared to
# consume when streaming to clients.
MaxBandwidth 500000

# Access log file (uses standard Apache log file format)
# '-' is the standard output.
CustomLog -

##################################################################
# Definition of the live feeds. Each live feed contains one video
# and/or audio sequence coming from an ffmpeg encoder or another
# ffserver. This sequence may be encoded simultaneously with several
# codecs at several resolutions.

<Feed camera.ffm>

# You must use 'ffmpeg' to send a live feed to ffserver. In this
# example, you can type:
#
# ffmpeg http://localhost:8090/feed1.ffm

# ffserver can also do time shifting. It means that it can stream any
# previously recorded live stream. The request should contain:
# "http://xxxx?date=[YYYY-MM-DDT][[HH:]MM:]SS[.m...]".You must specify
# a path where the feed is stored on disk. You also specify the
# maximum size of the feed, where zero means unlimited. Default:
# File=/tmp/feed_name.ffm FileMaxSize=5M
File /tmp/feed1.ffm
FileMaxSize 200M

# You could specify
# ReadOnlyFile /saved/specialvideo.ffm
# This marks the file as readonly and it will not be deleted or updated.

# Specify launch in order to start ffmpeg automatically.
# First ffmpeg must be defined with an appropriate path if needed,
# after that options can follow, but avoid adding the http:// field
#Launch ffmpeg

# Only allow connections from localhost to the feed.
#ACL allow 127.0.0.1

</Feed>


##################################################################
# Now you can define each stream which will be generated from the
# original audio and video stream. Each format has a filename (here
# 'test1.mpg'). FFServer will send this stream when answering a
# request containing this filename.

<Stream camera.mpeg>

# coming from live feed 'feed1'
Feed camera.ffm

# Format of the stream : you can choose among:
# mpeg       : MPEG-1 multiplexed video and audio
# mpegvideo  : only MPEG-1 video
# mp2        : MPEG-2 audio (use AudioCodec to select layer 2 and 3 codec)
# ogg        : Ogg format (Vorbis audio codec)
# rm         : RealNetworks-compatible stream. Multiplexed audio and video.
# ra         : RealNetworks-compatible stream. Audio only.
# mpjpeg     : Multipart JPEG (works with Netscape without any plugin)
# jpeg       : Generate a single JPEG image.
# mjpeg      : Generate a M-JPEG stream.
# asf        : ASF compatible streaming (Windows Media Player format).
# swf        : Macromedia Flash compatible stream
# avi        : AVI format (MPEG-4 video, MPEG audio sound)
Format mpeg

#AudioCodec mp3
# Bitrate for the audio stream. Codecs usually support only a few
# different bitrates.
#AudioBitRate 64

# Number of audio channels: 1 = mono, 2 = stereo
AudioChannels 1

# Sampling frequency for audio. When using low bitrates, you should
# lower this frequency to 22050 or 11025. The supported frequencies
# depend on the selected audio codec.
#AudioSampleRate 44100

# Bitrate for the video stream
VideoBitRate 4096

# Ratecontrol buffer size
VideoBufferSize 4096

VideoSize hd720

VideoQMin 5
VideoQMax 51

# Number of frames per second
VideoFrameRate 25

# Size of the video frame: WxH (default: 160x128)
# The following abbreviations are defined: sqcif, qcif, cif, 4cif, qqvga,
# qvga, vga, svga, xga, uxga, qxga, sxga, qsxga, hsxga, wvga, wxga, wsxga,
# wuxga, woxga, wqsxga, wquxga, whsxga, whuxga, cga, ega, hd480, hd720,
# hd1080
#VideoSize 640x480

# Transmit only intra frames (useful for low bitrates, but kills frame rate).
#VideoIntraOnly

# If non-intra only, an intra frame is transmitted every VideoGopSize
# frames. Video synchronization can only begin at an intra frame.
#VideoGopSize 25

# More MPEG-4 parameters
# VideoHighQuality
# Video4MotionVector

# Choose your codecs:
#AudioCodec mp2
#VideoCodec mpeg1video

# Suppress audio
#NoAudio

# Suppress video
#NoVideo

#VideoQMin 1
#VideoQMax 51

Strict -1
</Stream>

# Server status

<Stream stat.html>
Format status

# Only allow local people to get the status
ACL allow localhost
ACL allow 192.168.0.0 192.168.255.255

#FaviconURL http://pond1.gladstonefamily.net:8080/favicon.ico
</Stream>


# Redirect index.html to the appropriate site

<Redirect index.html>
URL http://www.ffmpeg.org/
</Redirect>

@BakytzhanAkzhol
Copy link

BakytzhanAkzhol commented Feb 8, 2019

Hello, I have problem with broadcasting video from webcamera to my server.
It's failed when try to run broadcast to server.

Technical info:

Client-site:Windows 10

Server-site: Ubuntu 16.04

ffserver.conf

HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 500000
CustomLog -
NoDaemon
RTSPPort 7654
RTSPBindAddress 0.0.0.0
<Feed feed1.ffm>               # This is the input feed where FFmpeg will send
   File ./feed1.ffm            # video stream.
   FileMaxSize 1G              # Maximum file size for buffering video
   ACL allow 127.0.0.1         # Allowed IPs
</Feed>

<Stream camera.mjpeg>
	Feed feed1.ffm
	Format webm
	# Audio settings
	AudioCodec vorbis
	AudioBitRate 64             # Audio bitrate

	# Video settings
	VideoCodec libvpx
	VideoSize 640x480          # Video resolution
	VideoFrameRate 30           # Video FPS
	AVOptionVideo flags +global_header  # Parameters passed to encoder
	                                   # (same as ffmpeg command-line parameters)
	AVOptionVideo cpu-used 0
	AVOptionVideo qmin 10
	AVOptionVideo qmax 42
	AVOptionVideo quality good
	AVOptionAudio flags +global_header
	PreRoll 15
	StartSendOnKey
	VideoBitRate 400 
</Stream>

<Stream status.html>
  Format status
  # Only allow local people to get the status
  ACL allow localhost
  ACL allow 192.168.0.0 192.168.255.255
</Stream>

<Redirect index.html>    # Just an URL redirect for index
   # Redirect index.html to the appropriate site
   URL http://XX.XXX.XXX.XXX
</Redirect>

Run code on Windows:

ffmpeg -f dshow -i video="USB2.0 HD UVC WebCam" -video_size 640x480 -rtbufsize 702000k -framerate 30 -pixel_format yuyv422 -f mpegts http://XXX.XXX.XX.XX:8090/feed1.ffm

Error message on Windows:

ffmpeg version N-92730-g978c935f2f Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181201
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 24.101 / 56. 24.101
  libavcodec     58. 42.103 / 58. 42.103
  libavformat    58. 24.101 / 58. 24.101
  libavdevice    58.  6.101 / 58.  6.101
  libavfilter     7. 46.101 /  7. 46.101
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Input #0, dshow, from 'video=USB2.0 HD UVC WebCam':
  Duration: N/A, start: 5326.566000, bitrate: N/A
    Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 10000k tbn, 10000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mpeg2video (native))
Press [q] to stop, [?] for help
[swscaler @ 06e5a000] deprecated pixel format used, make sure you did set range correctly
Output #0, mpegts, to 'http://95.183.11.180:8090/feed1.ffm':
  Metadata:
    encoder         : Lavf58.24.101
    Stream #0:0: Video: mpeg2video (4:2:2), yuv422p, 1280x720, q=2-31, 200 kb/s, 30 fps, 90k tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.42.103 mpeg2video
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
av_interleaved_write_frame(): Unknown error
Error writing trailer of http://95.183.11.180:8090/feed1.ffm: Error number -10054 occurred
frame=    3 fps=0.0 q=1.6 Lsize=      46kB time=00:00:00.00 bitrate=34318545.5kbits/s speed=9.07e-05x
video:84kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[http @ 06dd5c00] URL read error:  -10054
Conversion failed!

FFserver log:

Fri Feb  8 03:14:58 2019  - [POST] "/feed1.ffm HTTP/1.1" 200 4096

@lanbin45
Copy link

@BakytzhanAkzhol I've met same error when publishing webcam to ffserver on windows 10. Did you solve this?

@YasirKubarah
Copy link

Is the source code of ffserver still available ?

@peterhellberg
Copy link
Author

@YasirKubarah Yes, in commit 2ca65fc or the 3.4 release branch, as noted on https://trac.ffmpeg.org/wiki/ffserver

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