Millet Porridge

English version of https://corvo.myseu.cn

0%

FFmpeg Live Streaming on Bilibili

FFmpeg is a powerful video stream processing tool; basic transcoding operations go without saying. I think most people get to know it through its omnipotent format conversion.

If you have a Bilibili live account, you can try the commands below. If you want to push to Longzhu or Huya, you’re welcome to explore FFmpeg‘s commands — I’ll add a link on the blog for you.

If you really want to live stream, just use OBS; here’s a simple tutorial.

Pushing to Bilibili with FFmpeg

Actually I referred to this fellow’s configuration: The posture of live streaming on Bilibili with FFmpeg

1
2
3
4
5
6
7
8
9
10
11
# The address at the end is the RTMP address + stream key
ffmpeg -re \
-i Hero.E09.DVDrip.x264.AC3-YYeTs.mkv \
-vf subtitles=Hero.E09.DVDrip.x264.AC3-YYeTs.srt \
-c:v libx264 \
-preset veryfast \
-maxrate 3000k \
-bufsize 6000k -pix_fmt yuv420p -g 50 -b:a 160k -ac 2 \
-ar 44100 \
-f flv \
"rtmp://js.live-send.acg.tv/live-js/?streamname=xxx&key=xxx"

The command above pushes a video to the live room. Going through the whole command would bore you, so I’ll pick a few key parameters (actually I hadn’t used FFmpeg before; I really don’t want to learn the rest for now — if there are problems please point them out directly in the comments).

Parameter Some meaning
-re Slow down the reading speed. Without this parameter, pushing video reaches 100~200 fps — the server can’t keep up.
-i; -vf Specify the video and subtitles.
-c:v libx264 I don’t know why x264 encoding is a must — maybe it’s just needed
-flv Bilibili only accepts flv-format streams

Raspberry Pi Pushing

Many of you have little things like Raspberry Pis; you can totally push directly from a Pi. Anyway, for Linux, FFmpeg has almost become default software. Post your live address and let’s all watch together.

FFmpeg Screen Recording and Testing

After RTMP pushing, a bit of screen recording and testing:

1
2
3
4
5
6
7
ffmpeg \
-f x11grab \
-s 1920x1080 -i :0.0+0,0 \
-c:v libx264 \
-preset ultrafast -crf 0 \
-r 30 \
output.mp4

The command above records the screen content into output.mp4.

Note: because I’m a Linux user, I use -f x11grab here. If you’re a Windows user, use -f gdigrab; Mac users, explore on your own.

The command above is enough for recording, but if we just want to view the video, we don’t necessarily need it to generate a file — and generated files have to be deleted afterwards.

Achieving Quick Preview

At this point, if you have the concept of streams, you should know that video is also a stream. The operation just now simply directed the video stream into the file output.mp4. So we can play the video stream directly with a player, which leads naturally to the command below: generate no file, and use stdout directly as the intermediate stream.

1
2
3
4
5
6
7
ffmpeg \
-f x11grab \
-s 1920x1080 -i :0.0+0,0 \
-c:v libx264 \
-preset ultrafast -crf 0 \
-r 30 \
-f flv - | mpv -

The - in the command outputs the video stream to stdout; mpv - means reading the video stream from stdout (vlc - also works).

So you can directly use a local player to view and test FFmpeg‘s output.

Finally, here’s my live address. When I stream Oxygen Not Included, everyone’s welcome to watch — haha, seems this is the real point:

https://live.bilibili.com/3995303