I'm trying to finely control the video encoding of camera image frames captured on the fly using skvideo.io.FFmpegWriter
and cv2.VideoCapture
, e.g.
from skvideo import io
import cv2 fps = 60
stream = cv2.VideoCapture(0) # 0 is for /dev/video0
print("fps: {}".format(stream.set(cv2.CAP_PROP_FPS, fps))) stream.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
print("bit_depth: {}".format(stream.set(cv2.CAP_PROP_FORMAT, cv2.CV_8U))) video = io.FFmpegWriter('/tmp/test_ffmpeg.avi', inputdict={'-r': fps, '-width': 1920, '-height': 1080}, outputdict={'-r': fps, '-vcodec': 'libx264', '-pix_fmt': 'h264'}
) try: for i in range(fps*10): # 10s of video ret, frame = stream.read() video.writeFrame(frame)
finally: stream.release() try: video.close()
except: pass
However, I get the following exception (in Jupyter notebook):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last) in () 18 while range(fps*10): 19 ret, frame = stream.read()
---> 20 video.writeFrame(frame) 21 except BaseException as err: 22 raise err /usr/local/lib/python3.6/site-packages/skvideo/io/ffmpeg.py in writeFrame(self, im) 446 T, M, N, C = vid.shape 447 if not self.warmStarted:
--> 448 self._warmStart(M, N, C) 449 450 # Ensure that ndarray image is in uint8 /usr/local/lib/python3.6/site-packages/skvideo/io/ffmpeg.py in _warmStart(self, M, N, C) 412 cmd = [_FFMPEG_PATH + "/" + _FFMPEG_APPLICATION, "-y"] + iargs + ["-i", "-"] + oargs + [self._filename] 413 --> 414 self._cmd = "".join(cmd) 415 416 # Launch process TypeError: sequence item 3: expected str instance, int found
Changing this to video.writeFrame(frame.tostring())
results in ValueError: Improper data input
, leaving me stumped.
How should I go about writing each frame (as returned by OpenCV) to my FFmpegWriter instance?
EDIT
The code works fine if I remove inputdict
and outputdict
from the io.FFmpegWriter
call, however this defeats the purpose for me as I need fine control over the video encoding (I am experimenting with lossless/near-lossless compression of the raw video captured from the camera and trying to establish the best compromise in terms of compression vs fidelity for my needs).