How to open webcam using avformat_open_input()

For the developers that use FFmpeg in Windows software.

How to open webcam using avformat_open_input()

Postby taansari » Mon Nov 12, 2012 11:09 am

Hi!

I've been experimenting with ffmpeg since some time now; and recently joined the forums. I first set up Linux environment, then also the Windows environment. Since I'm more comfortable with Windows, decided to carry on with experimentation with Windows only.

I want to read input stream from a webcam (currently I'm using SoC PC-Camera as input), decode the stream, encode it into h264 format, and save (and eventually) transmit it over RTP/UDP, or RTSP. With files residing on HDisk, I can do the decode/encode sequence as well as UDP transmission successfully (only video part, which is also very buggy), but it works to start.

Now I want to read live stream from this webcam; so when I call avformat_open_input() function and specify "video=SoC PC-Camera" as input (also only "SoC PC-Camera"), it does not work (gives me error -2). Since on Windows we have only dll files, I cannot debug into code. So I ported it back on Linux box, and tried debugging there; but am still clueless why this is not opening properly.

This is example code of what I do:

Code: Select all
   av_register_all();
   avcodec_register_all();
   avformat_network_init();
   const char ip[90] = "SoC PC-Camera";
        AVFormatContext* pInputFileFormatCtx = NULL;
        int rv = avformat_open_input(pInputFileFormatCtx, ip, NULL, NULL);

Of course I've chopped off lots of code unrelated to this question.

Can anyone tell me what I could be doing wrong; also, isn't there code sample anywhere that can tell me how to steam from live webcam?

Thanks in advance for any guidance!
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby rogerdpack » Mon Nov 12, 2012 5:03 pm

viewtopic.php?f=15&t=274 may be helpful for you, though I've never tried it programmatically, only from the command line using ffmpeg.exe, but it...should...be possible :)
-r
rogerdpack
 
Posts: 1454
Joined: Fri Aug 05, 2011 9:56 pm

Re: How to open webcam using avformat_open_input()

Postby taansari » Tue Nov 13, 2012 3:58 am

rogerdpack wrote:http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=15&t=274 may be helpful for you, though I've never tried it programmatically, only from the command line using ffmpeg.exe, but it...should...be possible :)
-r

Hi rogerdpack!

Thank you for the prompt reply; I'll check it out in coding and get back shortly.
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby taansari » Tue Nov 13, 2012 5:31 am

Ok, I did a couple of tests, and tried different combinations in accordance with the post's suggestion, nothing worked. This is my code so far (focusing necessary part only):
Code: Select all
   int rv = 0, i = 0;
   AVInputFormat * a= NULL;
   a = av_find_input_format("dshow");
   if ( a == NULL )
   a = av_find_input_format("mpg");
   if ( a == NULL )
   a = av_find_input_format("yuv2");
   if ( a == NULL )
   a = av_find_input_format("mjpeg");
   if ( a == NULL )
   a = av_find_input_format("mpeg");
   if ( a == NULL )
   a = av_find_input_format("avi");
   if ( a == NULL )
   a = av_find_input_format("wmv");
   if ( a == NULL )
   a = av_find_input_format("libx264");
   if ( a == NULL )
   a = av_find_input_format("h264");
   if ( a == NULL )
   a = av_find_input_format("mpegts");

   AVDictionaryEntry *ad= new AVDictionaryEntry();
   AVDictionary *d = new AVDictionary();
   d->count= 1;
   d->elems= ad;
   ad->value= "SoC PC-Camera";
   ad->key= "video";
   rv = avformat_open_input(&pDecoderData->pInputFileFormatCtx, "video=SoC PC-Camera", a, &d);

What's funny is: 'dshow' is not working for me, I am able to initialize with mpg, mjpeg, avi, wmv, h264 formats though (IIRC), not sure if this is the main reason...

In above sample, return value is -2; if I replace video value with "video=\"SoC PC-Camera\"", I get -22 return value.

Also I am clueless why 'dshow' is not working for me, it is supposed to be linked in with FFmpeg dlls, isn't it?
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby taansari » Tue Nov 13, 2012 10:08 am

Also, one more observation/question:

With the static binaries, when we use something like:
Code: Select all
ffmpeg -f dshow -r 25 -i video="SoC PC-Camera" output.avi

It works flawlessly; albeit format is actually not avi it is mp4, see below debug information:
Code: Select all
c:\ffmpeg>ffmpeg -f dshow -r 25 -i video="SoC PC-Cam
era" output.avi
ffmpeg version N-44459-g8bdba0b Copyright (c) 2000-2012 the FFmpeg developers
  built on Sep 14 2012 18:35:56 with gcc 4.7.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runt
ime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass -
-enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-l
ibfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenj
peg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheo
ra --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-li
bvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --ena
ble-zlib
  libavutil      51. 73.100 / 51. 73.100
  libavcodec     54. 55.100 / 54. 55.100
  libavformat    54. 27.100 / 54. 27.100
  libavdevice    54.  2.100 / 54.  2.100
  libavfilter     3. 16.103 /  3. 16.103
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[dshow @ 0215c4e0] Estimating duration from bitrate, this may be inaccurate
Input #0, dshow, from 'video=SoC PC-Camera':
  Duration: N/A, start: 23511.644000, bitrate: N/A
    Stream #0:0: Video: rawvideo, bgr24, 352x288, 25 tbr, 10000k tbn, 25 tbc
Output #0, avi, to 'output.avi':
  Metadata:
    ISFT            : Lavf54.27.100
    Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 352x288, q=2-31, 200
 kb/s, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> mpeg4)
