提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
将一个rtsp流 经过流媒体服务,推送成rtmp 供 vlc查看
1启动服务,记录服务rtmp端口
见 之前写的搭建推流服务的笔记
# _*_ coding:utf-8 _*_
'''
python3
opencv
ffmpeg
rtmp 推流视频直播pipe:: Invalid argumentb
'''
import time
import cv2
import subprocess
import platform
import shlex
SYSTEM_NOW = platform.system()
# ffmpeg 推流
class FfmpegRtmp(object):
def __init__(self, rtmpurl, appname,rtmpfile, width = 1920,height=1080):
# self.rtmpUrl = "rtmp://127.0.0.1:1935/" + rtmpfile
self.rtmpUrl = rtmpurl + appname+ "/" + rtmpfile
self.width = width
self.height = height
self.sizeStr = "{}x{}".format(width, height)
self.FPS = 25.0
self.stat = True
if width == 1920 and height == 1080:
self.videoType = "yuv1080p"
else:
self.videoType = "yuv720p"
if SYSTEM_NOW == "Linux":
self.command = [
'/home/xiding/workspace/ffmpeg-4.4.1-amd64-static/ffmpeg',
'-y', '-an',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', self.sizeStr,
'-r', str(self.FPS),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
self.rtmpUrl
]
else:
self.command = [
'G:\\software\\ffmpeg\\ffmpeg-4.4-essentials_build\\bin\\ffmpeg', # 本机ffpeg软件地址
'-y', '-an',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', self.sizeStr,
'-r', str(self.FPS),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
self.rtmpUrl
]
self.pipe = subprocess.Popen(self.command,shell=False,stdin=subprocess.PIPE)
def write_img_ffmpeg(self,image):
self.pipe.stdin.write(image.tobytes())
def release_pipe_ffmpeg(self):
self.pipe.terminate()
def main_decode_gpu(gpuid, rtspurl):
cv2.setDevice(gpuid)
# img_Mat =
if __name__ == "__main__":
#rtmp://192.168.1.194/${app_name}/${stream_name}
#ffmpeg -rtsp_transport tcp -i rtsp://admin:[email protected]/video1 -vcodec copy -acodec copy -f flv -y rtmp://192.168.1.194/live/stream
# 流媒体服务器的 rtmp 地址
url_rtmp = "rtmp://192.168.12.149:1935"
file_video = "EXPID_STDID"
rtmpfile = "top"
fr = FfmpegRtmp(rtmpurl=url_rtmp,
appname=file_video,
rtmpfile=rtmpfile,
width=1920,
height=1080)
# 待转发的rtsp流 也可以是离线视频
# file_openvideo = "rtsp://admin:[email protected]/h264/ch1/main/av_stream"
file_openvideo = "rtsp://admin:[email protected]/h264/ch1/main/av_stream"
cap = cv2.VideoCapture(file_openvideo)
time.sleep(6)
while cap.isOpened():
t = time.time()
success, frame = cap.read()
# print(frame.shape)
if success:
# cv2.imshow('', frame)
# cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
fr.write_img_ffmpeg(frame)
print("{}\n".format(time.time()-t))
# time.sleep(0.04)
cap.release()
# fr.release_pipe_ffmpeg()
启动vlc, 脚本推送的rtmp 地址为 : rtmp://192.168.12.149:1935/top
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了推流使用