ffmpeg从USB摄像头采集一张原始图片
2014-06-23 13:41
417 查看
本文讲解使用ffmpeg从USB摄像头中采集一帧数据并写入文件保存,测试平台使用全志A20平台,其他平台修改交叉工具链即可移植。开发环境使用eclipse+CDT。交叉工具链使用arm-linux-gcc4.4版本。
ffmpeg库的移植和编译请参考博客http://blog.csdn.net/smilefyx/article/details/32714743,开发板usb摄像头支持配置请参照博客http://blog.csdn.net/smilefyx/article/details/29574783。转载请声明。
/*
*FileName:avcodec.cpp
*Author:yingxianFei
*Description:capture one frame data from usb camera and save into file named out.yuv .
**/
#include <iostream>
using namespace std;
extern "C" {
#include <math.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include <libavutil/dict.h>
};
int main(int argc, char **argv) {
int ret;
AVFormatContext *fmtCtx = NULL;
AVPacket pkt1, *pcaket = &pkt1;
/*1、注册*/
avcodec_register_all();
avdevice_register_all();
/*2、连接视频源*/
AVInputFormat *inputFmt = av_find_input_format("video4linux2");
if (NULL != inputFmt) {
std::cout << "input device name:" << inputFmt->name << std::endl;
} else {
std::cout << "Null point!" << std::endl;
}
/*3、打开视频采集设备*/
ret = avformat_open_input(&fmtCtx, "/dev/video0", inputFmt, NULL);
if (0 == ret) {
std::cout << "Open input device seccess!" << std::endl;
}
/*4、读取一帧数据,编码依据摄像头类型而定,我使用的摄像头输出的是yuv422格式*/
av_read_frame(fmtCtx, pcaket);
std::cout << "packet size:" << (pcaket->size) << std::endl;
/*5、写入帧数据到文件*/
FILE *fp = NULL;
fp = fopen("out.yuv", "wb");
if (NULL != fp) {
//将数据写入文件
fwrite(pcaket->data, 1, pcaket->size, fp);
}
//关闭文件
fclose(fp);
/*6、释放读取的帧数据*/
av_free_packet(pcaket);
/*7、关闭视频输入源*/
avformat_close_input(&fmtCtx);
return 0;
}
使用Pyuv工具可以打开文件查看文件内容,打开时按照自己摄像头的参数进行选项配置,如本文测试效果如下:
ffmpeg库的移植和编译请参考博客http://blog.csdn.net/smilefyx/article/details/32714743,开发板usb摄像头支持配置请参照博客http://blog.csdn.net/smilefyx/article/details/29574783。转载请声明。
1、工程建立
使用eclipse建立一个空的C++工程,工程目录请参照http://blog.csdn.net/smilefyx/article/details/32714743博文。工程建立后在编译选项中加入-D__STDC_CONSTANT_MACROS,否则会报一个错,可以自己尝试。2、编写源代码
打开刚才创建工程时创建的avcodec.cpp文件,编写测试代码,本文测试代码如下,代码比较简单,没做相应的错误检测等操作:/*
*FileName:avcodec.cpp
*Author:yingxianFei
*Description:capture one frame data from usb camera and save into file named out.yuv .
**/
#include <iostream>
using namespace std;
extern "C" {
#include <math.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include <libavutil/dict.h>
};
int main(int argc, char **argv) {
int ret;
AVFormatContext *fmtCtx = NULL;
AVPacket pkt1, *pcaket = &pkt1;
/*1、注册*/
avcodec_register_all();
avdevice_register_all();
/*2、连接视频源*/
AVInputFormat *inputFmt = av_find_input_format("video4linux2");
if (NULL != inputFmt) {
std::cout << "input device name:" << inputFmt->name << std::endl;
} else {
std::cout << "Null point!" << std::endl;
}
/*3、打开视频采集设备*/
ret = avformat_open_input(&fmtCtx, "/dev/video0", inputFmt, NULL);
if (0 == ret) {
std::cout << "Open input device seccess!" << std::endl;
}
/*4、读取一帧数据,编码依据摄像头类型而定,我使用的摄像头输出的是yuv422格式*/
av_read_frame(fmtCtx, pcaket);
std::cout << "packet size:" << (pcaket->size) << std::endl;
/*5、写入帧数据到文件*/
FILE *fp = NULL;
fp = fopen("out.yuv", "wb");
if (NULL != fp) {
//将数据写入文件
fwrite(pcaket->data, 1, pcaket->size, fp);
}
//关闭文件
fclose(fp);
/*6、释放读取的帧数据*/
av_free_packet(pcaket);
/*7、关闭视频输入源*/
avformat_close_input(&fmtCtx);
return 0;
}
3、编译测试
编译工程,将生成的源文件nfs到开发板上,运行可执行文件,成功后将保存一帧数据到out.yuv文件中。使用Pyuv工具可以打开文件查看文件内容,打开时按照自己摄像头的参数进行选项配置,如本文测试效果如下:
相关文章推荐
- ffmpeg从USB摄像头采集一张原始图片(转)
- zedboard--USB摄像头(V4L2接口)的图片采集(十二)
- 基于USB摄像头视频数据采集和利用FFMPEG库函数进行视频数据压缩
- 基于ZedBoard的Webcam设计(二):USB摄像头图片采集+QT显示
- ffmpeg 压缩摄像头采集的图片为*.H264文件
- USB摄像头(V4L2接口)的图片采集 (一)
- 基于ZedBoard的Webcam设计(一):USB摄像头(V4L2接口)的图片采集
- linux下usb摄像头采集的YUYV格式转换成JPEG格式的图片
- 基于ZedBoard的Webcam设计(二):USB摄像头图片采集+QT显示
- 基于ZedBoard的Webcam设计(一):USB摄像头(V4L2接口)的图片采集
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- 基于USB摄像头视频数据采集和利用FFMPEG库函数进行视频数据压缩
- linux下usb摄像头采集的YUYV格式转换成JPEG格式的图片
- 基于ZedBoard的Webcam设计(二):USB摄像头图片采集+QT显示
- (原创)基于ZedBoard的Webcam设计(二):USB摄像头图片采集+QT显示
- 使用 Video4Linux 采集USB摄像头的图像,并保存到一张 jpg图像文件中的程序
- Linux下用FFMPEG采集usb摄像头到RTMP
- Linux下用FFMPEG采集usb摄像头到RTMP
- [转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片