av_write_frame crashes without error message

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

av_write_frame crashes without error message

Drabner
Hey there,

Before I start, I should point out that I have almost no expertise in audio and video codecs, and the same goes for ffmpeg/libav.

I'm trying to get all frames of an audio file (*.wav) and then save those frames to a new audio file with the same format (so a more or less copied file should be the result). This works well.

But now I'm trying to get all video frames of a file (*.mp4) that has both audio and video content.
The getting seems to work fine without any errors.

But when trying to write the frames I got into a new video file with the same format - but this time, using video frames only, no audio - av_write_frame just crashes without giving any clue why.


Here is the function that reads the frames in:
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void
FramesCollection::DecodeVideo(const char* p_filename)
{
    av_log(NULL, AV_LOG_ERROR, "%s","Starting video decoding\n");

    // Get the format context...
    int ret = avformat_open_input(&_videoFormatContext, p_filename, NULL, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "%s","Error opening file '%s'\n", p_filename);
        exit(-1);
    }
    // ... and fill it with stream info (contains number of frames, etc.)
    av_find_stream_info(_videoFormatContext);

    // Find the video stream
    AVStream* stream;
    for(int i = 0; i < (int)_videoFormatContext->nb_streams; i++)
    {
        if(_videoFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            stream = _videoFormatContext->streams[i];
            break;
        }
    }

    // Use stream to set the context
    _videoCodecContext = stream->codec;

    // Find the codec
    _videoCodec = avcodec_find_decoder(_videoCodecContext->codec_id);
    if (!_videoCodec)
    {
        av_log(_videoCodecContext, AV_LOG_ERROR, "%s","codec not found\n");
        exit(-1);
    }
    // Open the codec
    if (avcodec_open(_videoCodecContext, _videoCodec) < 0)
    {
        av_log(_videoCodecContext, AV_LOG_ERROR, "%s","could not open codec\n");
        exit(-1);
    }

    // Read each frame into a packet
    AVPacket packet;
    av_init_packet(&packet);
    int frameCount = 0;
    while (av_read_frame(_videoFormatContext, &packet) == 0)
    {
        // Save the frame
        AVPacket* tempPacket = new AVPacket(packet);
        _videoPackets.push_back(tempPacket);
    } // END reading each frame
    av_free_packet(&packet);
}


And here is the function that should write the new video:
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void
FramesCollection::TestVideoEncode()
{
    // Try writing
    AVFormatContext* outputContext;
    outputContext = avformat_alloc_context();
    outputContext->oformat =
        av_guess_format(NULL, _videoFormatContext->filename, NULL);
    char buffer[512];
    sprintf(buffer, "C:\\test\\outTest.%s", outputContext->oformat->name);
    _snprintf_s(outputContext->filename,
                sizeof(outputContext->filename),
                strlen(buffer),
                "%s", buffer);

    // Add stream
    if (!av_new_stream(outputContext, 0))
    {
        av_log(outputContext, AV_LOG_ERROR, "%s","Error while creating stream.\n");
        exit(-1);
    }

    // Open Codec
    if (avcodec_open(outputContext->streams[0]->codec, _videoCodec) < 0)
    {
        av_log(outputContext->streams[0]->codec, AV_LOG_ERROR, "%s","Error while opening codec for context.\n");
        exit(-1);
    }

    // Set time base, dimensions, etc.
    outputContext->streams[0]->codec->time_base = _videoCodecContext->time_base;
    outputContext->streams[0]->codec->width = _videoCodecContext->width;
    outputContext->streams[0]->codec->height = _videoCodecContext->height;
    // Some container formats want stream headers to be global
    if(outputContext->oformat->flags & AVFMT_GLOBALHEADER)
    {
        outputContext->streams[0]->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    // Open output file
    if (url_fopen(&outputContext->pb, outputContext->filename, URL_WRONLY) < 0)
    {
        av_log(outputContext,
                AV_LOG_ERROR, "%s","Could not open '%s'\n", outputContext->filename);
        exit(-1);
    }

    // Write Header
    if (avformat_write_header(outputContext, NULL) < 0)
    {
        av_log(outputContext, AV_LOG_ERROR, "%s","Error while writing header.\n");
        exit(-1);
    }

    // Write each packet
    for (int i = 0; i < (int)_videoPackets.size(); i++)
    {
        if (av_write_frame(outputContext, _videoPackets[i]) < 0)
        {
            av_log(outputContext, AV_LOG_ERROR, "%s","Error while writing frame.\n");
            exit(-1);
        }
    }

    // Write trailer
    if (av_write_trailer(outputContext) < 0)
    {
        av_log(outputContext, AV_LOG_ERROR, "%s","Error while writing trailer.\n");
        exit(-1);
    }

    // Close stuff
    avcodec_close(outputContext->streams[0]->codec);
    url_fclose(outputContext->pb);
    avformat_free_context(outputContext);
}


As said, it always crashes while trying to write the packages.
Maybe I should also point out that the reading and writing of the audio frames is done in more or less identical functions, and works very well.

I have no clue... please help! :(
Reply | Threaded
Open this post in threaded view
|

Re: av_write_frame crashes without error message

rogerdpack2
> But when trying to write the frames I got into a new video file with the
> same format - but this time, using video frames only, no audio -
> av_write_frame just crashes without giving any clue why.

does running it in gdb reveal a backtrace?
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: av_write_frame crashes without error message

Drabner
Sorry, I should probably have pointed out that I'm running on Windows 7
using VS2010.
I'm not even sure if GDB would run on this setup, andf of course, I have
never worked with that ;)

2011/9/26 Roger Pack <[hidden email]>

> > But when trying to write the frames I got into a new video file with the
> > same format - but this time, using video frames only, no audio -
> > av_write_frame just crashes without giving any clue why.
>
> does running it in gdb reveal a backtrace?
> _______________________________________________
> ffmpeg-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>



--
Jan Drabner ׀ TD Programming Engine & Animation



 zoobe message entertainment gmbh

kurfürstendamm 226 l 10719 berlin



  email: [hidden email] <[hidden email]> l  mob: 0172 7017640



  geschäftsführer: lenard f. krawinkel

 tel: +49 30. 288 838 88 l site: *www.zoobe.com* <http://www.zoobe.com/> ׀
email: *[hidden email]* <[hidden email]>



  amtsgericht charlottenburg, berlin ׀ hrb-nr. 11 42 27 9b
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user