当前位置: 首页 > article >正文

C++ 使用 ffmpeg 解码本地视频并获取每帧的YUV数据

一、简介

FFmpeg 是一个‌开源的多媒体处理框架‌,非常适用于处理音视频的录制、转换、流化和播放。

二、代码

示例代码读取一个本地视频文件,解码并将二进制文件保存下来。

注意: 代码中仅展示了 YUV420P 格式,其他 NV12/NV21 等格式可相应修改。

1. yuv_decoder.cpp

#include <iostream>
#include <iomanip>
#include <fstream>extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libavutil/rational.h>
}// 打印媒体信息表格
void print_media_info(AVFormatContext* fmt_ctx, int video_stream) {std::cout << "\n\033[1;36m======= Media Stream Analysis =======\033[0m\n";std::cout << std::left << std::setw(20) << "Container Format:" << fmt_ctx->iformat->long_name << " (" << fmt_ctx->iformat->name << ")\n";std::cout << std::setw(20) << "Duration:" << (fmt_ctx->duration / AV_TIME_BASE) << " seconds\n";std::cout << std::setw(20) << "Bitrate:" << (fmt_ctx->bit_rate / 1000) << " Kbps\n";if (video_stream != -1) {AVStream* stream = fmt_ctx->streams[video_stream];std::cout << "\n\033[1;33m[Video Stream #" << video_stream << "]\033[0m\n";std::cout << std::setw(20) << "Codec:" << avcodec_get_name(stream->codecpar->codec_id) << " (ID:" << stream->codecpar->codec_id << ")\n";std::cout << std::setw(20) << "Resolution:" << stream->codecpar->width << "x" << stream->codecpar->height << "\n";std::cout << std::setw(20) << "Pixel Format:" << av_get_pix_fmt_name((AVPixelFormat)stream->codecpar->format) << "\n";std::cout << std::setw(20) << "Framerate:" << av_q2d(stream->avg_frame_rate) << " fps\n";std::cout << std::setw(20) << "Timebase:" << stream->time_base.num << "/" << stream->time_base.den << "\n";}std::cout << "\033[1;36m=====================================\033[0m\n\n";
}// 验证YUV420P格式
void validate_yuv420p(const AVFrame* frame) {if (frame->format != AV_PIX_FMT_YUV420P || frame->format != AV_PIX_FMT_YUVJ420P) {std::cerr << "\033[1;31mERROR: Unexpected pixel format: " << av_get_pix_fmt_name((AVPixelFormat)frame->format) << "\033[0m" << std::endl;}if (frame->linesize[1] != frame->linesize[2]) {std::cerr << "\033[1;31mERROR: UV plane stride mismatch: U=" << frame->linesize[1] << " V=" << frame->linesize[2]<< "\033[0m" << std::endl;}
}int main(int argc, char** argv) {if (argc < 2) {std::cerr << "Usage: " << argv[0] << " <input_file>" << std::endl;return -1;}AVFormatContext* fmt_ctx = nullptr;if (avformat_open_input(&fmt_ctx, argv[1], nullptr, nullptr) != 0) {std::cerr << "Could not open input file" << std::endl;return -1;}if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) {std::cerr << "Could not find stream information" << std::endl;avformat_close_input(&fmt_ctx);return -1;}int video_stream_index = -1;for (unsigned int i = 0; i < fmt_ctx->nb_streams; i++) {if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {video_stream_index = i;break;}}if (video_stream_index == -1) {std::cerr << "Could not find video stream" << std::endl;avformat_close_input(&fmt_ctx);return -1;}print_media_info(fmt_ctx, video_stream_index);  // 打印媒体信息const AVCodec* codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id);if (!codec) {std::cerr << "Unsupported codec" << std::endl;avformat_close_input(&fmt_ctx);return -1;}AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);if (avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar) < 0) {std::cerr << "Could not initialize codec context" << std::endl;avcodec_free_context(&codec_ctx);avformat_close_input(&fmt_ctx);return -1;}if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {std::cerr << "Could not open codec" << std::endl;avcodec_free_context(&codec_ctx);avformat_close_input(&fmt_ctx);return -1;}std::cout << "\033[1;32mDecoder Initialized Successfully!\033[0m\n";std::cout << "Output Format: " << av_get_pix_fmt_name(codec_ctx->pix_fmt) << "\n\n";AVFrame* frame = av_frame_alloc();AVPacket* pkt = av_packet_alloc();int frame_count = 0;while (av_read_frame(fmt_ctx, pkt) >= 0) {if (pkt->stream_index == video_stream_index) {if (avcodec_send_packet(codec_ctx, pkt) < 0) {std::cerr << "Error sending packet" << std::endl;break;}int ret = 0;while (ret >= 0) {ret = avcodec_receive_frame(codec_ctx, frame);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;std::cout << "Decoded Frame #" << frame_count << " (PTS: " << frame->pts << ", Format: " << av_get_pix_fmt_name((AVPixelFormat)frame->format)<< ")\r" << std::flush;validate_yuv420p(frame);  // 格式校验,可略过char filename[20];snprintf(filename, sizeof(filename), "%04d.yuv", frame_count++);std::ofstream file(filename, std::ios::binary);if (file.is_open()) {// 写入Y平面for (int y = 0; y < frame->height; y++) {file.write(reinterpret_cast<char*>(frame->data[0] + y * frame->linesize[0]), frame->width);}// 写入U平面for (int y = 0; y < frame->height / 2; y++) {file.write(reinterpret_cast<char*>(frame->data[1] + y * frame->linesize[1]), frame->width / 2);}// 写入V平面for (int y = 0; y < frame->height / 2; y++) {file.write(reinterpret_cast<char*>(frame->data[2] + y * frame->linesize[2]), frame->width / 2);}}}}av_packet_unref(pkt);}std::cout << "\n\n\033[1;32mDecoding Completed! Total frames: " << frame_count << "\033[0m\n";av_packet_free(&pkt);av_frame_free(&frame);avcodec_free_context(&codec_ctx);avformat_close_input(&fmt_ctx);return 0;
}

2. CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(FFmpegYUVDecoder)set(CMAKE_CXX_STANDARD 11)find_package(PkgConfig REQUIRED)
pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVFORMAT REQUIRED libavformat)
pkg_check_modules(AVUTIL REQUIRED libavutil)add_executable(yuv_decoder yuv_decoder.cpp)target_include_directories(yuv_decoder PRIVATE${AVCODEC_INCLUDE_DIRS}${AVFORMAT_INCLUDE_DIRS}${AVUTIL_INCLUDE_DIRS}
)target_link_libraries(yuv_decoder${AVCODEC_LIBRARIES}${AVFORMAT_LIBRARIES}${AVUTIL_LIBRARIES}
)

依赖的库在安装 ffmpeg 后应该都有,用到的就 libavcodec,libavformat,libavutil三个。

3. 文件目录结构

|—— CMakeLists.txt
|—— yuv_decoder.cpp

三、编译和运行

1. 安装 ffmpeg

如果还没有安装过,可以如下安装

# Ubuntu/Debian
sudo apt update && sudo apt install ffmpeg
# 查看版本号
ffmpeg -version

如果缺少某个库,可以执行

sudo apt install libavcodec-dev libavformat-dev libavutil-dev

2. 编译

mkdir build
cd build
cmake ..
make -j4

3. 运行

只有一个输入参数,及输入视频的文件路径,输出路径默认在当前路径。

./yuv_decoder input.mp4

4. 查看

可以使用 ffplay 命令查看保持的 yuv 数据是否正确。以 1920x1080 大小,yuv420p格式为例:

ffplay -video_size 1920x1080 -pixel_format yuv420p 0001.yuv
http://www.lryc.cn/news/2400635.html

相关文章:

  • 分布式微服务系统架构第143集:pom文件
  • 2.0 阅读方法论与知识总结
  • 5. Qt中.pro文件(1)
  • 第八部分:第三节 - 事件处理:响应顾客的操作
  • 共识机制全景图:PoW、PoS 与 DAG 的技术对比
  • 学习笔记085——Spring Data JPA笔记
  • 可视化大屏工具对比:GoView、DataRoom、积木JimuBI、Metabase、DataEase、Apache Superset 与 Grafana
  • 内网穿透:打破网络限制的利器!深入探索和简单实现方案
  • 如何选择合适的哈希算法以确保数据安全?
  • 简数采集技巧之快速获取特殊链接网址URL方法
  • React 性能监控与错误上报
  • AI 如何改变软件文档生产方式?
  • 激光干涉仪:解锁协作机器人DD马达的精度密码
  • Windows如何定制键盘按键
  • go语言学习 第1章:走进Golang
  • 使用Prometheus+Grafana+Alertmanager+Webhook-dingtalk搭建监控平台
  • HOPE800系列变频器安装到快速调试的详细操作说明
  • vCenter与ESXi主机每分钟周期性断连修复
  • JMeter函数整理
  • Sql Server 中常用语句
  • web3-区块链困境破解指南:从数字化签名到Rollup 到分片
  • 李飞飞World Labs开源革命性Web端3D渲染器Forge!3D高斯溅射技术首次实现全平台流畅运行
  • 小鹏汽车5月交付新车33525台 同比增长230%
  • OpenCV——Mat类及常用数据结构
  • 深入解析FutureTask:原理与实战
  • 每天总结一个html标签——Audio音频标签
  • 使用 React Native 开发鸿蒙(HarmonyOS)运动健康类应用的系统化准备工作
  • web3-Remix部署智能合约到“荷兰式”拍卖及以太坊gas费机制细讲
  • 网络编程及原理(一)
  • superior哥AI系列第9期:高效训练与部署:从实验室到生产环境