[FFmpeg-trac] #5833(avcodec:new): libavcodec encoding does not work

FFmpeg trac at avcodec.org
Thu Sep 8 02:44:43 EEST 2016


#5833: libavcodec encoding does not work
-------------------------------------+-------------------------------------
             Reporter:               |                     Type:  defect
  AssemblerX86                       |                 Priority:  critical
               Status:  new          |                  Version:  git-
            Component:  avcodec      |  master
             Keywords:               |               Blocked By:
             Blocking:               |  Reproduced by developer:  0
Analyzed by developer:  0            |
-------------------------------------+-------------------------------------
 Summary of the bug:
 Encoder at stream 0 (Mostly) does not get opened.
 How to reproduce:
 {{{
 Try compiling "transcoding.c" example and trying "transcode vid.mp4
 vid2.mp4"
 }}}

 Other than transcoding.c example, I have tried writing my code many times
 and not once it worked to encode a video (Tried different codecs for
 stream #0, but mainly h264), I get the error: "Tag avc1/<hex number here>
 incompatible with output codec id '28'" and then avformat_write_header
 returns error:  "Invalid data found when processing input" and the program
 crashes when writing packets.

 Here is the code:

 {{{
 #include "libavformat/avformat.h"
 #include "libavcodec/avcodec.h"
 #include "libavutil/avutil.h"
 #include "libavutil/rational.h"

 #include <stdio.h>

 int main()
 {

     av_register_all();

     //av_log_set_level(-8);


     AVFormatContext *ps = avformat_alloc_context();

     AVFormatContext *ps2 = avformat_alloc_context();
     AVOutputFormat *oF = av_guess_format("mp4", NULL, "video/mp4");


     if(avformat_open_input(&ps, "vid.mp4", NULL, NULL) != 0)
     {
         printf("Failed to open input file.\n");
         return -1;
     }

     avformat_alloc_output_context2(&ps2, ps->oformat, NULL, "vid2.mp4");

     avio_open(&ps2->pb, "vid2.mp4", AVIO_FLAG_WRITE);

     avformat_find_stream_info(ps, NULL);

    // AVStream *iStream;
     //AVStream *oStream;

     AVCodecContext *pC = avcodec_alloc_context3(NULL), *p2C =
 avcodec_alloc_context3(NULL);
     //AVCodec *encoder;

     AVStream *oStream = NULL;
     AVStream *iStream = NULL;

     AVCodec *encoder = NULL;

     for(unsigned int i = 0; i < ps->nb_streams; i++)
     {
         printf("%d\n", i);
         oStream = avformat_new_stream(ps2, NULL);
         iStream = ps->streams[i];

         avcodec_parameters_copy(oStream->codecpar, iStream->codecpar);
         //pC = iStream->codec;
         //p2C = oStream->codec;


         if(pC->codec_type == AVMEDIA_TYPE_VIDEO || pC->codec_type ==
 AVMEDIA_TYPE_AUDIO)
         {
             //p2C = avcodec_alloc_context3(pC->codec);
             encoder = avcodec_find_encoder(pC->codec_id);



             /*oStream->time_base = iStream->time_base;
             p2C->pix_fmt = AV_PIX_FMT_YUV420P;
             p2C->flags = CODEC_FLAG_GLOBAL_HEADER;
             p2C->width = iStream->codec->width;
             p2C->height = iStream->codec->height;
             p2C->time_base = (AVRational){1,
 iStream->codec->framerate.num};
             p2C->gop_size = iStream->codec->framerate.num;
             p2C->bit_rate = iStream->codec->bit_rate;
             p2C->codec_tag = pC->codec_tag;
             //oStream->pts = iStream->pts;*/

             AVCodecParameters *par = avcodec_parameters_alloc();
             avcodec_parameters_from_context(par, pC);
             avcodec_parameters_to_context(p2C, par);

             avcodec_open2(p2C, encoder, NULL);
             avcodec_open2(pC, avcodec_find_decoder(pC->codec_id), NULL);

             //p2C-> = pC->pts;
         }
     }
     printf("done\n");

     int ret = avformat_write_header(ps2, NULL);
     char err[200];
     av_make_error_string(err, 200, ret);
     printf("Write header %d: %s\n", ret, err);



     AVFrame *rawFrame = av_frame_alloc();

     AVPacket *pkts = av_packet_alloc();
     //av_init_packet(pkts);
     AVPacket *pktr = av_packet_alloc();
     //av_init_packet(pktr);

     while(av_read_frame(ps, pkts) == 0)
     {
         //decoding
         if(avcodec_send_packet(pC, pkts) == 0)
         {
             if(avcodec_receive_frame(pC, rawFrame) == 0)
             {
             //encoding
                 if(avcodec_send_frame(p2C, rawFrame) == 0)
                 {
                     if(avcodec_receive_packet(p2C, pktr) == 0)
                     {
                         printf("Succ dec/enc\n");
                         if(av_interleaved_write_frame(ps2, pktr) != 0)
                         {
                             printf("Failed to write packet\n");
                             break;
                         }
                         printf("packet written\n");
                     }
                 }
             }
         }
     }
     av_write_trailer(ps2);
     printf("Fine\n");
 }
 }}}

 However, on transcoding.c, I get the error: "Cannot open video encoder for
 stream #0 \n Error occurred: Generic error in an external library"

 Tried MSVC 2013 x32 and MinGW32.

--
Ticket URL: <https://trac.ffmpeg.org/ticket/5833>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker


More information about the FFmpeg-trac mailing list