本问用python实现了拉流(rtsp、http-flv、rtmp)、读文件、渲染、播放、以及可用于做视频AI分析等功能,用python开发最大好处是100行代码就实现了音视频拉流播放pipeline的整个流程,缺点是接口过于简单,不便于修改内部属性等,用与做点功能验证还是很方便的。不过要真正做音视频开发还的c,c++通过ffmpeg来实现
import json
import requests
import cv2
import datetime
import os
import shutil
import numpy as np
requests.packages.urllib3.disable_warnings()
# 0 open camera
# 1 open usb camera
# 2 read file
# 3 stream include rtsp rtmp httpflv
def openVideo(streamtype,filepath):
# cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("E:\\tool\\ffmpeg\\bin-bk\\1080p.mp4")
# cap = cv2.VideoCapture("rtsp://admin:[email protected]:554/h264/ch33/main/av_stream")
#print(cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if(streamtype==3 or streamtype==2):
cap = cv2.VideoCapture(filepath)
else:
cap = cv2.VideoCapture(0)
while (1):
ret, frame = cap.read() # get a frame
if ret == True:
showdate = str(datetime.datetime.now())
font = cv2.FONT_HERSHEY_SIMPLEX
frame = cv2.putText(frame, showdate, (10, 100), font, 0.5, (0, 255, 255), 2, cv2.LINE_AA)
cv2.imshow("capture", frame) # show a frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
openVideo(0,"")
本文实现了最简单的拉流、播放的流程,可以做其他的扩展使用。
参考:https://blog.csdn.net/m0_46825740/article/details/125301952