摄像头原始数据读取——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;
}
复制代码
相关推荐
45288655上山打老虎6 小时前
C++完美转发
java·jvm·c++
SunkingYang8 小时前
程序崩溃闪退——MFC共享内存多次OpenFileMapping和MapViewOfFile而没有相应的UnmapViewOfFile和CloseHandle
c++·mfc·共享内存·崩溃·闪退·close·openfilemapping
问君能有几多愁~8 小时前
C++ 日志实现
java·前端·c++
夜月yeyue8 小时前
Linux内高端内存
linux·运维·单片机·嵌入式硬件·ci/cd·硬件架构
JANGHIGH8 小时前
c++ 多线程(二)
开发语言·c++
珑墨8 小时前
【浏览器】页面加载原理详解
前端·javascript·c++·node.js·edge浏览器
猫豆~9 小时前
nginx实战-PHP——day2
linux·centos·云计算
杨云龙UP9 小时前
MySQL 自动备份与覆盖恢复实战:一套脚本搞定全库/按库备份恢复
linux·运维·数据库·sql·mysql
三小尛9 小时前
Linux的常见指令
linux
starvapour9 小时前
Ubuntu下sudo的免密操作
linux·ubuntu