Is there any hardware decoding or encoding when reading video stream byte in pyav python?

1.3k Views Asked by At

I have a python scripts for reading video bytes using pyav package as below code. when I use this code I figured out that reading that video byte for a long stream using a lot amount of CPU RAM and makes all cores busy(High CPU usage). But in my project I want to process them in realtime with lower CPU usage, so I want to know is there any hardware encoding or hardware decoding for reading stream in pyav(i.e GPU based instead of CPU based)

    def _get_video_stream_info(self, video_bytes):
        stream_options = [{'codec': 'h264'}]

        self.container = av.open(video_bytes, stream_options=stream_options)

        video_stream = [s for s in self.container.streams if s.type == "video"][0]

        fps = int(video_stream.average_rate)
        total_frames = video_stream.frames
        logger.info(f'[INFO] FPS video straem = {fps}')
        logger.info(f'[INFO] Total frames video straem = {total_frames}')

        num_split_frames = self.duration_split * fps

        packet_list = []
        for packet in self.container.demux(video_stream):
            packet_list.append(packet)

        return packet_list
1

There are 1 best solutions below

7
On

Adapt this example to your fits, you need to create a Codec context,below with Nvidia cuda

import av

video = av.open(VIDEO_FILE_PATH)
target_stream = video.streams.video[0]
ctx = av.Codec('h264_cuvid', 'r').create()
for packet in 
    video.demux(target_stream):
    for frame in ctx.decode(packet):
        print(frame)