命令行采集

ffmpeg在windows下查看设备的命令:

  1. ffmpeg -list_devices true -f dshow -i dummy

ffmpeg在windows下查看摄像机支持的编码参数

  1. ffmpeg -list_options true -f dshow -i video="Integrated Webcam"
  1. ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 out.mkv
  2. ffmpeg -f dshow -i video="Integrated Webcam" -r 5 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency out1.mp4
  3. ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 mycamera.h264

ffplay 播放摄像头采集的视频

  1. //播放yuv数据
  2. ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480
  3. //播放y
  4. ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='y'
  5. //播放u
  6. ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='u'
  7. //播放v
  8. ffplay -i F:\virtual-code\yuv_encode.c\video.yuv -pix_fmt yuv420p -s 640x480 -vf extractplanes='v'

调用ffmpeg接口采集

  1. #include <iostream>
  2. #include <strsafe.h>
  3. #define __STDC_CONSTANT_MACROS
  4. extern "C"
  5. {
  6. #include "libavutil/avutil.h"
  7. #include "libavdevice/avdevice.h"
  8. #include "libswscale/swscale.h"
  9. #include "libswresample/swresample.h"
  10. #include "libavutil\samplefmt.h"
  11. #include <stdio.h>
  12. }
  13. #ifdef _MSC_VER
  14. #include <tchar.h>
  15. #include <dshow.h>
  16. #include <atlcomcli.h>
  17. #pragma comment(lib, "Strmiids.lib")
  18. #endif
  19. using namespace std;
  20. int main()
  21. {
  22. int ret = 0;
  23. AVFormatContext* fmt_ctx = NULL;
  24. AVDictionary* options = NULL;
  25. char errors[1024] = { 0 };
  26. char device_name[256] = "video=Integrated Webcam";
  27. char file_name[256] = "collection.yuv";
  28. char name[128] = { 0 };
  29. char name_utf8[128] = { 0 };
  30. av_register_all();
  31. avdevice_register_all();
  32. AVInputFormat* in_format = av_find_input_format("dshow");
  33. if (in_format == NULL)
  34. {
  35. printf("av_find_input_format error\n");
  36. }
  37. //640*480 30 也可以采集
  38. av_dict_set(&options, "video_size", "1280*720", 0);
  39. av_dict_set(&options, "framerate", "10", 0);
  40. av_dict_set(&options, "pixel_format", "yuyv422", 0);
  41. if ((ret = avformat_open_input(&fmt_ctx, device_name, in_format, &options)) != 0)
  42. {
  43. av_strerror(ret, errors, 1024);
  44. printf("Failed to open video device, [%s][%d]\n", errors, ret);
  45. return -1;
  46. }
  47. AVPacket* pkt = av_packet_alloc();
  48. av_init_packet(pkt);
  49. FILE* out_file = fopen(file_name, "wb+");
  50. av_dump_format(fmt_ctx, 0, device_name, 0);
  51. while (!av_read_frame(fmt_ctx, pkt))
  52. {
  53. printf("Size of collected data %d\n", pkt->size);
  54. //640 * 480 * 2
  55. // (宽*高)*(yuv420=1.5/yuv422=2/yuv444=3)
  56. fwrite(pkt->data, 1, 1280 * 720 *2, out_file);
  57. fflush(out_file);
  58. av_packet_unref(pkt);
  59. }
  60. av_packet_free(&pkt);
  61. avformat_close_input(&fmt_ctx);
  62. fclose(out_file);
  63. // ffplay -video_size 640*480 -pixel_format yuyv422 -framerate 30 collection.yuv
  64. return 0;
  65. }

注:调用接口采集时发现,每个采集的分辨率都对应一套自己的参数
video_size —-> 640*480
framerate ——> 30
pixel_format ——> 摄像头自身决定,不能更改