Ask Your Question

Reading and Writing Videos: Python on GPU with CUDA - VideoCapture and VideoWriter

asked 2020-12-05 09:53:53 -0500

Wilan gravatar image

updated 2020-12-06 07:38:57 -0500

I'm trying to crop a video, using Python 3+, by reading it frame-by-frame and write certain frames to a new video.

I want to use GPU to speed up this process, as for a 1h video, it would take my CPU ~24h to complete.

My understanding is, Reading a video using CPU:

vid = cv2.VideoCapture(vid_path) fps = int(vid.get(cv2.CAP_PROP_FPS)) total_num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) frame_width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))

Writing a video using CPU:

fourcc = cv2.VideoWriter_fourcc(*'mp4v') new_vid = cv2.VideoWriter(new_vid_path, fourcc, fps, (frame_width, frame_height))

Reading a frame on CPU, uploading & downloading it to/from GPU, then writing it using CPU:

ret, frame = gpu_frame = cv2.cuda_GpuMat() gpu_frame.upload(frame) frame = new_vid.write(frame)

Note: I know uploading and downloading here is useless, I wrote it to express how I think the syntax should be used.

Is there a way to do this all on GPU? Or is there another more efficient way to accomplish my goal?

edit retag flag offensive close merge delete


please reformat. block code is indented with four spaces, and no backticks

crackwitz gravatar imagecrackwitz ( 2020-12-05 13:24:28 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-12-05 13:25:52 -0500

crackwitz gravatar image

there is the "cudacodec" module. that ought to support decoding and encoding videos using the GPU's hardware codec.

I don't know how well maintained that is.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-12-05 09:51:45 -0500

Seen: 226 times

Last updated: Dec 06 '20