使用OpenCV流式传输到Facebook Live
我计划将视频文件流式传输到Facebook Live,但我想编程编辑其帧,例如根据添加文本。我的问题是我不知道如何正确地将数据发送到Facebook Live。我尝试了FFMPEG,但它不起作用。
这是我尝试的代码
import subprocess
import cv2
rtmp_url = "rtmps://live-api-s.facebook.com:443/rtmp/FB-1081417119476224-0-AbwwMK91tFTjFy2j"
path = "7.mp4"
cap = cv2.VideoCapture(path)
# gather video info to ffmpeg
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# command and params for ffmpeg
command = ['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', f"{width}x{height}",
'-r', str(fps),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
rtmp_url]
# using subprocess and pipe to fetch frame data
p = subprocess.Popen(command, stdin=subprocess.PIPE)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("frame read failed")
break
# YOUR CODE FOR PROCESSING FRAME HERE
# write to pipe
p.stdin.write(frame.tobytes())
I am planning to stream a video file to Facebook Live but I want to programmatically edit its frames like adding texts depending. My problem is that I don't know how to properly send data to Facebook Live. I tried ffmpeg but it doesn't work.
Here is my code that I tried
import subprocess
import cv2
rtmp_url = "rtmps://live-api-s.facebook.com:443/rtmp/FB-1081417119476224-0-AbwwMK91tFTjFy2j"
path = "7.mp4"
cap = cv2.VideoCapture(path)
# gather video info to ffmpeg
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# command and params for ffmpeg
command = ['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', f"{width}x{height}",
'-r', str(fps),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
rtmp_url]
# using subprocess and pipe to fetch frame data
p = subprocess.Popen(command, stdin=subprocess.PIPE)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("frame read failed")
break
# YOUR CODE FOR PROCESSING FRAME HERE
# write to pipe
p.stdin.write(frame.tobytes())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论