01 ffmpeg对摄像头的调用编程

来源:互联网 发布:it服务外包是什么 编辑:程序博客网 时间:2024/05/30 02:26
库的分支:  libavcodec  // audio video codec 音视频的编解码库    libavformat  //  与容器相关的操作. 文件的格式  libswscale    // 转换数据格式  libavdevice   // 操作如摄像头,声卡设备文件的库   libavfilter   // 可加水印
头文件:#define __STDC_CONSTANT_MACROSextern "C" {    #include <libavcodec/avcodec.h>    #include <libavformat/avformat.h>    #include <libavdevice/avdevice.h>}
AVInputFormat类型表示一种输入文件/输入设备的数据格式AVFormatContext类型表示音视频的格式内容AVDictionary类型表示相关的配置项,如摄像头设备有图像大小的配置 AVPacket类型用于装载一帧数据
    1. av_register_all();         //注册能用的编解码器及文件格式    2. avdevice_register_all();   //注册能操作的输入输出设备    3. 准备参数       AVInputFormat *ifmt = av_find_input_format("v4l2"); ///指定要获取视频输入设备           AVFormatContext *fmtContxt = avformat_alloc_context();           AVDictionary *options = NULL;       av_dict_set(&options, "video_size", "640x480", 0); //设置摄像头的图像大小        //摄像头可配置的选项,可参考ffmpeg源码/libavdevice/v4l2.c                  4. 使用指定的参数, 打开指定的设备        if (avformat_open_input(&fmtContxt, "/dev/video0", ifmt, &options) < 0)        {        perror("avformat open");        return 1;        }    5. 打开设备后, 可检查摄像头的图像格式及分辨率.        AVCodecID id = fmtContxt->streams[0]->codec->codec_id; //获取数据的格式ID        qDebug() << id << "   " << avcodec_get_name(id);          // usb摄像头有两种, mjpeg和rawvideo(yuyv422)        qDebug() << fmtContxt->streams[0]->codec->width;        qDebug() << fmtContxt->streams[0]->codec->height;    6. 准备一帧数据的空间,及读取数据:         AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));        av_read_frame(fmtContxt, packet); //读取一帧数据        // packet->data指针指向图像数据缓冲区的首地址        // packet->size是图像数据的真实大小        // 如果图像格式是mjpeg,则把数据存成文件,就是一张jpg图像了        // 如果是rawvideo,  则数据是一张图像的yuyv数据.

pro文件:

LIBS += -lavformat -lavcodec -lavdevice -lavutil -lz -lm
参考:    http://my.csdn.net/leixiaohua1020