提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔
提示:這裡可以添加本文要記錄的大概內容:
例如:隨著人工智能的不斷發展,機器學習這門技術也越來越重要,很多人都開啟了學習機器學習,本文就介紹了機器學習的基礎內容。
提示:以下是本篇文章正文內容,下面案例可供參考
將一個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
提示:這裡對文章進行總結:
例如:以上就是今天要講的內容,本文僅僅簡單介紹了推流使用