Using libavformat and libavcodec(2)

本文介绍如何使用libavformat和libavcodec从视频文件中逐帧解码视频,并处理跨包的帧边界问题。通过示例代码展示了如何读取并过滤视频流包,以及如何将解码后的YUV420格式转换为RGB格式。

Decoding Video Frames

As I've already mentioned, a video file can contain several audio and video streams, and each of those streams is split up into packets of a particular size. Our job is to read these packets one by one using libavformat, filter out all those that aren't part of the video stream we're interested in, and hand them on to libavcodec for decoding. In doing this, we'll have to take care of the fact that the boundary between two frames can occur in the middle of a packet.

 

Sound complicated? Luckily, we can encapsulate this whole process in a routine that simply returns the next video frame:

 

Now, all we have to do is sit in a loop, calling GetNextFrame() until it return false. Just one more thing to take care of: Most codecs return images in YUV420(one luminance and two chrominance channels, with the chrominance channels samples at half the spatial resolution of the luminance channel). Depending on what you want to do with the video data, you may want to convert this to RGB. (Note, though, that this is not necessary if all you want to do is diplay the video data; take a look at the X11 Xvideo extension, which does YUV-to-RGB and scaling in hardware.) Fortunately, libavcodec provides a conversion routine called img_convert, which does conversion between YUV and RGB as well as a variety of other image formats. The loop that decodes the video thus becomes:

 

The RGB image pFrameRGB(of type AVFrame*) is allocated like this:

 

 

 

 

ed psutil-7.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (279 kB) Downloading scipy-1.15.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (35.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 35.5/35.5 MB 14.6 MB/s eta 0:00:00 Downloading tornado-6.5.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (443 kB) Downloading absl_py-2.3.1-py3-none-any.whl (135 kB) Using cached jinja2-3.1.6-py3-none-any.whl (134 kB) Using cached MarkupSafe-3.0.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21 kB) Installing collected packages: tornado, scipy, psutil, ml-dtypes, MarkupSafe, decorator, cloudpickle, attrs, absl-py, jinja2 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. op-compile-tool 0.1.0 requires getopt, which is not installed. op-compile-tool 0.1.0 requires inspect, which is not installed. op-compile-tool 0.1.0 requires multiprocessing, which is not installed. Successfully installed MarkupSafe-3.0.2 absl-py-2.3.1 attrs-25.3.0 cloudpickle-3.1.1 decorator-5.2.1 jinja2-3.1.6 ml-dtypes-0.5.1 psutil-7.0.0 scipy-1.15.3 tornado-6.5.1 (wanxiang_env) [root@localhost pythonenv]# sudo yum install -y pkg-config libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev libavfilter-dev libswscale-dev libswresample-dev Repository appstream is listed more than once in the configuration Repository baseos is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository powertools is listed more than once in the configuration github_git-lfs 88 B/s | 833 B 00:09 github_git-lfs-source 155 B/s | 833 B 00:05 软件包 pkgconf-pkg-config-1.4.2-1.el8.aarch64 已安装。 未找到匹配的参数: libavformat-dev 未找到匹配的参数: libavcodec-dev 未找到匹配的参数: libavdevice-dev 未找到匹配的参数: libavutil-dev 未找到匹配的参数: libavfilter-dev 未找到匹配的参数: libswscale-dev 未找到匹配的参数: libswresample-dev 错误:没有任何匹配: libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev libavfilter-dev libswscale-dev libswresample-dev (wanxiang_env) [root@localhost pythonenv]# 报错了
07-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值