webcam과 ffserver를 이용한 실시간 스트리밍 환경 구축하기

webcam과 ffserver를 이용한 실시간 스트리밍 환경 구축하기

작성자 : 서진우
작성일 : 2011년 3월 10일


앞서 webcam과 xawtv 의 image 캡처 파일 자동 전송 기능을 이용하여 웹 브라우저 상에서
영상을 실시간으로 확인하는 방법에 대해 설명한적이 있다.

이번에 구축 방법은 기존의 image reflash 방식의 실시간 영상 확인 방법이 아니라 고해상도의 영상을 바로 ffmpeg로 인코딩하고, 이것을  ffserver로 스트리밍 하는 방법에 대해 설명하고 있다.

xawtv를 이용한 방법은 저사양의 서버를 통해 영상의 품질보다 기능 구현 측면만을 고려할 경우 매우 괜찮은 방법 중에 하나이다.

본 글에서 설명하는 방법은 나름 고사양의 컴퓨터 환경과 고해상도의 CAM을 보유한 경우,
해당 CAM에서 촬용하는 영상을 웹상에서 실시간으로 확인하고자 할때 유용한 방법 중 하나이다.

본 문서에는 스트리밍 서비스 이외에 동영상 확인, 동영상 저장, 동영상의 이미지 캡쳐 등의
방법에 대해서도 간단히 설명하고 있다.

이후 본 문서에서 설명한 deploy 내용으로 간단한 웹 프로그래밍을 통해 NVR 솔루션을 만들어 볼  계획이다.


– webcam device select


Webcam Model : ALC-M1000


danawa 에 hit 1 상품으로 25,000원 정도의 저렴한 webcam 임.
해상도는 최대 4000×3000 지원
video 최대 화소 : 200만 화소
image 최대 화소 : 1000만 화소 


– OS and FFmpeg


OS : RHEL 5 update5
FFmpeg : ffmpeg-0.6.1-1.el5.rf


– driver install


UVC driver 지원으로 별도의 driver 설치 없이 장치 인식이 됨.


# lsmod | grep video
video                  53197  0
backlight              39873  1 video
uvcvideo               87497  0
compat_ioctl32         41665  1 uvcvideo
videodev               58689  1 uvcvideo
v4l1_compat            44613  2 uvcvideo,videodev
v4l2_common            57153  3 uvcvideo,compat_ioctl32,videodev



-live view
$ mplayer -cache 128 -vo xv -x 1920 -y 1200 tv://


-image capture
$ ffmpeg -f video4linux2 -s 1600×1200 -i /dev/video0 -vframes 1 -ss 00:00:01 -an -vcodec png -f rawvideo test.jpg


– video record
$ ffmpeg -f video4linux2 -s 800×600 -i /dev/video0 -r 30 -vcodec mpeg4 -vtag xvid -sameq -f alsa -i plughw:1 -ar 22050 -ab 128k -acodec libmp3lame output.avi


$ ffmpeg -f video4linux2 -s 320×240 -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -pass 1/2 -r 30 -i /dev/video0 -f alsa -i plughw:0 -ar 22050 -ab 64k -acodec libmp3lame -f mp4 webcam.mp4


– video streaming


vi /etc/ffserver.conf
————————————————–


Port 8090
# bind to all IPs aliased or not
BindAddress 0.0.0.0
# max number of simultaneous clients
MaxClients 1000
# max bandwidth per-client (kb/s)
MaxBandwidth 10000
# Suppress that if you want to launch ffserver as a daemon.
NoDaemon


<Feed feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 100M
</Feed>


<Feed feed2.ffm>
File /tmp/feed2.ffm
FileMaxSize 10M
</Feed>



<Stream mydesk.flv>
Feed feed1.ffm
Format flv
VideoCodec flv
VideoFrameRate 20
VideoBufferSize 80000
VideoBitRate 100
VideoQMin 1
VideoQMax 3
VideoSize 640×480
PreRoll 15
AudioBitRate 32
#Noaudio
</Stream>


<Stream mydesk.asf>
Feed feed2.ffm
Format asf
VideoFrameRate 20
VideoBufferSize 80000
VideoBitRate 200
VideoQMin 1
VideoQMax 3
VideoSize 640×480
PreRoll 0
Noaudio
</Stream>


<Stream stat.html>
Format status
ACL allow localhost
ACL allow 192.168.123.0 192.168.123.255
</Stream>


———————————————————————-


$ ffserver -f /etc/ffserver.conf


– flv streaming


$ ffmpeg -f video4linux2 -s 640×480 -i /dev/video0 -r 20 -vcodec mpeg4 -vtag xvid -sameq -f alsa -i plughw:2 -ar 22050 -ab 32k -acodec libmp3lame http://localhost:8090/feed1.ffm


– asf streaming


$ ffmpeg -f video4linux2 -s 640×480 -i /dev/video0 -r 20 -vcodec mpeg4 -vtag xvid -sameq http://localhost:8090/feed2.ffm


$ mplayer http://localhost:8090/mydesk.flv


