Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决';不支持文件类型、文件扩展名不正确或文件已损坏';写视频的时候?_Python_Opencv_Cv2 - Fatal编程技术网

Python 如何解决';不支持文件类型、文件扩展名不正确或文件已损坏';写视频的时候?

Python 如何解决';不支持文件类型、文件扩展名不正确或文件已损坏';写视频的时候?,python,opencv,cv2,Python,Opencv,Cv2,我正在尝试使用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\u fourcc()更改为*“X264”、“H264”、“MP4V”、“

我正在尝试使用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\u 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()

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

  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))
你看过了吗,还有。将流链接更改为视频路径