在过去的一周中,我一直在寻找一种实时流媒体解决方案,以从连接到Raspberry Pi的USB网络摄像头播放视频。 这篇文章将帮助您逐步在Raspberry Pi上设置实时流服务器。
在Raspberry Pi上安装FFmpeg
我的第一次尝试是在命令行中输入“ sudo apt-get install ffmpeg ”。 然后我得到了错误消息: 软件包’ffmpeg’没有安装候选者 。
幸运的是,我们可以从GitHub中获取FFmpeg的源代码,并参考Raspberry Pi的FFMPEG文章自行构建。
在构建FFmpeg之前,您必须构建依赖的编解码器库。 假设要以Ogg格式流式传输视频,则需要视频编解码器libtheora,它取决于libogg-1.3.1和libvorbis-1.3.3。
运行以下命令来构建和安装FFmpeg:
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar
-xf libvorbis-1.3.3.tar.gz
cd
libvorbis-1.3.3/
./configure
--host=arm-unknown-linux-gnueabi --enable-static
make
sudo
make
install
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.1.tar.gz
tar
-xf libogg-1.3.1.tar.gz
cd
libogg-1.3.1/
./configure
--host=arm-unknown-linux-gnueabi --enable-static
make
sudo
make
install
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
tar
-xf libtheora-1.1.1.tar.bz2
cd
libtheora-1.1.1/
./configure
--host=arm-unknown-linux-gnueabi --enable-static
make
sudo
make
install
git clone git://git.videolan.org/x264
cd
x264
./configure
--host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make
sudo
make
install
git clone https://github.com/FFmpeg/FFmpeg.git
cd
ffmpeg
sudo
./configure
--arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree --enable-libtheora --enable-libvorbis
make
sudo
make
install
FFServer实时流
一旦安装了FFmpeg,就可以运行ffserver。 缺省情况下,ffserver加载配置文件/etc/ffserver.conf 。 在Raspberry Pi上,您需要手动创建它。 请参考ffserver示例页面。 这是我对Ogg格式的配置:
HTTPPort 9090
HTTPBindAddress 0.0.0.0
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 100000
#NoDaemon
< Feed
feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 200K
ACL allow 127.0.0.1
</ Feed >
< Stream
test.ogg>
Format ogg
Feed feed1.ffm
VideoCodec libtheora
VideoFrameRate 24
VideoBitRate 512
VideoSize 320x240
VideoQMin 1
VideoQMax 31
VideoGopSize 12
PreRoll 0
AVOptionVideo flags +global_header
Noaudio
</ Stream >
< 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
</ Stream >
运行ffserver并发送视频流:
ffserver -f /etc/ffserver.conf & ffmpeg -re -f video4linux2 -i /dev/video0
-vcodec libtheora http://localhost:9090/feed1.ffm
如何停止ffserver? 我从StackOverflow获得了解决方案。 找到端口并终止进程:
netstat -tulnap sudo定影器-k 9090 / tcp
使用ffplay或vlc播放流:
ffplay http://192.168.8.50:9090/test.ogg
使用Nginx RTMP模块进行实时流传输
受帖子https://docs.peer5.com/guides/setting-up-hls-live-streaming-server-using-nginx/的启发,我使Nginx成为Raspberry Pi上的实时流服务器。
获取nginx-rtmp-module的源代码:
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
安装Nginx依赖项:
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
安装Nginx:
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar
-xf nginx-1.12.1.tar.gz
cd
nginx-1.12.1
./configure
--with-http_ssl_module --add-module=../nginx-rtmp-module
make
-j 2
sudo
make
install
在/ usr / local / nginx / html下创建一个hls文件夹:
须藤mkdir / usr / local / nginx / html / hls
编辑/usr/local/nginx/conf/nginx.conf :
rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;
application show {
live on;
# Turn on HLS
hls on;
hls_path /usr/local/nginx/html/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
http {
server {
listen 8080;
location /hls {
# Disable cache
add_header 'Cache-Control' 'no-cache';
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
}
}
}
启动Nginx:
须藤/ usr / local / nginx / sbin / nginx
重新加载Nginx:
须藤/ usr / local / nginx / sbin / nginx -s重新加载
发送视频流:
ffmpeg -re -f video4linux2 -i / dev / video0 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://192.168.8.50/show/stream
从Windows发送视频流:
ffmpeg -re -f dshow -rtbufsize 100M -i video =“ USB2.0 Camera” -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://192.168.8.50/show/stream
播放流:
vlc http://192.168.8.50:8080/hls/stream.m3u8
ffplay http://192.168.8.50:8080/hls/stream.m3u8
