基于USB摄像头视频数据采集和利用FFMPEG库函数进行视频数据压缩
2019-07-13 15:51发布
生成海报
-
从一篇博客中看到的代码,觉得很有用,暂时没时间研究就先转载保存先。参考http://blog.csdn.net/yakimin/article/details/19284467
-
后面可以通过tcp传输等,在pc上或者开发板上把yuv视频解码,然后再把yuv转RGB播放出来。同时之前找了个解码的播放器有源码,可以结合两者实现此功能。链接:
-
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
#define VIDEO_WIDTH 640
-
#define VIDEO_HEIGHT 480
-
#define VIDEO_FORMAT V4L2_PIX_FMT_YUYV
-
#define BUFFER_COUNT 4
-
#define URL_WRONLY 1
-
-
struct fimc_buffer {
-
int length;
-
void *start;
-
} framebuf[BUFFER_COUNT];
-
-
int fd;
-
unsigned char yuv4200[1000000] = { 0 };
-
unsigned char yuv4220[1000000] = { 0 };
-
-
AVFormatContext* pFormatCtxEnc;
-
AVCodecContext* pCodecCtxEnc;
-
AVFrame* pFrameEnc;
-
-
void register_init();
-
int open_device();
-
int capability();
-
int set_v4l2_format();
-
int request_buffers();
-
int get_camera_data();
-
void unregister_all();
-
void video_encode_init();
-
-
int yuv422_2_yuv420(unsigned char* yuv420, unsigned char* yuv422, int width,
-
int height);
-
-
void register_init() {
-
avcodec_register_all();
-
av_register_all();
-
-
}
-
-
int open_device() {
-
char camera_device[20];
-
struct stat buf;
-
int i;
-
for (i = 0; i < 10; i++) {
-
sprintf(camera_device, "/dev/video%i", i);
-
if (stat(camera_device, &buf) == 0) {
-
break;
-
}
-
-
}
-
fd = open(camera_device, O_RDWR, 0);
-
if (fd < 0) {
-
printf("Cannot open camera_device
");
-
return -1;
-
}
-
-
}
-
-
int set_v4l2_format() {
-
int ret;
-
struct v4l2_format fmt;
-
memset(&fmt, 0, sizeof(fmt));
-
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
fmt.fmt.pix.width = VIDEO_WIDTH;
-
fmt.fmt.pix.height = VIDEO_HEIGHT;
-
fmt.fmt.pix.pixelformat = VIDEO_FORMAT;
-
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
-
ret = ioctl(fd, VIDIOC_S_FMT, &fmt);
-
if (ret < 0) {
-
printf("VIDIOC_S_FMT failed
");
-
return ret;
-
}
-
-
ret = ioctl(fd, VIDIOC_G_FMT, &fmt);
-
if (ret < 0) {
-
printf("VIDIOC_G_FMT failed (%d)/n", ret);
-
return ret;
-
}
-
-
}
-
-
int request_buffers() {
-
int ret;
-
int i;
-
struct v4l2_requestbuffers reqbuf;
-
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
reqbuf.memory = V4L2_MEMORY_MMAP;
-
reqbuf.count = BUFFER_COUNT;
-
ret = ioctl(fd, VIDIOC_REQBUFS, &reqbuf);
-
if (ret < 0) {
-
printf("VIDIOC_REQBUFS failed
");
-
return
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