Use Raspberry Pi to Hang Up Live and Auto Check-in

Android emulator and Docker on Windows can’t be used at the same time. I want to use Raspberry Pi to set up a development environment. I just watched the live broadcast recently, so maybe running an RTMP live broadcast on the Raspberry Pi is a good idea.

Live

The live streaming is using FFmpeg, a command-line tool for video conversion. By installing H264 and FFmpeg on the Raspberry Pi, we can use Pi for RTMP streaming live.

The advantage of this solution is that the Raspberry Pi low power consumption, but can the same hardware solution by GPU, the CPU occupancy rate is not high, which can support Raspberry Pi running more tasks simultaneously.

Install

It is nothing more than compile and install, just pay attention to speed up the compilation with multi-threading.

Install H264, the sample code is as follows

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
sudo make -j4
sudo make install
cd ..
rm -rf x264

Install FFMPEG, the sample code is as follows

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
sudo make -j4
sudo make install
cd ..
rm -rf ffmpeg

Then you can push streaming, sample code as follows

1
ffmpeg -re -i "1.mp4" -vcodec copy -acodec aac -b:a 192k -f flv "rtmp地址及推流码"

This is my testing [live room address] (https://live.bilibili.com/4683991), the loading is slow (the upload bandwidth is small), and sometimes it is disconnected (the network is unstable).

Check in

The login of the Bilibili is a bit strict. I use the cookies to keep the login status, and then simulate the check-in.

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
31
32
33
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests
s = requests.session()
import os
import requests.packages.urllib3.util.ssl_
import time
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL'
cookies = {
'sid': 'XXX',
'buvid3': 'XXX',
'fts': 'XXX',
'pgv_pvi': 'XXX',
... ...
'DedeUserID__ckMd5': 'XXX',
'SESSDATA': 'XXX',
'bili_jct': 'XXX',
'finger': 'XXX',
}

headers = {
'User-Agent': 'XXX',
'Host': 'api.live.bilibili.com',
'Origin': 'https://live.bilibili.com',
'Connection': 'keep-alive',
'Referer': 'https://live.bilibili.com',
'Content-Type': 'application/x-www-form-urlencoded',
}
url = "https://api.live.bilibili.com/sign/doSign"

r = s.get(url=url, headers=headers, verify=False, cookies=cookies)
print(time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime())+r.text)

Use crontab -e to set up scheduled tasks, so you don’t have to manually check in before the cookies expire.

————————–2018.03.22 updated ——————————–

Added time display in the log.