[Libav-user] Write X264 to MP4

Edimartin edimartin at gmail.com
Fri Feb 14 21:13:23 CET 2014


Hi. I am trying to write h264 frames to a MP4 file (no audio) and I am having
problems.

I try one example code but not with success .

I am trying write h264 frames in the file but the VLC don't recognize the
duration. Then I try make a frame to write on the file and it crash.

Here is the source code. It's just a temporary project to teste to write the
file. I am using one class who I made to encode h264 with x264 library.

source code:


#define fileName "test.mp4"

int main(){
    printf("\nHELLO MP4");
    av_register_all();

    unsigned char frame[(unsigned int)(352*240*1.5)];
    H264VideoEncoder encoder;

    //create the context
    AVFormatContext *file=avformat_alloc_context();
    if(file){
        //load the format (MP4)
        AVOutputFormat *format = av_guess_format(NULL, fileName, NULL);
        if(format){
            format->video_codec = CODEC_ID_H264;
            //add the format in the context
            file->oformat = format;

            //copi the fileName
            snprintf(file->filename,sizeof(file->filename),"%s",fileName);
            printf("FileName '%s'",file->filename);fflush(stdout);



            //create a new stream
            AVStream *stream = av_new_stream(file, 0);
            if(stream){
                //
                printf("\nTRUE");fflush(stdout);

                if(avio_open(&file->pb,fileName,AVIO_FLAG_WRITE)>=0){
                    printf("\nTRUE");fflush(stdout);

                    //try write a header file
                    av_write_header(file);
                    //flush the file
                    avio_flush(file->pb);

                    //load the encoder
                    encoder.loadEncoder(352,240,5);//five is the keyframes
number
                    unsigned int count = 0u;
                    for(unsigned int i=0u;i<500;i++){
                        //draw the frame to the encoder
                        encoder.drawYUVinFrame(frame);
                        //process the encoder
                        while(!encoder.encode()){
                            //
                            count++;
                        }

                        AVPacket pkt;
                        av_init_packet(&pkt);
                        pkt.data = encoder.getEncoded();
                        pkt.size = encoder.getEncodedSize();

                        //THE PROGRAM CRASH HERE
                        av_interleaved_write_frame(file, &pkt);
                        pkt.flags        |= AV_PKT_FLAG_KEY;
                        pkt.stream_index  = stream->index;
                        pkt.data = NULL;
                        pkt.size = 0;


                        av_write_frame(file,&pkt);

                        //write the frame in a file
                        /*
                        avio_write(file->pb,
                                   encoder.getEncoded(),
                                   encoder.getEncodedSize()
                                   );
                        */
                    }
                    encoder.deleteEncoder();

                    //close the file
                    avio_close(file->pb);
                }
            }
        }
    }

    return 0;
}

I hope someone can show me a good tutorial to write MP4 files with libav .



--
View this message in context: http://libav-users.943685.n4.nabble.com/Write-X264-to-MP4-tp4659258.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list