摄像头原始数据读取——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;
}
复制代码
相关推荐
David猪大卫2 小时前
【Linux修炼】环境变量、进程地址空间
linux·c语言·经验分享·笔记·学习·ubuntu·centos
叼烟扛炮3 小时前
Linux 学习笔记(五十六):IO 多路复用初探——select 的“三宗罪”
linux·服务器·数据库·笔记·学习·php
汉克老师3 小时前
CSP-J 2026 第二轮入门组真题解析(第一题 、旅游)
c++·csp-j·信息学竞赛·小学生学c++
Ruiery3 小时前
Linux 6.6内核内存管理深度解析(十五):CMA 连续内存分配器
linux·运维·服务器
9527华安4 小时前
FPGA纯verilog实现DP视频接口,支持2K60帧分辨率,提供3套工程源码和技术支持
fpga开发·音视频·dp
xiaoye-duck4 小时前
《C++ 项目:手写大模型接入 SDK》Gemini接口设计(上):初始化、全量响应实现和测试
开发语言·网络·c++·语言模型
测试界的小码农4 小时前
【Linux 命令实战(01):认识 Linux 终端,从零掌握第一个命令】
linux·运维·服务器
布裘4 小时前
【银河麒麟】重启后时区丢失?排查发现 localtime 被业务程序替换成硬链接
linux·运维·网络
GeW7 小时前
高端化转型进入“补底层”阶段:Linux与数据库的再定位
linux
UIU1148 小时前
P1481 魔族密码
c++·算法·动态规划