使用 FFmpeg 在 C++ 中解码摄像头数据并输出为 H.264 格式涉及几个步骤,包括初始化 FFmpeg,打开视频流,读取帧并解码。下面是一个简单的示例代码,演示了如何用 C++ 和 FFmpeg 库从摄像头捕获和解码 H.264 数据。
示例代码
确保你已经安装了 FFmpeg,并在编译时链接相关库(如 libavformat
、libavcodec
、libavutil
和 libswscale
)。
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
#include <iostream>
int main() {
// 注册所有组件
av_register_all();
// 打开摄像头设备(RTSP 流或 V4L2)
const char* deviceName = "/dev/video0"; // 根据你的系统更改
AVFormatContext* pFormatCtx = nullptr;
if (avformat_open_input(&pFormatCtx, deviceName, nullptr, nullptr) != 0) {
std::cerr << "Couldn't open input stream." << std::endl;
return -1;
}
// 查找流信息
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
std::cerr << "Couldn't find stream info." << std::endl;
avformat_close_input(&pFormatCtx);
return -1;
}
int videoStreamIndex = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; ++i) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
std::cerr << "Couldn't find a video stream." << std::endl;
avformat_close_input(&pFormatCtx);
return -1;
}
AVCodecParameters* pCodecParameters = pFormatCtx->streams[videoStreamIndex]->codecpar;
// 查找解码器
AVCodec* pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
if (!pCodec) {
std::cerr << "Unsupported codec!" << std::endl;
avformat_close_input(&pFormatCtx);
return -1;
}
// 创建解码上下文
AVCodecContext* pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
std::cerr << "Could not allocate codec context." << std::endl;
avformat_close_input(&pFormatCtx);
return -1;
}
// 初始化解码上下文参数
if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) < 0) {
std::cerr << "Failed to copy codec parameters to decoder context." << std::endl;
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
return -1;
}
// 打开解码器
if(avcodec_open2(pCodecCtx, pCodec, nullptr)<0){
std::cerr<<"Could not open codec!"<<std::endl;
av_codec_free_context (&p_codec_ctx);
av_format_close_input (&fmt_ctx);
return-1;
}
AVPacket packet;
AVFrame *frame= av_frame_alloc();
while(av_read_frame(fmt_ctx,&packet)>=0){
if(packet.stream_index==video_stream_index){
int response= av_codec_send_packet(codec_ctx,&packet);
if(response<0){
std :: cerr<<"Error while sending a packet"<<std :: endl;
break ;
}
while(response==0){
response= av_codec_receive_frame(codec_ctx, frame);
if(response==AVERROR(EAGAIN)||response==AVERROR_EOF){
break ;
} else if(response<0){
std :: cerr<<"Error while receiving a frame"<<std :: endl;
break ;
}
// 处理帧数据,进行后续操作,比如保存或显示
std :: cout<<"Frame received!"<<std :: endl;
}
}
av_packet_unref(&packet);
}
// 清理资源
av_frame_free(&frame);
av_packet_unref(&packet);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
编译指令
如果你将上述代码保存为 camera_capture.cpp
文件,可以使用以下命令进行编译:
g++ camera_capture.cpp -o camera_capture -lavformat -lavcodec -lavutil -lswscale
注意事项
- 确保你具有对摄像头设备的访问权限。
- 根据你的操作系统和摄像头类型,可能需要调整设备路径(例如
/dev/video0
)。 - 本示例程序只是一个基本框架,你可以在接收到每一帧后添加进一步的处理逻辑,如显示、编码等。
- 在实际项目中,请注意错误处理和资源管理,以避免内存泄漏和其他问题。
内容由零声教学AI助手提供,问题来源于学员提问