96SEO 2026-08-15 00:52 0
其实,
在完成了 FFmpeg 解码 的学习后接下来往往是把已经封装好的视频文件进行解复用再配合 SDL 将画面实时渲染出来。话说回来,很多同学在实际开发中会遇到:
# 打开媒体文件
avformat_open_input
# 探测流信息
avformat_find_stream_info
# 遍历流并获取索引
for { …}
# 循环读取压缩数据包
while == 0) { …}
开始
↓ . 打开视频文件 ✅
↓ . 查找流信息 ✅
↓ . 获得流索引 ✅
↓ . 查找视频解码器
↓ . 创建解码器上下文
↓ . 将流参数复制到解码器上下文
↓ . 打开解码器
↓ . 初始化 SDL
↓ . 读取数据包 ✅
↓ . 发送数据包到解码器
↓ . 接收解码帧
↓ . SDL 更新纹理并渲染
↓ . 冲刷解码器
↓ . 释放所有资源
结束
#include
#include
#include
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavutil/avutil.h"
#include "libavformat/avformat.h"
}
#include "SDL.h"
作用:打开本地文件或网络流,读取媒体容器头部并创建 FormatContext。

| 参数 | 类型 | 说明 |
|---|---|---|
| ps | FormatContext ** | 输出指针,函数内部会分配并填充。传入 NULL 即可, |
| url | const char * | 媒体方法或 URL。 |
| fmt | const InputFormat * | 强制指定封装格式,传 NULL 时自动探测。 |
| options | Dictionary ** | 可选字典,用于设置超时、缓存等高级选项。 |
Pain Point:If you ignore return value and it’s negative,context will be NULL and later calls will segfault. Always check and call PrintErr.
FormatContext *demux_ctx = nullptr;int ret = avformat_open_input;if {
std:的观点是,cout < "avformat_open_input failed!" < std::endl;
PrintErr;
goto FAILED;
}
作用:Lazily 探测每条流的详细参数。 为后续查找合适的解码器提供依据。
| 参数 | 类型 | 说明 |
|---|---|---|
| ic | FormatContext * | 已打开的格式上下文。 |
| options | Dictionary ** | 探测行为的可选控制,通常为 NULL。 |
Pain Point:The function may return ERROR_EOF if file is truncated. In that case you still get partial stream info – check for warnings in log.
ret = avformat_find_stream_info;if {
std:的观点是,cout < "avformat_find_stream_info failed!" < std::endl;
PrintErr;
goto FAILED;
}
You can eir iterate manually or use newer helper API.
// 手动遍历
int video_index = -1。audio_index = -1;for {
CodecParameters *par = demux_ctx->streams->codecpar;if video_index = i;else if audio_index = i;老实说,}
// 新 API
video_index = av_find_best_stream(demux_ctx。MEDIA_TYPE_VIDEO,-1,-1,NULL,0);audio_index = av_find_best_stream(demux_ctx。MEDIA_TYPE_AUDIO,-1,-1,NULL,0);
Pain Point:If you forget to check that -1!= video_index。subsequent calls will crash because you’ll access an invalid stream.
This is core of demuxing – it pulls one packet from container.
| 参数说明表格同上略,为保持简洁请自行参考官方文档。 |
|---|
Packet *pkt = av_packet_alloc;其实,while {
ret = av_read_frame;if break,// 正常结束
if { // 错误处理
PrintErr;break,}
if {
// 视频包交给后面的解码环节处理
// ...
}
av_packet_unref;// 必须释放内部缓冲区
}
av_packet_free;
// 得到 codec_id 并寻找对应的 decoder
CodecID codec_id = demux_ctx->streams->codecpar->codec_id;const Codec *decoder = avcodec_find_decoder;if { /* 错误处理 */ }
CodecContext *dec_ctx = avcodec_alloc_context3;if { /* 错误处理 */ }
// 把流参数拷贝进 decoder context
ret = avcodec_parameters_to_context(dec_ctx,demux_ctx->streams->codecpar);话说回来,if { /* 错误处理 */ }
ret = avcodec_open2;if { /* 错误处理 */ }
SDL_Init;int screen_w=0,screen_h=0;其实,SDL_DisplayMode dm;if ) {
screen_w = dm.w;screen_h = dm.h;
}
int video_w = dec_ctx->width;怎么说呢,int video_h = dec_ctx->height;// 按比例缩放至不超过屏幕尺寸
double aspect = video_w / video_h;if {
video_w = screen_w;video_h = static_cast;}
if {
video_h = screen_h;video_w = static_cast;}
SDL_Window *win = SDL_CreateWindow("FFmpeg + SDL 播放"。SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,video_w,video_h,0);SDL_Renderer *rend = SDL_CreateRenderer(win,-1。SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);SDL_Texture *tex = SDL_CreateTexture(rend,SDL_PIXELFORMAT_IYUV。SDL_TEXTUREACCESS_STREAMING,dec_ctx->width,dec_ctx->height);SDL_Rect rect={0,0,video_w,video_h};
Packet *pkt = av_packet_alloc;其实,Frame *frame = av_frame_alloc;while )==0){
if{ // 跳过非视频包
av_packet_unref;不过,continue,}
ret=avcodec_send_packet;// 把压缩包喂给 decoder
av_packet_unref;// 可立即归还
while )==0){
// 更新纹理并渲染到窗口
SDL_UpdateYUVTexture(tex,NULL,frame->data。frame->linesize,frame->data,frame->linesize,frame->data,frame->linesize);SDL_RenderClear;SDL_RenderCopy;SDL_RenderPresent;// 简单帧率控制
double fps = av_q2d;int delay_ms= static_cast;SDL_Delay,}
}
// 冲刷剩余帧
avcodec_send_packet;while==0){
// 同上渲染剩余帧...
}
// 清理资源
av_packet_free;av_frame_free;avcodec_free_context;avformat_close_input);SDL_DestroyTexture;SDL_DestroyRenderer;SDL_DestroyWindow;SDL_Quit,
-22。-541478725,等等。使用 a vstrerror *一定*.SdlDelay,audio will drift. 在实际项目中请使用音频时钟同步或 libswresample 做重采样。#extern "C".
-
#define main 为 Windows 项目做兼容:在使用 SDL 时一定要把源码文件命名为 Main.cpp或者 添加#undef main .
.作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback