如何解决在编写视频时“文件类型不受支持、文件扩展名不正确或文件已损坏”的问题?

2024-05-01 21:53:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用OpenCV将视频写入媒体文件。视频播放良好,mp4文件写入媒体文件。然而,当我打开书面视频时,我收到了这条信息

This file isn't playable. That might be because the file type is unsupported, the file extension is incorrect, or the file is corrupt

我已将VideoWriter_fourcc()更改为*“X264”、“H264”、“MP4V”、“MP4V”和*“MPEG”。所有这些都没有解决这个问题

import cv2

path = "C:/Users/gri_o/Desktop/opencvProjects/ProjectOne/testVideos/myvideo.mp4"

video_selected = cv2.VideoCapture(path)

vid_cod = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('media/testOne.mp4', vid_cod, 20.0, (640,480))
while(True):
    ret,frame = video_selected.read()
    if ret == True:

        out.write(frame)
        cv2.imshow('frame', frame)
    # Press Q on keyboard to  exit
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    else:
        break

video_selected.release()
out.release()

Tags: thepath视频isvideooutcv2frame
1条回答
网友
1楼 · 发布于 2024-05-01 21:53:41

误差可能是由于中提到的宽度和高度引起的

  out = cv2.VideoWriter('media/testOne.mp4', vid_cod, 20.0, (640,480))

获取框架的宽度和高度,如下所示

w, h = frame.shape[:2]
out = cv2.VideoWriter('media/testOne.mp4', vid_cod, 20.0, (w,h))

相关问题 更多 >