摄像头原始数据读取——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;
}
复制代码
相关推荐
wsoz3 分钟前
Leetcode矩阵-day7
c++·算法·leetcode·矩阵
Emberone7 分钟前
C++内存管理+模板初尝试:暴风雨前的尝试
c++
6Hzlia8 分钟前
【Hot 100 刷题计划】 LeetCode 79. 单词搜索 | C++ 标准方向数组 DFS 与回溯
c++·leetcode·深度优先
忙什么果10 分钟前
海洋遥感论文中常说的:in-situ数据和proxy
linux·运维·服务器
山河君19 分钟前
从后验到先验:语音信噪比估计与DD算法详解
算法·音视频·语音识别·信号处理
bukeyiwanshui21 分钟前
20260413 条件测试常用语法
linux
齐潇宇28 分钟前
Tomcat服务
linux·运维·网络·http·tomcat·web应用
沐雪轻挽萤33 分钟前
4. C++17新特性-内联变量 (Inline Variables)
开发语言·c++
木下~learning35 分钟前
嵌入式Linux 小项目:RK3399 基于 MPlayer 实现视频播放器(从环境搭建到完整播放列表)
linux·运维·嵌入式硬件·音视频
DO your like36 分钟前
Vim编辑器指令
linux·编辑器·vim