import cv2
import ffmpeg  

vformat='X264'#mp4v,X264,H264
inUrl='/home/th/WJ/data/XunHe/ai_online.mp4'
outUrl='/home/th/WJ/data/XunHe/ai_%s.avi'%(vformat)
# 打开输入视频文件
input_video = cv2.VideoCapture(inUrl)

# 获取视频的帧率和帧大小
fps = input_video.get(cv2.CAP_PROP_FPS)
frame_width = int(input_video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 定义输出视频的编解码器和创建 VideoWriter 对象
#fourcc = cv2.VideoWriter_fourcc(*'%s'%(vformat) )
output_video = cv2.VideoWriter(outUrl, fourcc, fps, (frame_width, frame_height))
#output_video = ffmpeg.VideoWriter(outUrl, vformat, fps, (frame_width, frame_height))
# 逐帧读取视频
iframe=0
while input_video.isOpened():
    ret, frame = input_video.read()
    if not ret:
        break
    if iframe>1000: break
    iframe+=1
    # 将帧写入输出视频
    ret2=output_video.write(frame)
    print(iframe,ret,ret2)
# 释放 VideoCapture 和 VideoWriter 对象
input_video.release()
output_video.release()
