摄像头原始数据读取——ffmpeg(av_read_frame)

摄像头原始数据读取------ffmpeg(av_read_frame)

测试代码test.cpp

cpp 复制代码
#include <iostream>
#include <stdio.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif
#include "libavdevice/avdevice.h"
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "libavutil/avutil.h"
#include <libavfilter/avfilter.h>
#include <libavutil/log.h>

#ifdef __cplusplus
};
#endif

int main(int argc, char *argv[]) 
{   
    int count=0;
    AVPacket pack_data;
    AVFormatContext *fmt_ctx = NULL;
    AVDictionary *video_args = NULL;
    AVInputFormat *inputformat=NULL;

    avdevice_register_all();

    inputformat = av_find_input_format("video4linux2");
	
	//设置视频设备格式参数
    av_dict_set(&video_args, "video_size", "1280x720", 0);
    av_dict_set(&video_args, "framerate", "10", 0);
    av_dict_set(&video_args, "pixel_format", "yuyv422", 0);
    
    std::string video_dev_name = "/dev/video0";
    //打开视频设备,并设置参数
    int ret = avformat_open_input(&fmt_ctx, video_dev_name.c_str(), inputformat, &video_args);
    if(ret < 0) 
    {
        char errors[1024];
        av_strerror(ret, errors, 1024);
        std::cerr<<"failed to open video capture device:"<<errors<<std::endl;
        return -1;
    }  

    //读取视频帧数据
    while((av_read_frame(fmt_ctx, &pack_data) == 0) && (count++ < 100)) 
    {
        std::string yuvfile="picture_yuv/"+std::to_string(count)+".yuv";
        FILE *yuv_pic_file = fopen(yuvfile.c_str(), "wb+");
        fwrite(pack_data.data, 1, pack_data.size, yuv_pic_file);
        fclose(yuv_pic_file);

        av_packet_unref(&pack_data); // release package data
    }

    return 0;
}
复制代码
相关推荐
云深处@4 分钟前
【C++11】特殊类&&单例模式
开发语言·c++
IT 行者5 分钟前
OpenClaw 浏览器自动化测试的那些坑(一):Linux Snap 版本的 Chromium 无法使用托管模式
linux·运维·服务器·人工智能
ADDDDDD_Trouvaille7 分钟前
2026.2.15——OJ83-85题
c++·算法
Tangcan-35 分钟前
在Ubuntu 22.04上安装redis
linux·redis·ubuntu
麦德泽特37 分钟前
STM32与Linux的无缝协作:通过USB CDC/VCP实现高效数据交互
linux·stm32·单片机·嵌入式硬件·安全
Ronaldinho Gaúch39 分钟前
算法题中的日期问题
开发语言·c++·算法
zenpluck2 小时前
RTAB-Map学习记录(1)--论文阅读
c++·论文阅读·学习·机器人
郝学胜-神的一滴2 小时前
Effective Modern C++ 条款36:如果有异步的必要请指定std::launch::async
开发语言·数据结构·c++·算法
小此方2 小时前
Re:从零开始的 C++ STL篇(六)一篇文章彻底掌握C++stack&queue&deque&priority_queue
开发语言·数据结构·c++·算法·stl
REDcker2 小时前
FFmpeg开发者快速入门
ffmpeg