天天看点

Raspberry Pi 1Model B+ Streaming Troubleshoot

一、首次开机

使用设备是6年前的Raspberry Pi 1b+,性能太差劲了。首先使用ubuntu挂载sd卡,

sudo mount -t drvfs G: sd/
cd sd/
           

当我们把树莓派系统镜像烧录到SD卡之后,我们在windows看到的TF卡变成了空间很小的名为boot的盘,我们在此目录下新建一个名为

wpa_supplicant.conf

空白文件,并在其中加入以下配置:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
	ssid="WiFi名字,不删除引号,不能有中文"
	psk="WiFi密码,不删除引号"
	priority=将我替换成数字,数字越大代表优先级越高
}
           

默认没有ssh,开启ssh,要在该分区创建一个空文件ssh:

touch ssh
           

ssh连接地址使用主机名,不用每次查IP:

raspberrypi.local
pi,raspberry
           

使用bullseye的源:

deb http://mirrors.aliyun.com/raspbian/raspbian/ bullseye contrib firmware main non-free rpi
           

或者配置代理也可以,打开clash的Allow LAN选项,关闭防火墙,在rasp短的rasp-config中配置proxy。

ffmpeg

直接使用预编译版本,不支持硬件编码。编码器选项使用:

-vcodec h264_v4l2m2m // 硬件编码
-vcodec libx264  // 软件编码
           
ffmpeg  -f video4linux2 -video_size 640x480 -framerate 30 -thread_queue_size 1024 -s 640*480 -r 30 -i /dev/video0 -g 60 -b:v 2M -maxrate 2M -bufsize 2M -vcodec h264_v4l2m2m -pix_fmt yuv420p -f flv -flvflags no_duration_filesize "rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_8684421_9312898&key=bdba22d8d07fb948160b3c8831d13a11&schedule=rtmp&pflag=9"
           

ultrafast设置,用于软解:

ffmpeg -i /dev/video0 -tune zerolatency -vcodec libx264 -preset ultrafast -b:v 400k -s 640*480 -r 30 -f flv "rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_8684421_9312898&key=bdba22d8d07fb948160b3c8831d13a11&schedule=rtmp&pflag=9"
           
ffmpeg -nostdin -threads auto -y -f v4l2 -thread_queue_size 4096 -fflags +genpts -flags +global_header -i /dev/video0 -vcodec h264_v4l2m2m -num_output_buffers 32 -num_capture_buffers 16 -an -s 640x480 -r 30 -sc_threshold 0 -pix_fmt yuv420p -b:v 2M -minrate 1M -maxrate 2M -bufsize 2M
           
从源码编译(使用h264_v4l2m2m):

不能用omx,omx在新版本中已经废弃,源码编译参照[SOLVED] how to install 64 bit FFmpeg? - Raspberry Pi Forums:

ffmpeg -f video4linux2 -video_size 640x480 -framerate 24 -thread_queue_size 1024 -s 640*480 -r 24 -i /dev/video0 -g 48 -c:v h264_v4l2m2m -b:v 2M -maxrate 2M -bufsize 2M -c:a copy -f flv -flvflags no_duration_filesize ""
           
直播参数

bilibili支持最高码率14000,支持1080P,16:9。

Raspberry Pi 1Model B+ Streaming Troubleshoot

输出参数q,refers to the Constant Rate Factor (

crf

).

The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A *lower* value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn’t technically lossless.

q=-0.0代表无损画质。

Roundabout

h264_v4l2m2m虽然是硬件编码器,但是该编码器的输出视频不能直接在bilibili直播,所以目前只能采取先写入临时文件,再使用ffmpeg从临时文件读取,推流到bilibili的办法。

首先从usb摄像头读取视频流并编码,

capture.sh

如下:

#!/bin/bash

nohup ffmpeg -f video4linux2 -video_size 640x480 -framerate 20 -thread_queue_size 1024 -s 640*480 -r 20 -i /dev/video0 -g 40 -c:v h264_v4l2m2m -b:v 2M -maxrate 5M -bufsize 2M -f flv -flvflags no_duration_filesize tmp.flv
           

然后使用ffmpeg从本地临时文件推流,

push.sh

,push的时候,直播间的地址保存至

live.txt

中,每次从文件读取:

#!/bin/bash

echo "$1"
nohup ffmpeg -re -i tmp.flv -vcodec copy -f flv "$1"
           

推流的速度比较快,能够每秒推200FPS以上,编码的速度根本赶不上,所以要使用

-re

选项,保持源视频的速度,同时从摄像头抓取视频时把

tmp.flv

的帧率设置低一些,只有20FPS。这样推流就只有20FPS,而抓取视频一般可以达到24FPS,避免速度太快把文件读完后进程崩了。

启动这后台两个进程,

stream.sh

#!/bin/bash

rm -rf tmp.flv
rm -rf live.txt
echo "$1" >> live.txt
./capture.sh &
sleep 15
./push.sh "$1" &
           

推流使用

-vcodec copy

,不改变源文件的编码。

接着设置定时任务,

killboth.sh

用于杀死这两个ffmpeg进程:

#!/bin/bash

NAME='ffmpeg'
echo $NAME
ID=$(ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}')
echo $ID
for id in $ID; do
    kill -9 $id
    echo "killed $id"
done
           

定时任务

checkalive.sh

,每次发现推流的进程崩了,就

killboth.sh

,然后执行

stream.sh

#!/bin/bash

ps -fe | grep 'ffmpeg -re' | grep -v grep
if [ $? -ne 0 ]; then
    echo "pushing process dead"
    ./killboth.sh
    ./stream.sh "$(cat live.txt)"
else
    echo "alive....."
fi
           

随后使用

crontab -e

,设置定时任务:

*/20 * * * * bash /home/pi/killboth.sh
*/1 * * * * bash /home/pi/checkalive.sh
           

每次崩了之后等定时任务拉起即可。

alive…"

fi

随后使用`crontab -e`,设置定时任务:

```bash
*/20 * * * * bash /home/pi/killboth.sh
*/1 * * * * bash /home/pi/checkalive.sh
           

每次崩了之后等定时任务拉起即可。

继续阅读