| Linux活用レシピ > 家電の制作 > RaspberryPiでライブストリーミング環境を構成 | |
|
このページでは、「RaspberryPi」と、それで動作するOS「Raspbian」で、安価にリアルタイム動画を配信する環境(ライブストリーミング環境)を構成するレシピを紹介します。 |
|
はじめに └ 説明 準備 ├ ハードウェア ├ Raspbian日本語 ├ 配信サーバ └ 無線設定 動画配信 ├ ライブ配信 ├ YouTubeLive └ LiveTool 製品 このレシピで利用した「ラズベリーパイ(Raspberry Pi)OS Raspbian 日本語版(LibreOffice付) 16G」\1900 は以下の販売チャネルで。 |
◆RaspberryPiを使ったライブストリーミング環境の製作◆ |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
1. パッケージインストール 2. Webサーバー設定 3. デバイス確認 4. ライブストリーミング用の環境設定 5. ストリーミングの開始・停止スクリプト 6. ストリーミングの開始と確認そして停止 7. BASIC認証をかける 8. マイク設定 |
◆ 1. パッケージインストール ◆
デスクトップ画面の上のメニューから以下の通り赤枠の部分をクリックしてコンソールを起動します。

以下のコマンドで、リポジトリを最新にします。
pi@raspberrypi:~ $ sudo apt-get update[ENTER] |
次に、以下のコマンドで、今回Webサーバーを構成する「nginx」はじめ、必要なモジュールをインストールします。
pi@raspberrypi:~ $ sudo apt install nginx php-fpm \ libnginx-mod-rtmp apache2-utils ffmpeg [ENTER] |
コンソールが返ってくれば必要モジュールをインストールは完了です。
◆ 2. Webサーバー設定 ◆
まずは、以下のコマンドでnginxのバージョンを確認し、問題なくインストールされているか確認します。
pi@raspberrypi:~ $ nginx -v[ENTER] |
nginx version: nginx/1.14.2 |
次に、以下のコマンドでnginxが動作していることを確認します。
pi@raspberrypi:~ $ systemctl status nginx[ENTER] |
|
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en Active: active (running) since Sun 2020-03-01 16:30:33 JST; 2min 27s ago Docs: man:nginx(8) Main PID: 6654 (nginx) Tasks: 5 (limit: 2200) Memory: 4.8M CGroup: /system.slice/nginx.service ├─6654 nginx: master process /usr/sbin/nginx -g daemon on; master_pro ├─6655 nginx: worker process ├─6656 nginx: worker process ├─6657 nginx: worker process └─6658 nginx: worker process 3月 01 16:30:33 raspberrypi systemd[1]: Starting A high performance web server 3月 01 16:30:33 raspberrypi systemd[1]: Started A high performance web server a |
次に、nginx のPHP設定を行います。
まず、既存設定をバックアップし、編集していきます。
pi@raspberrypi:~ $ sudo cp -ip /etc/nginx/sites-available/default \ /etc/nginx/sites-available/default.old[ENTER] pi@raspberrypi:~ $ sudo vi /etc/nginx/sites-available/default[ENTER] |
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
|
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
|
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
|
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
|
#location ~ /\.ht {
# deny all;
#}
|
location ~ /\.ht {
deny all;
}
|
pi@raspberrypi:~ $ sudo systemctl daemon-reload[ENTER] pi@raspberrypi:~ $ sudo systemctl restart nginx[ENTER] |
pi@raspberrypi:~ $ sudo vi /var/www/html/index.php[ENTER] |
<?php
phpinfo();
?>
|

