ffmpegでライブカメラを楽しもう。

ffmpegでライブストリーミングサーバを作ってライブカメラを楽しもう。

USBカメラとffmepgを使って簡単お手軽、ライブストリーミングサーバを作ってみよう。
まず、USBカメラを認識させるのですが、ここでは割愛させていただきます。
今回はドライバーを新たに入れず認識できる「ELECOM UCAM-C1C30SV」を選びました。
内蔵されているチップが「OV510」というものなので、これなら大体のLinuxなら認識するのではないでしょうか?
【2007年1月更新:swf形式を追加】
【2008年4月更新:SVN形式のダウンロードとコンパイル方法・UVC対応USBカメラetc...】

ffmpegのインストール方法

ffmepgのソースをhttp://ffmpeg.sourceforge.net/から入手します。執筆時点ではver 0.4.8です。

まずはソースの展開とインストールです。展開は適当な場所で行ってください。

# tar zxfv ffmpeg-0.4.8.tar.gz
# cd ffmpeg-0.4.8
# ./configure
# make
# make install

【追記】かなり前からSVNでのダウンロードになったようです。

#svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
# cd ffmpeg
# ./configure
# make
# make install
オプション-r 【リビジョンコード】を指定することにより該当のリビジョンをダウンロードできます。
#svn checkout -r 9133 svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

インストールが終わると/usr/local/bin に ffmpeg, ffplay, ffserver がコピーされます。

ffmpeg-0.4.7/doc に ffserver.conf の雛形があるので、それを/usr/local/etc/ffserver.conf へコピーして編集します。

#cp ffserver.conf /usr/local/etc/ffserver.conf
#vi /usr/local/etc/ffserver.conf

ffserver.confの設定内容です。設定に必要な部分だけを引用してあります。
下記の設定では、WindowsMediaPlayerとRealPlayer、JPEG形式を出力するように設定してあります。
音声は省いております。

# port from your standard http web server if it is running on the same
# computer.
Port 8070 ←ffmpegを利用するポート番号 

# Address on which the server is bound. Only useful if you have
# several network interfaces.
BindAddress 0.0.0.0 ←確信がないので説明省きます。デフォルトでOK

# Number of simultaneous requests that can be handled. Since FFServer
# is very fast, this limit is determined mainly by your Internet
# connection speed.
MaxClients 10 ←クライアントの最大接続台数

# This the maximum amount of kbit/sec that you are prepared to
# consume when streaming to clients
MaxBandwidth 1000 ←毎秒あたりの最大転送ビット

# Access Log file (uses standard Apache log file format)
# '-' is the standard output
CustomLog /usr/local/apache2/logs/livecam_log 
↑ファイル名を指定するとアクセスログが取れます

# Suppress that if you want to launch ffserver as a daemon
NoDaemon
↑デーモンで起動しないので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 (100M bytes here). Default:
# File=/tmp/feed_name.ffm FileMaxSize=5M
File /tmp/feed1.ffm
FileMaxSize 1024K

# Specify launch in order to start ffmpeg automatically
# Launch

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

</Feed>

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

JPEGファイルの設定
# Single JPEG

<Stream smp.jpg>
Feed feed1.ffm
Format jpeg
VideoFrameRate 2
VideoIntraOnly
VideoSize 320x240
NoAudio
< /Stream>

WindowsMediaPlayer形式の設定(300kbit)
# ASF compatible

<Stream smp.asf>
Feed feed1.ffm
Format asf
VideoFrameRate 15
VideoSize 320x240
VideoBitRate 300
VideoGopSize 50
NoAudio
StartSendOnKey
NoAudio
Author "KumaNeko"
Copyright "Kuma-neko ko-bo-"
Title "Kawanutsu Live"
Comment "Live Camera"

</Stream>

######################################

# Real with audio and video at 64 kbits

RealPlayer形式の設定 (128kbit)
<Stream smp.rm>
Feed feed1.ffm
Format rm
#AudioBitRate 32
VideoBitRate 128
VideoFrameRate 15
VideoGopSize 25
VideoSize 320x240
NoAudio
< /Stream>

#######################################

Flashの設定 ※FlashPlayer8までは正常に動作を確認

<Stream test.swf>
Feed feed1.ffm
Format swf
VideoFrameRate 8
VideoSize 320x240
VideoBitRate 160
VideoIntraOnly
NoAudio
</Stream>

#######################################

# Special streams

# Server status

ffmepgの動作ステータス設定
この場合 http://[servername]:8090/stat.html/でストーリミングステータスが表示される
<Stream stat.html>
Format status

# Only allow local people to get to the status
ACL allow localhost
ACL allow 192.168.0.0 192.168.255.255
#ACL deny
#FaviconURL http://pond1.gladstonefamily.net:8080/favicon.ico
< /Stream>


# Redirect index.html to the appropriate site

<Redirect index.html>
URL http://ffmpeg.sourceforge.net/
< /Redire>

ffmpegを実行してみましょう。

/usr/local/bin/ffserver -f /usr/local/etc/ffserver.conf &
/usr/local/bin/ffmpeg -s 320x240 -vd /dev/video0 http:localhost:8070/feed1.ffm
※ffserverはバックグラウンドで動作させます。
【追記】最近は上記では起動できなくなりました。 -vdオプションが使えません。
/usr/local/bin/ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 /tmp/test.ffm
-f オプションはrawvideo video4linux video4linux2の3つになるのかな?

メディアプレイヤーで動作確認をしてみましょう。
メディアプレイやーの<ファイル> - <URLを開く>で http://www.kuma-neko.jp:8070/test.asfを入力しOKボタンを押しましょう。映像が見れたらOKです。

以上でLinuxでの設定は完了となります。
ffmpegを使った当HPです。【http://papanda.jp/livecam/】

【追記:おまけ】
LinuxでもUVC対応USBカメラが使えるようになりました。
#svn checkout svn://svn.berlios.de/linux-uvc/linux-uvc/trunk
#cd trunk
#make
#make install
以上でUVCドライバがインストールされます。
下記、UVC対応USBカメラを買い接続をしたところ認識されることを確認しました。
ELECOM UCAM-DLM130HWH マニュアルフォーカス
シグマ UVCA130AFWH オートフォーカス
シグマのUSBカメラですがオートフォーカス機能もバッチリ動作します。

次に出力されるストリーミングファイルをホームページで見れるようにメタファイルを作成してみましょう。

 

 

ぱぱんだサイト > コンピューター > ffmpegでライブストリーミングサーバを作ってみよう。