First time here? Check out the FAQ!

Ask Your Question
0

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

asked Dec 5 '0

Wilan gravatar image

updated Dec 6 '0

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 = vid.read() gpu_frame = cv2.cuda_GpuMat() gpu_frame.upload(frame) frame = gpu_frame.download() 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?

Preview: (hide)

Comments

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

crackwitz gravatar imagecrackwitz (Dec 5 '0)edit

1 answer

Sort by » oldest newest most voted
0

answered Dec 5 '0

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.

https://docs.opencv.org/master/d0/d61...

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Dec 5 '0

Seen: 5,371 times

Last updated: Dec 06 '20