◆ 3. デバイス確認 ◆
pi@raspberrypi:~ $ v4l2-ctl --list-device[ENTER] |
bcm2835-codec-decode (platform:bcm2835-codec): /dev/video10 /dev/video11 /dev/video12 USB_Camera: USB_Camera (usb-3f980000.usb-1.1.2): /dev/video0 /dev/video1 |
pi@raspberrypi:~ $ arecord -l[ENTER] |
**** ハードウェアデバイス CAPTURE のリスト **** カード 1: USBCamera [USB_Camera], デバイス 0: USB Audio [USB Audio] サブデバイス: 1/1 サブデバイス #0: subdevice #0 |
pi@raspberrypi:~ $ mkdir -p ~/public_html/temp[ENTER] pi@raspberrypi:~ $ cd /var/www/html/[ENTER] pi@raspberrypi:/var/www/html $ sudo ln -s ~/public_html/temp .[ENTER] pi@raspberrypi:/var/www/html $ cd tmp[ENTER] pi@raspberrypi:/var/www/html/tmp $ |
pi@raspberrypi:/var/www/html/tmp $ ffmpeg -f alsa -ac 2 -i hw:1 -f v4l2 \ -s 640x480 -i /dev/video0 ~/public_html/temp/output.mpg[ENTER] |
cannot set channel count to 1 (Invalid argument) hw:1: Input/output error |
pi@raspberrypi:/var/www/html/tmp $ ffmpeg -f alsa -i plughw:1 -f v4l2 \ -s 640x480 -i /dev/video0 ~/public_html/temp/output.mpg[ENTER] |
frame= 22 fps=0.0 q=4.9 size= 148kB time=00:00:00.63 bitrate=1914.3kbits/s frame= 49 fps= 48 q=8.6 size= 260kB time=00:00:01.53 bitrate=1389.1kbits/s frame= 76 fps= 49 q=13.4 size= 356kB time=00:00:02.43 bitrate=1198.5kbits/ frame= 106 fps= 51 q=15.3 size= 462kB time=00:00:03.43 bitrate=1102.3kbits/ frame= 136 fps= 53 q=13.9 size= 546kB time=00:00:04.43 bitrate=1008.9kbits/ frame= 151 fps= 49 q=12.3 size= 610kB time=00:00:04.93 bitrate=1012.9kbits/ frame= 166 fps= 46 q=12.5 size= 650kB time=00:00:05.43 bitrate= 980.0kbits/ |
video:496kB audio:317kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.027768%
Exiting normally, received signal 2.
pi@raspberrypi:/var/www/html/tmp $ |
◆ 4. ライブストリーミング用の環境設定 ◆
pi@raspberrypi:~ $ sudo mkdir -p /var/www/html/live/[ENTER] pi@raspberrypi:~ $ cd /var/www/html/live/[ENTER] pi@raspberrypi:/var/www/html/live $ sudo ln -s /dev/shm hls[ENTER] |
pi@raspberrypi:~ $ sudo curl \ https://cdn.jsdelivr.net/hls.js/latest/hls.min.js \ -o /var/www/html/hls.min.js[ENTER] |
pi@raspberrypi:~ $ vi /var/www/html/index.html[ENTER] |
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8"/>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>
<body>
<video id="video" controls width="100%"></video>
<script>
if(Hls.isSupported()) {
var video = document.getElementById('video');
var hls = new Hls();
hls.loadSource('live/hls/stream.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
}
</script>
<p>
iPhoneなどで再生されない場合は、<a href="live/hls/stream.m3u8">
こちら</a>をクリック。
</p>
</body>
</html>
|
pi@raspberrypi:~ $ vi sudo vi /etc/nginx/conf.d/rtmp[ENTER] |
rtmp {
server {
listen 1935;
chunk_size 4096;
allow play all;
access_log /var/log/nginx/rtmp_access.log;
application live {
live on;
hls on;
record off;
hls_path /var/www/html/live/hls;
hls_fragment 1s;
hls_type live;
}
}
}
|
pi@raspberrypi:~ $ sudo sh -c \ "echo \"include /etc/nginx/conf.d/rtmp;\" >> /etc/nginx/nginx.conf"[ENTER] |
pi@raspberrypi:~ $ sudo vi /etc/nginx/conf.d/default.conf[ENTER] |
server {
listen 8090;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log /var/log/nginx/access.log combined;
error_log /var/log/nginx/error.log warn;
location = /favicon.ico {
access_log off;
empty_gif;
expires 30d;
}
location / {
#auth_basic "Web Cam Streaming";
#auth_basic_user_file /var/www/.htpasswd;
root /var/www/html;
index index.html;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
}
}
|
pi@raspberrypi:~ $ sudo systemctl daemon-reload[ENTER] pi@raspberrypi:~ $ sudo systemctl restart nginx[ENTER] |
◆ 5. ストリーミングの開始・停止スクリプト ◆
pi@raspberrypi:~ $ mkdir ~/scripts/[ENTER] |
pi@raspberrypi:~ $ vi ~/scripts/streaming-start.sh[ENTER] |
#!/bin/bash
NUM=`ps aux | grep ffmpeg | grep -v grep | wc -l`
if [ $NUM -gt 0 ]; then
echo "Already running."
exit
fi
ffmpeg \
-f alsa -thread_queue_size 8192 \
-i plughw:1 -f v4l2 \
-thread_queue_size 8192 \
-input_format yuyv422 -video_size 640x480 \
-framerate 30 -i /dev/video0 -c:v h264_omx \
-b:v 768k -bufsize 768k -vsync 1 -g 16 \
-c:a aac -b:a 128k -ar 44100 -af "volume=5dB" \
-f flv rtmp://localhost/live/stream > /dev/null 2>&1 </dev/null &
|
pi@raspberrypi:~ $ v4l2-ctl -d /dev/video0 --list-formats-ext[ENTER] |
ioctl: VIDIOC_ENUM_FMT Type: Video Capture [0]: 'YUYV' (YUYV 4:2:2) Size: Discrete 640x480 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 160x120 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 176x144 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 320x240 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 352x288 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 800x600 Interval: Discrete 0.100s (10.000 fps) Size: Discrete 1280x720 Interval: Discrete 0.125s (8.000 fps) Size: Discrete 1920x1080 Interval: Discrete 0.200s (5.000 fps) [1]: 'MJPG' (Motion-JPEG, compressed) Size: Discrete 640x480 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 160x120 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 176x144 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 320x240 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 352x288 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 800x600 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 1280x720 Interval: Discrete 0.033s (30.000 fps) Size: Discrete 1920x1080 Interval: Discrete 0.033s (30.000 fps) |
#!/bin/bash
NUM=`ps aux | grep ffmpeg | grep -v grep | wc -l`
if [ $NUM -gt 0 ]; then
echo "Already running."
exit
fi
ffmpeg \
-re -f concat -thread_queue_size 8192 \
-safe 0 -i <(for file in ~/Music/*.mp3 ; \
do echo file "$file" ; done) \
-f v4l2 -thread_queue_size 8192 \
-input_format yuyv422 -video_size 640x480 \
-framerate 30 -i /dev/video0 -c:v h264_omx \
-b:v 768k -bufsize 768k -vsync 1 -g 16 \
-c:a aac -b:a 128k -ar 44100 -af "volume=5dB" \
-f flv rtmp://localhost/live/stream > /dev/null 2>&1 </dev/null &
|
#!/bin/bash
NUM=`ps aux | grep ffmpeg | grep -v grep | wc -l`
if [ $NUM -gt 0 ]; then
echo "Already running."
exit
fi
ffmpeg \
-re -f concat -thread_queue_size 8192 \
-safe 0 -i <(for i in {1..10};do for file in ~/Music/*.mp3 ; \
do echo file "$file" ; done; done) \
-f v4l2 -thread_queue_size 8192 \
-input_format yuyv422 -video_size 640x480 \
-framerate 30 -i /dev/video0 -c:v h264_omx \
-b:v 768k -bufsize 768k -vsync 1 -g 16 \
-c:a aac -b:a 128k -ar 44100 -af "volume=5dB" \
-f flv rtmp://localhost/live/stream > /dev/null 2>&1 </dev/null &
|
#!/bin/bash
NUM=`ps aux | grep ffmpeg | grep -v grep | wc -l`
if [ $NUM -gt 0 ]; then
echo "Already running."
exit
fi
ffmpeg \
-re -f concat -thread_queue_size 8192 \
-safe 0 -i <(for i in {1..10};do for file in \
`ls ~/Music/*.mp3|sort -R` ; \
do echo file "$file" ; done; done) \
-f v4l2 -thread_queue_size 8192 \
-input_format yuyv422 -video_size 640x480 \
-framerate 30 -i /dev/video0 -c:v h264_omx \
-b:v 768k -bufsize 768k -vsync 1 -g 16 \
-c:a aac -b:a 128k -ar 44100 -af "volume=5dB" \
-f flv rtmp://localhost/live/stream > /dev/null 2>&1 </dev/null &
|
pi@raspberrypi:~ $ vi ~/scripts/streaming-stop.sh[ENTER] |
#!/bin/bash sudo killall -INT ffmpeg |
pi@raspberrypi:~ $ chmod +x ~/scripts/streaming-*.sh[ENTER] |
◆ 6. ストリーミングの開始と確認そして停止 ◆
pi@raspberrypi:~ $ ~/scripts/streaming-start.sh[ENTER] |
◆ 7. BASIC認証をかける ◆
pi@raspberrypi:~ $ sudo htpasswd -c /var/www/.htpasswd username[ENTER] |
pi@raspberrypi:~ $ sudo vi /etc/nginx/conf.d/default.conf[ENTER] |
#auth_basic "Web Cam Streaming";
#auth_basic_user_file /var/www/.htpasswd;
|
auth_basic "Web Cam Streaming";
auth_basic_user_file /var/www/.htpasswd;
|
pi@raspberrypi:~ $ sudo systemctl daemon-reload[ENTER] pi@raspberrypi:~ $ sudo systemctl restart nginx[ENTER] |
◆ 8. マイク設定 ◆
pi@raspberrypi:~ $ alsamixer[ENTER] |

F6キーでサウンドカードの一覧を開き

「USB_Camera」選択します


F4キーで録音音量調整(マイクボリューム調整)を行います。

上矢印キーで増音、下矢印キーで減音となります。
ライブ配信しながら調整可能です。