利用树莓派实现b站24小时音乐直播

本篇文章只能实现固定歌曲的循环播放功能,如果你想要点歌功能,请查看下面这篇文章:

【重写】树莓派驱动的b站点播台


最新的文章已经把代码更新为弹幕点歌了,并且在GitHub持续更新。


本教程参考了以下文章:

ffmpeg-rtmp搞事指南

树莓派安装ffmpeg

使用FFmpeg在B站直播的姿势

第一步:安装ffmpeg

先安装解码器

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

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. git下载慢的话可以先导入到gitosc再clone

  2. 编译ffmpeg的过程在树莓上十分漫长。。。我的花了两个半小时,所以建议使用screen扔到后台,以免断网导致需要重来

第二步:准备直播所需要的视频文件

我们需要制作一个只有背景音乐的视频,一般会做一个一图流的视频(整个视频画面就是一张图)

这里我使用了小丸工具箱(点我下载

先要合并一堆mp3文件,具体方法:

  1. 全选这些mp3文件

  2. 右击添加到压缩文件

  3. 文件类型选择zip,压缩类型选择“储存”

  4. 将压缩好的xxx.zip拓展名改为.mp3即可(是不是很神奇hhhhh)

接下来照一张图片,打开小丸工具箱,选择“常用”选项卡

把图片拖到“一图流”的“图片”框里,把整合好的音乐拖到“音频”框里,点击“压制”

这样我们就得到了我们需要的文件

第三步:进行推流

把文件传到树莓上,使用下面的命令即可推流

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

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

#!/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 screen 安装)

screen nohup sh live.sh &

搭建好的直播间:http://live.bilibili.com/16703

不保证我以后的直播内容不变哈

39 Comments

  1. Google Chrome 58.0.3029.110 Google Chrome 58.0.3029.110 Windows 10 x64 Edition Windows 10 x64 Edition

    ffmpeg: error while loading shared libraries: libavdevice.so.57: cannot open shared object file: No such file or directory
    你知道这是怎么回事吗?缺少libraries吗? 怎么解决啊?

  2. Google Chrome 58.0.3029.110 Google Chrome 58.0.3029.110 Windows 10 x64 Edition Windows 10 x64 Edition

    你好 我又照着安装了一遍 成功了 但现在想实现 视频文件顺序播放 请教怎么弄啊?

  3. Firefox 53.0 Firefox 53.0 Windows 7 x64 Edition Windows 7 x64 Edition

    ffbuild/common.mak:59: recipe for target ‘libavcodec/h264qpel.o’ failed
    make: *** [libavcodec/h264qpel.o] Error 1
    编译ffmpeg出的错了,要怎么办?

      1. Google Chrome 60.0.3112.78 Google Chrome 60.0.3112.78 Windows 10 x64 Edition Windows 10 x64 Edition

        dit 下载特别慢而且下载到百分之97就不动了。。。速度还显示7kb/s这个咋解决啊

    1. Vivaldi 1.97.1259.3 Vivaldi 1.97.1259.3 Windows 10 x64 Edition Windows 10 x64 Edition

      先去学一下电脑的基本使用方法、linux的命令使用方法、计算机路径的表达方式,再来问这些问题

发表评论

您的电子邮箱地址不会被公开。