Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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?

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

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)) 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?