命令行采集
ffmpeg在windows下查看设备的命令:
ffmpeg -list_devices true -f dshow -i dummy
ffmpeg在windows下查看摄像机支持的编码参数
ffmpeg -list_options true -f dshow -i video="Integrated Webcam"
ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 out.mkv
ffmpeg -f dshow -i video="Integrated Webcam" -r 5 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency out1.mp4
ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 mycamera.h264
ffplay 播放摄像头采集的视频
//播放yuv数据
ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480
//播放y
ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='y'
//播放u
ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='u'
//播放v
ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='v'
调用ffmpeg接口采集
#include <iostream>
#include <strsafe.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "libavutil/avutil.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil\samplefmt.h"
#include <stdio.h>
}
#ifdef _MSC_VER
#include <tchar.h>
#include <dshow.h>
#include <atlcomcli.h>
#pragma comment(lib, "Strmiids.lib")
#endif
using namespace std;
int main()
{
int ret = 0;
AVFormatContext* fmt_ctx = NULL;
AVDictionary* options = NULL;
char errors[1024] = { 0 };
char device_name[256] = "video=Integrated Webcam";
char file_name[256] = "collection.yuv";
char name[128] = { 0 };
char name_utf8[128] = { 0 };
av_register_all();
avdevice_register_all();
AVInputFormat* in_format = av_find_input_format("dshow");
if (in_format == NULL)
{
printf("av_find_input_format error\n");
}
//640*480 30 也可以采集
av_dict_set(&options, "video_size", "1280*720", 0);
av_dict_set(&options, "framerate", "10", 0);
av_dict_set(&options, "pixel_format", "yuyv422", 0);
if ((ret = avformat_open_input(&fmt_ctx, device_name, in_format, &options)) != 0)
{
av_strerror(ret, errors, 1024);
printf("Failed to open video device, [%s][%d]\n", errors, ret);
return -1;
}
AVPacket* pkt = av_packet_alloc();
av_init_packet(pkt);
FILE* out_file = fopen(file_name, "wb+");
av_dump_format(fmt_ctx, 0, device_name, 0);
while (!av_read_frame(fmt_ctx, pkt))
{
printf("Size of collected data %d\n", pkt->size);
//640 * 480 * 2
// (宽*高)*(yuv420=1.5/yuv422=2/yuv444=3)
fwrite(pkt->data, 1, 1280 * 720 *2, out_file);
fflush(out_file);
av_packet_unref(pkt);
}
av_packet_free(&pkt);
avformat_close_input(&fmt_ctx);
fclose(out_file);
// ffplay -video_size 640*480 -pixel_format yuyv422 -framerate 30 collection.yuv
return 0;
}
注:调用接口采集时发现,每个采集的分辨率都对应一套自己的参数
video_size —-> 640*480
framerate ——> 30
pixel_format ——> 摄像头自身决定,不能更改