摄像头原始数据读取——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;
}
复制代码
相关推荐
Soneswy21 分钟前
Linux系统篇--重定向与文件操作命令
linux
有毒的教程25 分钟前
Linux底层存储路由配
linux·运维·网络
Linux系统运维工程师_郭主任2 小时前
运维人还在被拒offer?现在能考的,只有 RHCE 10.0。
linux
自养Agent2 小时前
MCP Server Boundary Audit Report
linux·服务器·人工智能·windows·大模型·llm
半杯咖啡半行码2 小时前
C++11核心特性详解:从C++11到C++20 的现代C++指南
开发语言·c++
Coisinier3 小时前
docker命令管理镜像及容器
linux·运维·nginx·docker·容器·操作系统
All for pursuit.3 小时前
【位运算-3】136.只出现一次的数字
数据结构·c++·算法·leetcode
A心有千千结3 小时前
通过 SSH 反向代理让服务器借用本机网络访问 GitHub 的最佳实践
linux·服务器·网络·git·ssh·github
书生执笔画浮沉4 小时前
Linux 内核模块为什么不能自己解决兼容性问题?
linux·c·kernel·update·灵感
15Moonlight4 小时前
Linux网络(03):Socket 编程 TCP
linux·网络·tcp/ip