摄像头原始数据读取——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;
}
复制代码
相关推荐
MediaTea1 天前
Python 库手册:wave WAV 音频读写工具
开发语言·python·音视频
EasyDSS1 天前
视频推流平台EasyDSS无人机推流直播技术在智慧消防场景中的应用
音视频·无人机
虾..1 天前
Linux 进程间通信---命名管道
linux·运维·服务器
Once_day1 天前
CC++八股文之基础语法(2)
c语言·c++
小宇的天下1 天前
Calibre DESIGNrev DRC/LVS启动和准备文件(10-2)
linux·运维·lvs
一只旭宝1 天前
Linux专题五:fork函数进阶,其在内存,进程上的关系,以及进程替换
linux
dawnButterfly1 天前
C 语言标准、编译器与操作系统的关系
c语言·开发语言·c++
lifewange1 天前
Linux 日志查看命令速查表
java·linux·运维
AndyHeee1 天前
【rk3576 BSP音频开发bug记录】
linux·驱动开发
程序员龙一1 天前
进程、线程、协程通俗讲解与对比
c++·线程·进程·协程