Python and OpenCV - Cannot write readable avi video files

2024/11/14 21:51:08

I have a code like this:

import numpy as np
import cv2cap = 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 frameout.write(frame)cv2.imshow('frame',frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakelse:break# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

But the output.avi cannot be played.

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: tag 0xffffffff/'    ' is not found (format 'avi / AVI (Audio Video Interleaved)')'.

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

Answer

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

So here's what I did:

  1. I followed these instructions and installed the latest ffmpeg build on machine:
    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
  2. Modified your code as follows:

_

import numpy as np
import cv2
import osbase_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 + 1else:break# Release everything if job is finished
cap.release()  
  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.
https://en.xdnf.cn/q/71850.html

Related Q&A

Python as FastCGI under windows and apache

I need to run a simple request/response python module under an existing system with windows/apache/FastCGI.All the FastCGI wrappers for python I tried work for Linux only (they use socket.fromfd() and …

Scrapy shell return without response

I have a little problem with scrapy to crawl a website. I followed the tutorial of scrapy to learn how crawl a website and I was interested to test it on the site https://www.leboncoin.fr but the spide…

How to replace values using list comprehension in python3?

I was wondering how would you can replace values of a list using list comprehension. e.g. theList = [[1,2,3],[4,5,6],[7,8,9]] newList = [[1,2,3],[4,5,6],[7,8,9]] for i in range(len(theList)):for j in r…

Installed PySide but cant import it: no module named PySide

Im new to Python. I have both Python 2.7 and Python 3 installed. I just tried installing PySide via Homebrew and got this message:PySide package successfully installed in /usr/local/lib/python2.7/sit…

How to run SQLAlchemy on AWS Lambda in Python

I preapre very simple file for connecting to external MySQL database server, like below:from sqlalchemy import *def run(event, context):sql = create_engine(mysql://root:[email protected]/scraper?chars…

saving csv file to s3 using boto3

I am trying to write and save a CSV file to a specific folder in s3 (exist). this is my code: from io import BytesIO import pandas as pd import boto3 s3 = boto3.resource(s3)d = {col1: [1, 2], col2: […

httplib2, how to set more than one cookie?

As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:response, content = http.requ…

FTP upload file works manually, but fails using Python ftplib

I installed vsFTP in a Debian box. When manually upload file using ftp command, its ok. i.e, the following session works:john@myhost:~$ ftp xxx.xxx.xxx.xxx 5111 Connected to xxx.xxx.xxx.xxx. 220 Hello,…

Baktracking function which calculates change exceeds maximum recursion depth

Im trying to write a function that finds all possible combinations of coins that yield a specified amount, for example it calculates all possible way to give change for the amount 2 British pounds from…

How to interface a NumPy complex array with C function using ctypes?

I have a function in C that takes an array of complex floats and does calculations on them in-place.:/* foo.c */ void foo(cmplx_float* array, int length) {...}The complex float struct looks like this:t…