使用 ffmpeg-python 对rtmp 服务器做压力测试
FFmpeg 下载: https://github.com/BtbN/FFmpeg-Builds/releases ffmpeg-master-latest-win64-gpl.zip
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @mail : lshan523@163.com
# @Time : 2023/12/23 16:05
# @Author : Sea
# @File : ffmpngtest4.py
# @history: 使用 ffmpeg-python 对rtmp 服务器做压力测试
# ****************************
import subprocess
import threading
import datetime
import uuid
RTMP_SERVER = "rtmp://192.168.18.61:1935/Sea1/test212"
MP4_FILE = "F:\SeaCode\pytest\FFmpeg\zbh_zp_bg.mp4"
NUM_CLIENTS = 10 # 模拟客户端数量
# ffmpeg_command = "ffmpeg -re -i /home/sea/test/video.mp4 -c:v copy -f flv rtmp://192.168.18.61:1935/Sea1/test212"
# 调整分辨率 -vf scale=1280:720 -vf scale=854:480参数来指定视频的分辨率为480p
# ffmpeg_command = "ffmpeg -re -i /home/sea/test/video.mp4 -vf scale=1280:720 -c:v libx264 -f flv rtmp://192.168.18.61:1935/Sea1/test212"
def push_to_rtmp(url):
strftime = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
command = [
"ffmpeg",
"-re",
"-i", MP4_FILE,
"-vf", "scale=854:480",
"-c:v", "libx264",
"-preset", "ultrafast",
"-tune", "zerolatency",
"-c:a", "aac",
"-f", "flv",
url+str(strftime)
]
subprocess.run(command)
threads = []
for i in range(NUM_CLIENTS):
t = threading.Thread(target=push_to_rtmp, args=(RTMP_SERVER+str(i),))
threads.append(t)
t.start()
for t in threads:
t.join()