import ffmpeg
import cv2
import numpyas np
out_filename ="C://Users//new//Videos//202204061434-out2.mp4"
width =640
height =480
video_format ="h264"
server_url ="udp://127.0.0.1:1234"
#ffmpeg.input("video=xxx",format="dshow").output(filename=server_url,format=video_format,listen=1).run_async()
#ffmpeg.exe -f gdigrab -video_size 640x480 -i desktop -tune zerolatency -vcodec h264 -preset ultrafast -g 13 -max_delay 100 -r 20 -acodec aac -pkt_size 1316 -probesize 32 -flags low_delay -f h264 udp://localhost:12345
#ffmpeg.input("C://Users//new//Videos//1.mp4").output(filename=server_url,format=video_format,listen=1).run_async()
def gadd(a,b):
cv2.imshow("img", b)
saveFile ="imgSave.png" # 保存文件的路径
# cv2.imwrite(saveFile, img3, [int(cv2.IMWRITE_PNG_COMPRESSION), 8]) # 保存图像文件, 设置压缩比为8
cv2.imwrite(saveFile, b)# 保存图像文件
frame_to2(b)
process2 = (
ffmpeg
.input('pipe:',format='rawvideo',pix_fmt='rgb24',s='{}x{}'.format(width, height))
.output(server_url,vcodec="libx264",format=video_format,pkt_size=1316,preset="ultrafast",tune="zerolatency")
.overwrite_output()
.run_async(pipe_stdin=True)
)
flag=0
def frame_to2(out_frame):
global flag
if(flag==1):
flag=0
else:
process2.stdin.write(
out_frame
.astype(np.uint8)
.tobytes()
)
flag=1
# if(++i<100):
# process2.stdin.close()