本文介绍了带有C ++的FFMPEG访问网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,找不到关于如何使用C ++中的ffmpeg访问网络摄像头的示例或教程.任何使我了解一些文档的示例代码或帮助,都将不胜感激.

I have searched all around and can not find any examples or tutorials on how to access a webcam using ffmpeg in C++. Any sample code or any help pointing me to some documentation, would greatly be appreciated.

谢谢.

推荐答案

对于Windows,请使用dshow

For windows use dshow

对于Linux(如ubuntu),请使用Video4Linux(V4L2).

For Linux (like ubuntu) use Video4Linux (V4L2).

FFmpeg可以从V4l2接收输入并可以执行此过程.

FFmpeg can take input from V4l2 and can do the process.

要查找USB视频路径类型:ls/dev/video *例如:/dev/video(n),其中n = 0/1/2….

To find the USB video path type : ls /dev/video*E.g : /dev/video(n) where n = 0 / 1 / 2 ….

AVInputFormat –结构,包含有关输入设备格式/媒体设备格式的信息.

AVInputFormat – Struct which holds the information about input device format / media device format.

av_find_input_format("v4l2")[linux]

av_find_input_format ( "v4l2") [linux]

av_format_open_input(AVFormatContext,"/dev/video(n)",AVInputFormat,NULL)如果返回值为!= 0,则错误.

av_format_open_input(AVFormatContext , "/dev/video(n)" , AVInputFormat , NULL)if return value is != 0 then error.

现在您已经使用FFmpeg访问了相机,可以继续操作了.

Now you have accessed the camera using FFmpeg and can continue the operation.

示例代码如下.

    int CaptureCam()
  {

  avdevice_register_all(); // for device
  avcodec_register_all();
  av_register_all();

  char *dev_name = "/dev/video0"; // here mine is video0 , it may vary.
  AVInputFormat *inputFormat =av_find_input_format("v4l2");
  AVDictionary *options = NULL;
  av_dict_set(&options, "framerate", "20", 0);

  AVFormatContext *pAVFormatContext = NULL;

    // check video source
  if(avformat_open_input(&pAVFormatContext, dev_name, inputFormat, NULL) != 0)
  {
   cout<<"\nOops, could'nt open video source\n\n";
   return -1;
  }
  else
  {
   cout<<"\n Success !";
  }

  } // end function

注意:头文件< libavdevice/avdevice.h>必须包含

Note : Header file < libavdevice/avdevice.h > must be included

这篇关于带有C ++的FFMPEG访问网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 22:39