简体   繁体   English

使用C ++访问网络摄像头的FFMPEG

[英]FFMPEG with C++ accessing a webcam

I have searched all around and can not find any examples or tutorials on how to access a webcam using ffmpeg in C++. 我四处搜索,找不到任何关于如何使用C ++中的ffmpeg访问网络摄像头的示例或教程。 Any sample code or any help pointing me to some documentation, would greatly be appreciated. 任何示例代码或任何帮助指向我的一些文档,将不胜感激。

Thanks in advance. 提前致谢。

I have been working on this for months now. 我已经做了几个月了。 Your first "issue" is that ffmpeg (libavcodec and other ffmpeg libs) does NOT access web cams, or any other device. 您的第一个“问题”是ffmpeg(libavcodec和其他ffmpeg库)不访问网络摄像头或任何其他设备。

For a basic USB webcam, or audio/video capture card, you first need driver software to access that device. 对于基本USB网络摄像头或音频/视频采集卡,首先需要驱动程序软件才能访问该设备。 For linux, these drivers fall under the Video4Linux (V4L2 as it is known) category, which are modules that are part of most distros. 对于Linux,这些驱动程序属于Video4Linux(众所周知的V4L2)类别,这些模块是大多数发行版的一部分。 If you are working with MS Windows, then you need to get an SDK that allows you to access the device. 如果您使用的是MS Windows,那么您需要获得一个允许您访问该设备的SDK。 MS may have something for accessing generic devices, (but from my experience, they are not very capable, if they work at all) If you've made it this far, then you now have raw frames (video and/or audio). MS可能有一些东西可以访问通用设备,(但根据我的经验,如果他们工作的话他们不是很有能力)如果你已经做到这一点,那么你现在有了原始帧(视频和/或音频)。

THEN you get to the ffmpeg part - libavcodec - which takes the raw frames (audio and/or video) and encodes them into a streams, which ffmpeg can then mux into your final container. 然后你到了ffmpeg部分 - libavcodec - 它接收原始帧(音频和/或视频)并将它们编码成流,然后ffmpeg可以复制到你的最终容器中。

I have searched, but have found very few examples of all of these, and most are piece-meal. 我搜索过,但发现所有这些都很少的例子,而且大部分都是零食。

If you don't need to actually code of this yourself, the command line ffmpeg, as well as vlc, can access these devices, capture and save to files, and even stream. 如果您不需要自己实际编写代码,命令行ffmpeg以及vlc可以访问这些设备,捕获并保存到文件,甚至是流。

That's the best I can do for now. 这是我现在能做的最好的事情。

ken

For windows use dshow 对于Windows使用dshow

For Linux (like ubuntu) use Video4Linux (V4L2). 对于Linux(如ubuntu),请使用Video4Linux(V4L2)。

FFmpeg can take input from V4l2 and can do the process. FFmpeg可以从V4l2获取输入并且可以执行该过程。

To find the USB video path type : ls /dev/video* Eg : /dev/video(n) where n = 0 / 1 / 2 …. 要查找USB视频路径类型:ls / dev / video *例如:/ dev / video(n)其中n = 0/1/2 ....

AVInputFormat – Struct which holds the information about input device format / media device format. AVInputFormat - 包含有关输入设备格式/媒体设备格式的信息的Struct。

av_find_input_format ( “v4l2”) [linux] av_find_input_format(“v4l2”)[linux]

av_format_open_input(AVFormatContext , “/dev/video(n)” , AVInputFormat , NULL) if return value is != 0 then error. av_format_open_input(AVFormatContext,“/ dev / video(n)”,AVInputFormat,NULL)如果返回值为!= 0则出错。

Now you have accessed the camera using FFmpeg and can continue the operation. 现在您已使用FFmpeg访问摄像头并可以继续操作。

sample code is below. 示例代码如下。

    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

Note : Header file < libavdevice/avdevice.h > must be included 注意:必须包含头文件<libavdevice / avdevice.h>

This really doesn't answer the question as I don't have a pure ffmpeg solution for you, However, I personally use Qt for webcam access. 这真的没有回答这个问题,因为我没有为您提供纯粹的ffmpeg解决方案,但是,我个人使用Qt进行网络摄像头访问。 It is C++ and will have a much better API for accomplishing this. 它是C ++,将有一个更好的API来实现这一目标。 It does add a very large dependency on your code however. 但它确实会对您的代码添加非常大的依赖性。

It definitely depends on the webcam - for example, at work we use IP cameras that deliver a stream of jpeg data over the network. 这绝对取决于网络摄像头 - 例如,在工作中我们使用IP摄像机通过网络传输jpeg数据流。 USB will be different. USB会有所不同。

You can look at the DirectShow samples, eg PlayCap (but they show AmCap and DVCap samples too). 您可以查看DirectShow样本,例如PlayCap (但它们也显示AmCap和DVCap样本)。 Once you have a directshow input device (chances are whatever device you have will be providing this natively) you can hook it up to ffmpeg via the dshow input device . 一旦你有了一个directshow输入设备(你可能有本机提供的任何设备),你可以通过dshow输入设备将它连接到ffmpeg。

And having spent 5 minutes browsing the ffmpeg site to get those links, I see this ... 并且花了5分钟浏览ffmpeg网站来获取这些链接,我看到 ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM