I am trying to write 16-bit grayscale video using the FFV1 codec and opencv.ImageWriter on Windows 10
Here is my code:
import numpy as np
import cv2, pdb
print(cv2.getBuildInformation())
def to8(img):
return (img/256).astype('uint8')
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','1','6',' '))
cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
out = cv2.VideoWriter('out.avi', cv2.VideoWriter_fourcc('F','F','V','1'), cap.get(cv2.CAP_PROP_FPS), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
while True:
ret, frame = cap.read()
frame = cv2.normalize(frame,None,0,65535,cv2.NORM_MINMAX)
cv2.imshow('Video',to8(frame))
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
And here is my error:
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (415) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): out.avi in function 'cv::icvExtractPattern'
I am running this script from a command window with admin privileges. I've tried both making sure the output file does and does not exist before running.
My OpenCV build information is here: https://pastebin.com/whtF6ixG
Thanks!
EDIT: Based on Rotem's suggestion, instead of using VideoWriter I piped directly to FFMPEG using ffmpeg-python:
import numpy as np
import cv2, pdb
import ffmpeg
def to8(img):
return (img/256).astype('uint8')
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','1','6',' '))
cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
ff_proc = (
ffmpeg
.input('pipe:',format='rawvideo',pix_fmt='gray16le',s='%sx%s'%(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))),r='60')
.output('out3.avi',vcodec='ffv1',an=None)
.run_async(pipe_stdin=True)
)
while True:
ret, frame = cap.read()
cv2.imshow('Video',to8(frame))
ff_proc.stdin.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.terminate()
cap.release()
cv2.destroyAllWindows()