在pyav python中读取视频流字节时是否有任何硬件解码或编码?

发布于 01-15 12:03 字数 918 浏览 6 评论 0原文

我有一个使用 pyav 包读取视频字节的 python 脚本,如下代码所示。当我使用这段代码时,我发现使用大量 CPU RAM 读取长流的视频字节会使所有核心都很忙(CPU 使用率高)。但在我的项目中,我想以较低的CPU使用率实时处理它们,所以我想知道是否有任何硬件编码或硬件解码用于读取pyav中的流(即基于GPU而不是基于CPU)

    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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗2025-01-22 12:03:05

根据您的需要调整此示例,您需要创建一个 Codec 上下文,如下使用 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文