摄像头原始数据读取——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;
}
复制代码
相关推荐
mi200611 分钟前
银河麒麟上tabby和electerm两款终端工具比较
linux·运维
Laurence11 分钟前
从零到一构建 C++ 项目(IDE / 命令行双轨实现)
前端·c++·ide
muyan912 分钟前
统信uos-server-20-1070e-arm64-20250704-1310 安装mysql-5.7.44
linux·mysql·yum·rpm·uos·统信
muyan912 分钟前
浅吐槽一下统信uos linux
linux·运维·国产化·uos·统信·去ioe
我在人间贩卖青春17 分钟前
cout语句和cin语句
c++·cin·输入输出流·cout
Jiu-yuan21 分钟前
C++文件操作
c++
LaoWaiHang25 分钟前
Linux基础知识14:文件使用权限信息
linux
习惯就好zz25 分钟前
[Android/Linux] 实战记录:利用 Kconfig 精确控制 i.MX8MM 特定 DTB 的编译生成
android·linux·dts·dtb·lunch·多卡板配置
2301_7634725835 分钟前
实时系统下的C++编程
开发语言·c++·算法
小义_37 分钟前
【Docker】知识三
linux·docker·云原生·容器