http://localhost:8090/mydesk.asf



– ffserver.conf example
————————————————————————
Port 8090
# bind to all IPs aliased or not
BindAddress 0.0.0.0
# max number of simultaneous clients
MaxClients 1000
# max bandwidth per-client (kb/s)
MaxBandwidth 10000
# Suppress that if you want to launch ffserver as a daemon.
NoDaemon


<Feed feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 5M
</Feed>


# FLV output – good for streaming
<Stream test.flv>
# the source feed
Feed feed1.ffm
# the output stream format – FLV = FLash Video
Format flv
VideoCodec flv
# this must match the ffmpeg -r argument
VideoFrameRate 15
# generally leave this is a large number
VideoBufferSize 80000
# another quality tweak
VideoBitRate 200
# quality ranges – 1-31 (1 = best, 31 = worst)
VideoQMin 1
VideoQMax 5
VideoSize 352×288
# this sets how many seconds in past to start
PreRoll 0
# wecams don’t have audio
Noaudio
</Stream>


# ASF output – for windows media player
<Stream test.asf>
# the source feed
Feed feed1.ffm
# the output stream format – ASF
Format asf
VideoCodec msmpeg4
# this must match the ffmpeg -r argument
VideoFrameRate 15
# generally leave this is a large number
VideoBufferSize 80000
# another quality tweak
VideoBitRate 200
# quality ranges – 1-31 (1 = best, 31 = worst)
VideoQMin 1
VideoQMax 5
VideoSize 352×288
# this sets how many seconds in past to start
PreRoll 0
# wecams don’t have audio
Noaudio
</Stream>
————————————————————————–


————————————————————————–
# Port on which the server is listening. You must select a different
# port from your standard HTTP web server if it is running on the same
# computer.
Port 8090


# Address on which the server is bound. Only useful if you have
# several network interfaces.
BindAddress 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 1000


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


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


# Suppress that if you want to launch ffserver as a daemon.
NoDaemon



##################################################################
# 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 feed1.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 2000M


# 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 test1.mpg>


# coming from live feed ‘feed1’
Feed feed1.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.
# asf        : ASF compatible streaming (Windows Media Player format).
# swf        : Macromedia Flash compatible stream
# avi        : AVI format (MPEG-4 video, MPEG audio sound)
Format mpeg


# Bitrate for the audio stream. Codecs usually support only a few
# different bitrates.
AudioBitRate 32
#NoAudio


# 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 200


# Ratecontrol buffer size
#VideoBufferSize 40
# Number of frames per second
VideoFrameRate 30


# Size of the video frame: WxH (default: 160×128)
# 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 640×480


# 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 12


# More MPEG-4 parameters
# VideoHighQuality
# Video4MotionVector


# Choose your codecs:
#AudioCodec mp2
#VideoCodec mpeg1video


# Suppress audio
NoAudio


# Suppress video
#NoVideo


#VideoQMin 3
#VideoQMax 31


# Set this to the number of seconds backwards in time to start. Note that
# most players will buffer 5-10 seconds of video, and also you need to allow
# for a keyframe to appear in the data stream.
PreRoll 15


# ACL:


# You can allow ranges of addresses (or single addresses)
#ACL ALLOW <first address> <last address>


# You can deny ranges of addresses (or single addresses)
#ACL DENY <first address> <last address>


# You can repeat the ACL allow/deny as often as you like. It is on a per
# stream basis. The first match defines the action. If there are no matches,
# then the default is the inverse of the last ACL statement.
#
# Thus ‘ACL allow localhost’ only allows access from localhost.
# ‘ACL deny 1.0.0.0 1.255.255.255’ would deny the whole of network 1 and
# allow everybody else.


</Stream>



##################################################################
# Example streams



# Multipart JPEG


#<Stream test.mjpg>
#Feed feed1.ffm
#Format mpjpeg
#VideoFrameRate 2
#VideoFrameRate 30
#VideoIntraOnly
#NoAudio
#Strict -1
#</Stream>



# Single JPEG


#<Stream test.jpg>
#Feed feed1.ffm
#Format jpeg
#VideoFrameRate 2
#VideoFrameRate 30
#VideoIntraOnly
#VideoSize 320×240
#NoAudio
#Strict -1
#</Stream>


# Flash


#<Stream test.swf>
#Feed feed1.ffm
#Format swf
#VideoFrameRate 2
#VideoIntraOnly
#NoAudio
#</Stream>



# ASF compatible


<Stream test.asf>
Feed feed1.ffm
Format asf
VideoFrameRate 30
VideoSize 320×240
VideoBitRate 200
#VideoBufferSize 40
VideoGopSize 30
#AudioBitRate 64
NoAudio
StartSendOnKey
</Stream>



# MP3 audio


#<Stream test.mp3>
#Feed feed1.ffm
#Format mp2
#AudioCodec mp3
#AudioBitRate 64
#AudioChannels 1
#AudioSampleRate 44100
#NoVideo
#</Stream>



# Ogg Vorbis audio


