Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AVCODEC_OPEN and AVCODEC_ENCODE_VIDEO fails...

AVCODEC_OPEN and AVCODEC_ENCODE_VIDEO fails...

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gmallax
    wrote on last edited by
    #1

    Hi all, I am working on transcoding using ffmeg.Im having the mpeg4 video and alaw audio which should be converted to the desired video and audio format.Im reading the frame by frame conversion from my media stream using av_read_frame(...)

    int main(int argc, char **argv)
    {
    const char *filename;
    AVOutputFormat *fmt;
    AVFormatContext *oc;
    AVFormatContext *ic;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;
    AVStream *audio_st, *video_st;
    int i,frame,video_stream;

    // initialize libavcodec, and register all codecs and formats 
    av\_register\_all();    
     
    filename = "test.mp4";
    
    if(av\_open\_input\_file(&ic, filename, NULL, 0, NULL)!=0) 
    	return -1;	// Couldn't open file 
    
    // Retrieve stream information
    if(av\_find\_stream\_info(ic)<0) 
    	return -1; // Couldn't find stream information 
    
    // Find the first video stream 
    video\_stream=-1; 
    for(i=0; i<10; i++) 
    if(ic->streams\[i\]->codec->codec\_type==CODEC\_TYPE\_VIDEO) 
    { 
    	video\_stream=i; 
    	break; 
    }
    if(video\_stream==-1) 
    	return -1; // Didn't find a video stream 
    
    // Get a pointer to the codec context for the video stream 
    pCodecCtx=ic->streams\[video\_stream\]->codec; 
    
    // Find the decoder for the video stream 
    pCodec=avcodec\_find\_decoder(pCodecCtx->codec\_id); 
    if(pCodec==NULL) return -1; // Codec not found 
    
    // Open codec 
    if(avcodec\_open(pCodecCtx, pCodec)<0) return -1; // Could not open codec 
    
    // Allocate frames
    AVPacket    pkt;
    AVFrame videoFrame;
    
    // auto detect the output format from the name. default is mpeg. 
    fmt = guess\_format(NULL, filename, NULL);
    if (!fmt) {
        printf("Could not deduce output format from file extension: using MPEG.\\n");
        fmt = guess\_format("mpeg", NULL, NULL);
    }
    if (!fmt) {
        fprintf(stderr, "Could not find suitable output format\\n");
        exit(1);
    }
    
    /\* allocate the output media context \*/
    oc = av\_alloc\_format\_context();
    if (!oc) {
        fprintf(stderr, "Memory error\\n");
        exit(1);
    }
    oc->oformat = fmt;
    \_snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
    
    // Add video stream
    AVStream\* stream = av\_new\_stream(oc, 0);
    AVCodecContext\* videoEncoderContext = stream->codec;
    setupVideoEncode(videoEncoderContext);
    stream->sample\_aspect\_ratio = av\_d2q(1, 255);
    stream->pts\_wrap\_bits = 33;
    stream->codec->thread\_count = 0;
    
    // Set output parameters
    if (av\_set
    
    E 1 Reply Last reply
    0
    • G gmallax

      Hi all, I am working on transcoding using ffmeg.Im having the mpeg4 video and alaw audio which should be converted to the desired video and audio format.Im reading the frame by frame conversion from my media stream using av_read_frame(...)

      int main(int argc, char **argv)
      {
      const char *filename;
      AVOutputFormat *fmt;
      AVFormatContext *oc;
      AVFormatContext *ic;
      AVCodecContext *pCodecCtx;
      AVCodec *pCodec;
      AVStream *audio_st, *video_st;
      int i,frame,video_stream;

      // initialize libavcodec, and register all codecs and formats 
      av\_register\_all();    
       
      filename = "test.mp4";
      
      if(av\_open\_input\_file(&ic, filename, NULL, 0, NULL)!=0) 
      	return -1;	// Couldn't open file 
      
      // Retrieve stream information
      if(av\_find\_stream\_info(ic)<0) 
      	return -1; // Couldn't find stream information 
      
      // Find the first video stream 
      video\_stream=-1; 
      for(i=0; i<10; i++) 
      if(ic->streams\[i\]->codec->codec\_type==CODEC\_TYPE\_VIDEO) 
      { 
      	video\_stream=i; 
      	break; 
      }
      if(video\_stream==-1) 
      	return -1; // Didn't find a video stream 
      
      // Get a pointer to the codec context for the video stream 
      pCodecCtx=ic->streams\[video\_stream\]->codec; 
      
      // Find the decoder for the video stream 
      pCodec=avcodec\_find\_decoder(pCodecCtx->codec\_id); 
      if(pCodec==NULL) return -1; // Codec not found 
      
      // Open codec 
      if(avcodec\_open(pCodecCtx, pCodec)<0) return -1; // Could not open codec 
      
      // Allocate frames
      AVPacket    pkt;
      AVFrame videoFrame;
      
      // auto detect the output format from the name. default is mpeg. 
      fmt = guess\_format(NULL, filename, NULL);
      if (!fmt) {
          printf("Could not deduce output format from file extension: using MPEG.\\n");
          fmt = guess\_format("mpeg", NULL, NULL);
      }
      if (!fmt) {
          fprintf(stderr, "Could not find suitable output format\\n");
          exit(1);
      }
      
      /\* allocate the output media context \*/
      oc = av\_alloc\_format\_context();
      if (!oc) {
          fprintf(stderr, "Memory error\\n");
          exit(1);
      }
      oc->oformat = fmt;
      \_snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
      
      // Add video stream
      AVStream\* stream = av\_new\_stream(oc, 0);
      AVCodecContext\* videoEncoderContext = stream->codec;
      setupVideoEncode(videoEncoderContext);
      stream->sample\_aspect\_ratio = av\_d2q(1, 255);
      stream->pts\_wrap\_bits = 33;
      stream->codec->thread\_count = 0;
      
      // Set output parameters
      if (av\_set
      
      E Offline
      E Offline
      EmoBemo
      wrote on last edited by
      #2

      Try calling

      avcodec_init();
      avcodec_register_all();

      near av_register_all();

      G 1 Reply Last reply
      0
      • E EmoBemo

        Try calling

        avcodec_init();
        avcodec_register_all();

        near av_register_all();

        G Offline
        G Offline
        gmallax
        wrote on last edited by
        #3

        hi I have solved that by assigning correct parameters...(In setupVideoEncode(videoEncoderContext)). But now im getting crash in av_write_frame(..)inside that loop. :(( pls help me..

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups