FFMPEG with C++ accessing a webcam

限于喜欢 提交于 2019-12-06 08:50:56

问题


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.

Thanks in advance.


回答1:


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.

For a basic USB webcam, or audio/video capture card, you first need driver software to access that device. For linux, these drivers fall under the Video4Linux (V4L2 as it is known) category, which are modules that are part of most distros. If you are working with MS Windows, then you need to get an SDK that allows you to access the device. 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).

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.

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.

That's the best I can do for now.

ken




回答2:


For windows use dshow

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

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

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

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

av_find_input_format ( “v4l2”) [linux]

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

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

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




回答3:


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. It is C++ and will have a much better API for accomplishing this. It does add a very large dependency on your code however.




回答4:


It definitely depends on the webcam - for example, at work we use IP cameras that deliver a stream of jpeg data over the network. USB will be different.

You can look at the DirectShow samples, eg PlayCap (but they show AmCap and DVCap samples too). 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.

And having spent 5 minutes browsing the ffmpeg site to get those links, I see this...



来源:https://stackoverflow.com/questions/22342869/ffmpeg-with-c-accessing-a-webcam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!