VideoWriter 设置为 2fps 时会导致视频损坏

问题描述 投票:0回答:1

我正在尝试读取视频,从顶部和底部裁剪每个帧,然后写回每个帧并将它们写入 mp4 格式的视频中。 输入视频为 2fps,也是 mp4 格式。 帧保存正确,但我无法将它们写为 2fps 视频。我尝试过其他帧速率,例如 1fps、3fps、5fps、10fps 甚至 25fps。除了 2fps 之外,所有这些都工作正常,这会导致视频损坏(要么除了第一帧之外没有显示任何名气,要么只是显示绿屏) 任何想法或评论将不胜感激:

reader = cv2.VideoCapture('video.mp4') 
cropped_video = os.path.join('video_crop.mp4')
cropped_frames = os.path.join('video/')

if (reader.isOpened() == False):
    print('Error while trying to open video. Plese check again...')
else:   
    # get the frame width and height
    frame_width = int(reader.get(cv2.CAP_PROP_FRAME_WIDTH ))
    frame_height = int(reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
    frame_count = int(reader.get(7))
    fps =  int(reader.get(cv2.CAP_PROP_FPS))
    print('frame_width, frame_height, frame_count, fps = ', frame_width, frame_height, frame_count, fps)

    # define codec and create VideoWriter object
    fourcc= cv2.VideoWriter_fourcc(*'XVID')#*"MP4V")
    writer = cv2.VideoWriter(cropped_video , fourcc, 2, (frame_width, frame_height))

top = 20
botton= frame_height-20

count = 0

# read until end of video
while True:
    ret, frame = reader.read()
    if ret==True:
        cropped = frame[top:botton , :]
        frame = cv2.resize(cropped, (frame_width, frame_height), interpolation = cv2.INTER_NEAREST_EXACT) 
        # save video frame
        cv2.imwrite(os.path.join(cropped_frames,"frame%d.jpg" % count), frame)
        writer.write(frame)
        count += 1
        print(count)
    else:
        break

            

# release VideoCapture()
reader.release()
writer.release()
# close all frames and video windows
cv2.destroyAllWindows()
python opencv video-processing video-capture frame-rate
1个回答
0
投票

我遇到了同样的问题,但是当我用 vlc 播放器(不是我的 Mac 上的默认视频播放器)打开它时,视频看起来不错,所以也许这不是 CV2 中的错误。我也播放过其他操作系统的计算机生成的视频,效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.