[Libav-user] avformat_open_input can not open input file (example code)

Wenpeng Zhou zhou.wenpeng at rdnet.fi
Wed Oct 31 08:48:18 CET 2012


Hi,

I try to use USB camera to capture live stream with FFMpeg. But I cannot
open dshow USB camera.
What is wrong with my code?

Thanks!

#include <iostream>
#include <libavformat/avformat.h>

using namespace std;
int main (int argc, char **argv)
{
	int frame_width  = 640;
	int frame_height = 480;
	int frame_rate = 30;
	int video_bit_rate = 100*1000;

	av_register_all();
	avformat_network_init();

	AVCodec *encoder_mjpeg;
	encoder_mjpeg=avcodec_find_encoder(CODEC_ID_MJPEG);

	AVFormatContext *pFormatCtx = avformat_alloc_context();
	AVStream *st = avformat_new_stream(pFormatCtx, encoder_mjpeg);
	st->codec = avcodec_alloc_context();
    st->codec->bit_rate = video_bit_rate;
    st->codec->width = frame_width;
    st->codec->height = frame_height;
    st->codec->pix_fmt = PIX_FMT_YUVJ420P; 
    st->codec->time_base.den = frame_rate;
    st->codec->time_base.num = 1;
    
   if (!encoder_mjpeg || avcodec_open(st->codec, encoder_mjpeg) < 0){
  		av_log(NULL, AV_LOG_ERROR, "Unable to open mjpeg
encoder!\n");
                
        }
 AVInputFormat * a= av_find_input_format("dshow");
int r = avformat_open_input(&pFormatCtx, "video=Mercury USB2.0 Camera", a,
NULL);
if(r < 0){
      av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
      cout<<"The returned value is:"<<r<<endl;
      }
return 0;
}



More information about the Libav-user mailing list