Skip to content

FFmpeg

我们有 whipintowhepfrom 来把 rtp <-> whip/whep 进行转换

例如:

ffmpeg -> whipinto -> live777 -> whepfrom -> ffplay

ffmpeg/whip

ffmpeg >= 8.0 版本之后支持 whip 协议 ffmpeg/whip

需要在构建时打开 --enable-muxer=whip

大部分预编译二进制包都没有启用这个

ffmpeg -> live777 -> whepfrom -> ffplay

目前只支持 h264opus 编码

可以使用这个 ffmpeg 的 Docker 镜像:

bash
docker build -f docker/Dockerfile.ffmpeg -t ghcr.io/binbat/ffmpeg .

H264

X264 RTP

视频测试源

bash
# send RTP and Create SDP file
ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=30 -vcodec libx264 \
-preset ultrafast -tune zerolatency \
-profile:v baseline -level 3.0 -pix_fmt yuv420p \
-g 30 -keyint_min 30 -b:v 1000k \
-minrate 1000k -maxrate 1000k -bufsize 1000k \
-f rtp 'rtp://127.0.0.1:5002' -sdp_file input.sdp

对于 ffplay,你需要一个 sdp 文件

bash
ffplay -protocol_whitelist rtp,file,udp -i input.sdp

你可以使用 whipinto 工具接收 RTP 并转换为 WebRTC

bash
# Use SDP file as input
whipinto -i input.sdp -w http://localhost:7777/whip/777
bash
whepfrom -o output.sdp -w http://localhost:7777/whep/777

对于 ffplay,你需要一个 sdp 文件

bash
ffplay -protocol_whitelist rtp,file,udp -i output.sdp

X264 WHIP

bash
docker run --rm --network host \
ghcr.io/binbat/ffmpeg:latest \
\
ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=30 \
-vcodec libx264 -profile:v baseline -level 3.0 \
-pix_fmt yuv420p -g 30 -keyint_min 30 \
-b:v 1000k -minrate 1000k -maxrate 1000k \
-bufsize 1000k -preset ultrafast -tune zerolatency \
-f whip http://localhost:7777/whip/777

H265

bash
ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=30 -vcodec libx265 \
-preset ultrafast -tune zerolatency \
-x265-params keyint=30:min-keyint=30:bframes=0:repeat-headers=1 \
-pix_fmt yuv420p \
-b:v 1000k -minrate 1000k -maxrate 1000k -bufsize 1000k \
-f rtp 'rtp://127.0.0.1:5002' -sdp_file input.sdp
bash
whipinto -i input.sdp -w http://localhost:7777/whip/777

AV1

WARNING

  • 需要设置 -strict experimental
  • 对于 whipinto AV1 需要设置 pkt_size<=1200-f rtp "rtp://127.0.0.1:5002?pkt_size=1200"
bash
ffmpeg -re -f lavfi -i testsrc=size=640x360:rate=30 -pix_fmt yuv420p \
-c:v libaom-av1 -cpu-used 8 -tile-columns 0 -tile-rows 0 -row-mt 1 \
-lag-in-frames 0 -g 30 -keyint_min 30 -b:v 0 -crf 30 -threads 4 \
-strict experimental \
-f rtp "rtp://127.0.0.1:5002?pkt_size=1200" -sdp_file input.sdp

VP8

bash
ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=30 \
-vcodec libvpx -f rtp 'rtp://127.0.0.1:5002' -sdp_file input.sdp
bash
whipinto -i input.sdp -w http://localhost:7777/whip/777

VP9

WARNING

VP9 打包功能尚处于实验阶段,其规范尚处于草案状态。请设置 -strict experiment 选项以启用此功能。

bash
ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=30 \
-strict experimental -vcodec libvpx-vp9 -pix_fmt yuv420p \
-f rtp 'rtp://127.0.0.1:5002' -sdp_file input.sdp

WARNING

VP9 支持多种颜色空间,必须添加 -pix_fmt yuv420p 参数。

bash
whipinto -i input.sdp -w http://localhost:7777/whip/777

OPUS

OPUS RTP

bash
ffmpeg -re -f lavfi -i sine=frequency=1000 \
-acodec libopus -f rtp 'rtp://127.0.0.1:5004'

OPUS WHIP

bash
docker run --rm --network host \
ghcr.io/binbat/ffmpeg:latest \
\
ffmpeg -re -f lavfi -i sine=frequency=1000 \
-ac 2 -ar 48000 -acodec libopus \
-f whip http://localhost:7777/whip/777

G722

bash
ffmpeg -re -f lavfi -i sine=frequency=1000 \
-acodec g722 -f rtp 'rtp://127.0.0.1:5004?pkt_size=1200'

Both

VP8+OPUS RTP

bash
ffmpeg -re \
-f lavfi -i sine=frequency=1000 \
-f lavfi -i testsrc=size=640x480:rate=30 \
-acodec libopus -vn -f rtp rtp://127.0.0.1:5002 \
-vcodec libvpx -an -f rtp rtp://127.0.0.1:5004 -sdp_file input.sdp

H264+G722 RTP

bash
ffmpeg -re \
-f lavfi -i sine=frequency=1000 \
-f lavfi -i testsrc=size=640x480:rate=30 \
-acodec g722 -vn -f rtp rtp://127.0.0.1:5002 \
-vcodec libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p \
-g 30 -keyint_min 30 -b:v 1000k -minrate 1000k -maxrate 1000k -bufsize 1000k \
-preset ultrafast -tune zerolatency -an -f rtp rtp://127.0.0.1:5004 \
-sdp_file input.sdp

H264+G722 WHIP

bash
docker run --rm --network host \
ghcr.io/binbat/ffmpeg:latest \
\
ffmpeg -re \
-f lavfi -i sine=frequency=1000 \
-f lavfi -i testsrc=size=640x480:rate=30 \
-ac 2 -ar 48000 -acodec libopus \
-vcodec libx264 -profile:v baseline -level 3.0 \
-pix_fmt yuv420p -g 30 -keyint_min 30 \
-b:v 1000k -minrate 1000k -maxrate 1000k \
-bufsize 1000k -preset ultrafast -tune zerolatency \
-f whip http://localhost:7777/whip/777

Released under the MPL-2.0 License.