#<Stream test.ogg>
#Feed feed1.ffm
#Title “Stream title”
#AudioBitRate 64
#AudioChannels 2
#AudioSampleRate 44100
#NoVideo
#</Stream>



# Real with audio only at 32 kbits


#<Stream test.ra>
#Feed feed1.ffm
#Format rm
#AudioBitRate 32
#NoVideo
#NoAudio
#</Stream>



# Real with audio and video at 64 kbits


#<Stream test.rm>
#Feed feed1.ffm
#Format rm
#AudioBitRate 32
#VideoBitRate 128
#VideoFrameRate 25
#VideoGopSize 25
#NoAudio
#</Stream>



##################################################################
# A stream coming from a file: you only need to set the input
# filename and optionally a new format. Supported conversions:
#    AVI -> ASF


#<Stream file.rm>
#File “/usr/local/httpd/htdocs/tlive.rm”
#NoAudio
#</Stream>


#<Stream file.asf>
#File “/usr/local/httpd/htdocs/test.asf”
#NoAudio
#Author “Me”
#Copyright “Super MegaCorp”
#Title “Test stream from disk”
#Comment “Test comment”
#</Stream>



##################################################################
# RTSP examples
#
# You can access this stream with the RTSP URL:
#   rtsp://localhost:5454/test1-rtsp.mpg
#
# A non-standard RTSP redirector is also created. Its URL is:
#  
http://localhost:8090/test1-rtsp.rtsp


#<Stream test1-rtsp.mpg>
#Format rtp
#File “/usr/local/httpd/htdocs/test1.mpg”
#</Stream>



##################################################################
# SDP/multicast examples
#
# If you want to send your stream in multicast, you must set the
# multicast address with MulticastAddress. The port and the TTL can
# also be set.
#
# An SDP file is automatically generated by ffserver by adding the
# ‘sdp’ extension to the stream name (here
#
http://localhost:8090/test1-sdp.sdp). You should usually give this
# file to your player to play the stream.
#
# The ‘NoLoop’ option can be used to avoid looping when the stream is
# terminated.


#<Stream test1-sdp.mpg>
#Format rtp
#File “/usr/local/httpd/htdocs/test1.mpg”
#MulticastAddress 224.124.0.1
#MulticastPort 5000
#MulticastTTL 16
#NoLoop
#</Stream>



##################################################################
# Special streams


# 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>


————————————————————————————


video 와 audio 모두 streaming 할 경우 5~10 간격으로 sync 문제로 buffering 이 일어나는 증세가 있다. 이는 ffserver.conf 의 preroll 설정에 15 값을 넣고, stream video 을 볼때 잠시 중단했다가 다시 시작하면 어느 정도 증세를 완화 시킬 수 있다.


이제 웹 브라우저에서 표시하는 방법이다. flv 포맷의 영상의 경우 flowplayer를 이용하면 된다.


flv 포맷 영상을 웹 브라우저에서 보는 방법은 앞서 올려둔  http://blog.syszone.co.kr/2719 (리눅스 기반 VOD 동영상 스트리밍 서비스 환경 구축) 문서를 참고하면 된다.


asf 포맷의 영상을 웹브라우저에서 확인하기 위해서는 아래와 같은 페이지를 만든다.





<table board=0 cellpadding=0 cellspacing=0  width=640 height=480>
<tr><td background=”mydesk.jpg”>
<OBJECT ID=”WMPlay” WIDTH=”640″ HEIGHT=”480″
                           classid=”clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95″

         CODEBASE=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab
         standby=”Loading Microsoft?Windows? Media Player components…”
    type=”application/x-oleobject”>
         <param name=”Filename” value=”http://server_ip:8090/mydesk.asf”>
         <param name=”ShowControls” value=”0″>
         <param name=”ShowPositionControls” value=”0″>
         <param name=”ShowGotoBar” value=”0″>
         <param name=”ShowDisplay” value=”0″>
         <param name=”ShowCaptioning” value=”0″>
         <param name=”ShowStatusBar” value=”0″>
         <param name=”AutoStart” value=”1″>
         <param name=”EnableContextMenu” value=”0″>
         <param name=”AnimationAtStart” value=”1″>
         <param name=”TransparentAtStart” value=”1″>
         <embed type=”application/x-mplayer2″>
</OBJECT>


</td></tr></table>




이제 윈도우 익스플로러에서 웹캠의 영상을 실시간으로 확인할 수 있다. 만일 운영체제 환경이 리눅스일 경우 mplayerplug-in 을 설치하면, 리눅스 웹 브라우저에서도 확인할 수 있다.


윈도우와 리눅스 모두에서 기본적으로 확인 가능하고자 할때는 flv 영상 포맷을 이용하면 된다.


사용자 삽입 이미지사용자 삽입 이미지












서진우

슈퍼컴퓨팅 전문 기업 클루닉스/ 상무(기술이사)/ 정보시스템감리사/ 시스존 블로그 운영자

You may also like...

페이스북/트위트/구글 계정으로 댓글 가능합니다.