Python和OpenCV - 无法编写可读的avi视频文件 [英] Python and OpenCV - Cannot write readable avi video files

查看:5314
本文介绍了Python和OpenCV - 无法编写可读的avi视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的代码:

import numpy as np
import cv2


cap = cv2.VideoCapture('C:/Users/Hilman/haatsu/drive_recorder/sample/3.mov')

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

但是, code> output.avi 无法播放。

But the output.avi cannot be played.

尝试也可以更改 out = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))到这样的东西(由某些人建议) out = cv2.VideoWriter('output.avi',-1,20.0,(640,480))。但是当我这样做时,我收到了这个消息

Tried also change the out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480)) to something like this (as suggested by some people) out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480)). But when I did this, I got this message

OpenCV:没有找到FFMPEG:标签0xffffffff /''(格式'avi / AVI音频视频交错)')'

可能是什么问题?我正在使用Windows 10。

What could be the problem? I am using Windows 10 by the way.

推荐答案

我无法将该代码运行在我的Windows 10机器上。

I couldn't get that code to run on my Windows 10 machine either.

所以这就是我所做的:


  1. 我遵循这些说明,并在机器上安装了最新的ffmpeg版本:

  1. I followed these instructions and installed the latest ffmpeg build on machine:

  1. 下载适用于Windows的最新静态版本然后解压缩文件。您可能需要7zip解压缩。

  2. 在C:\中创建一个文件夹,名为 ffmpeg

  3. 将提取的文件的内容复制到C:\ffmpeg

  4. 编辑您的PATH环境变量,最后附加以下条目:
    C:\ffmpeg\bin;

  5. 通过打开cmd提示符并输入以下内容,确认所有内容都正常运行(注意,您可能需要运行cmd作为管理员):
    ffmpeg -version

  1. Download the latest static build for Windows and then extract the files. You may need 7zip to extract.
  2. Create a folder in C:\ called ffmpeg
  3. Copy the contents of the extracted files into C:\ffmpeg
  4. Edit your PATH environment variable to append at the end the following entry: C:\ffmpeg\bin;
  5. Confirm that everything is working correct by opening a cmd prompt and enter the following (Note you might need to run cmd as administrator): ffmpeg -version


  • 修改代码如下:

  • _

    import numpy as np
    import cv2
    import os
    
    base_path = 'C:\\Users\\Hilman\\haatsu\\drive_recorder\\sample\\'
    cap = cv2.VideoCapture('%s3.mov' % base_path)
    
    i = 0
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret==True:
            frame = cv2.flip(frame,0)
            cv2.imwrite(os.path.join(base_path, str(i) + '.png'), frame)
            i = i + 1
        else:
            break
    
    # Release everything if job is finished
    cap.release()  
    




    1. C:\Users\Hilman\haatsu\drive_recorder\sample 并运行以下命令: ffmpeg -framerate 29 -i%d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4

    2. 您的视频应该保存为 out.mp4

    1. Opened a command prompt at C:\Users\Hilman\haatsu\drive_recorder\sample and ran the following command: ffmpeg -framerate 29 -i %d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4
    2. Your video should be saved as out.mp4.

    这篇关于Python和OpenCV - 无法编写可读的avi视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