Press [q] to stop, [?] for help
frame=    7 fps=0.0 q=3.9 size=      43kB time=00:00:00.28 bitrate=1257.4kbits/s
frame=   14 fps= 14 q=4.0 size=      58kB time=00:00:00.56 bitrate= 852.4kbits/s
frame=   25 fps= 16 q=3.2 size=      77kB time=00:00:01.00 bitrate= 634.8kbits/s
frame=   37 fps= 18 q=3.5 size=      97kB time=00:00:01.48 bitrate= 537.8kbits/s
frame=   49 fps= 19 q=3.5 size=     116kB time=00:00:01.96 bitrate= 483.9kbits/s
frame=   59 fps= 20 q=4.3 Lsize=     125kB time=00:00:02.36 bitrate= 435.6kbits/
s
video:119kB audio:0kB subtitle:0 global headers:0kB muxing overhead 5.876246%
Received signal 2: terminating.

So I have reasons to believe dshow is supported... But I run sample code I've posted above using dshow as in: av_find_input_format("dshow"), it retuns NULL, for error (or not found).

Can't I get functionality from dlls exactly the same way I can get with the static binaries? Please advice...

Thanks...
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby rogerdpack » Tue Nov 13, 2012 6:47 pm

http://libav-users.943685.n4.nabble.com ... 46231.html may be useful, though I'm grasping at straws...or maybe https://gist.github.com/3210277
rogerdpack
 
Posts: 1454
Joined: Fri Aug 05, 2011 9:56 pm

Re: How to open webcam using avformat_open_input()

Postby taansari » Wed Nov 14, 2012 5:17 am

rogerdpack wrote:http://libav-users.943685.n4.nabble.com/av-find-input-format-not-finding-formats-td946231.html may be useful, though I'm grasping at straws...or maybe https://gist.github.com/3210277

Thanks for the reply!

I understand now why 'dshow' was not initializing for me, that part is patched now.

But this line:
rv = avformat_open_input(&pDecoderData->pInputFileFormatCtx, "video=SoC PC-Camera", a, &options);
now is giving me -1414092869 as error code (rv).

Once again, many thanks for the help; I will try to correct this issue; meanwhile if anyone has encountered this thing before, or have further clues, do let me know.
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby rogerdpack » Wed Nov 14, 2012 11:40 pm

taansari wrote:
rogerdpack wrote:http://libav-users.943685.n4.nabble.com/av-find-input-format-not-finding-formats-td946231.html may be useful, though I'm grasping at straws...or maybe https://gist.github.com/3210277

Thanks for the reply!

I understand now why 'dshow' was not initializing for me, that part is patched now.

But this line:
rv = avformat_open_input(&pDecoderData->pInputFileFormatCtx, "video=SoC PC-Camera", a, &options);
now is giving me -1414092869 as error code (rv).

Once again, many thanks for the help; I will try to correct this issue; meanwhile if anyone has encountered this thing before, or have further clues, do let me know.



What was the fix?
Anyway based on viewtopic.php?f=15&t=274 possibly you need to add some quotation marks...
rogerdpack
 
Posts: 1454
Joined: Fri Aug 05, 2011 9:56 pm

Re: How to open webcam using avformat_open_input()

Postby taansari » Thu Nov 15, 2012 3:24 am

rogerdpack wrote:
taansari wrote:
rogerdpack wrote:http://libav-users.943685.n4.nabble.com/av-find-input-format-not-finding-formats-td946231.html may be useful, though I'm grasping at straws...or maybe https://gist.github.com/3210277

Thanks for the reply!

I understand now why 'dshow' was not initializing for me, that part is patched now.

But this line:
rv = avformat_open_input(&pDecoderData->pInputFileFormatCtx, "video=SoC PC-Camera", a, &options);
now is giving me -1414092869 as error code (rv).

Once again, many thanks for the help; I will try to correct this issue; meanwhile if anyone has encountered this thing before, or have further clues, do let me know.



What was the fix?
Anyway based on viewtopic.php?f=15&t=274 possibly you need to add some quotation marks...

Actually I was not calling avdevice_register_all() function, thats why 'dshow' wasnt recognized. So I did move forward but got stuck again with the error posted recently, combination of double quotes (with and without) is not working for me still...
taansari
 
Posts: 81
Joined: Fri Sep 28, 2012 6:18 am

Re: How to open webcam using avformat_open_input()

Postby rogerdpack » Thu Nov 15, 2012 3:06 pm

oh wait it's suppose to *not* need quotes oops. and it still doesn't work? what's your console output if you set log level to debug?
-r
rogerdpack
 
Posts: 1454
Joined: Fri Aug 05, 2011 9:56 pm

Next

Return to Using FFmpeg In Windows Software

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 2 guests