用树莓派在B站24小时直播

第一步:安装ffmpeg和解码器

1
2
3
4
5
6
7
sudo git clone git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make
sudo make install
cd ..
rm -rf x264

再安装ffmpeg(一句一句执行……)

1
2
3
4
5
6
7
sudo git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make
sudo make install
cd ..
rm -rf ffmpeg

第三步:进行推流
把文件传到树莓上,使用下面的命令即可推流:

1
ffmpeg -re -i "1.mp4" -vcodec copy -acodec aac -b:a 192k -f flv "你的rtmp地址/你的直播码"

由于此命令只能运行一次,播放完就停了,所以要实现无限循环播放的话就需要编写一个sh文件,内容如下:

1
2
3
4
5
#!/bin/bash
while true
do
ffmpeg -re -i "1.mp4" -vcodec copy -acodec aac -b:a 192k -f flv "你的rtmp地址/你的直播码"
done

保存为live.sh,使用下面的命令运行即可(如果没有screen,先使用sudo apt-get install screen安装):

1
screen nohup sh live.sh &

修改ffmpeg运行脚本

因为我们是运行多个文件,所以我们需要连续推流

首先新建一个txt,内容类似为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
file '/home/pi/songs/1.flv'
file '/home/pi/songs/2.flv'
file '/home/pi/songs/3.flv'
file '/home/pi/songs/4.flv'
file '/home/pi/songs/5.flv'
file '/home/pi/songs/6.flv'
file '/home/pi/songs/7.flv'
file '/home/pi/songs/8.flv'
file '/home/pi/songs/9.flv'
file '/home/pi/songs/10.flv'
file '/home/pi/songs/11.flv'
file '/home/pi/songs/12.flv'
file '/home/pi/songs/13.flv'
file '/home/pi/songs/14.flv'
file '/home/pi/songs/15.flv'
file '/home/pi/songs/16.flv'
file '/home/pi/songs/17.flv'
file '/home/pi/songs/18.flv'
file '/home/pi/songs/19.flv'
file '/home/pi/songs/20.flv'
file '/home/pi/songs/21.flv'
file '/home/pi/songs/22.flv'
file '/home/pi/songs/23.flv'
file '/home/pi/songs/24.flv'
file '/home/pi/songs/25.flv'
file '/home/pi/songs/26.flv'
file '/home/pi/songs/27.flv'
file '/home/pi/songs/28.flv'
file '/home/pi/songs/29.flv'
file '/home/pi/songs/30.flv'

启动的sh脚本要改成下面的样子:

1
2
3
4
5
#!/bin/bash
while true
do
  ffmpeg -re -f concat -safe 0 -i playlist.txt -vcodec copy -acodec aac -b:a 192k -f flv "你的。。。那啥网址和码"
done

启动就行了。。

1
2
screen nohup sh live.sh &
screen nohup python live.py &

发表评论