gpt4 book ai didi

c++ - (FFMPEG) avformat_write_header 崩溃 (MSVC2013) (C++) (Qt)

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:55 66 4
gpt4 key购买 nike

我刚刚下载了 FFMPEG,现在我正尝试在带有 MSVC2013 编译器的 Qt 中使用它。

为了了解它的工作原理,我开始阅读文档和 API。根据这个figure , 我试图用 libavformat 做一个小测试。

我做了他们在 demuxing module 中所说的一切,然后是 muxing 模块。但是,当我调用 avformat_write_header() 函数时,我的程序崩溃了。

我想知道我做错了什么,如果你能帮助我理解这一点。

主要是:

av_register_all();

if(!decode())
return;

decode() 方法:

bool MainWindow::decode()
{
AVFormatContext *formatContext = NULL;
AVPacket packet;

/**************** muxing varaiables ******************/

AVFormatContext *muxingContext = avformat_alloc_context();
AVOutputFormat *outputFormat = NULL;
AVIOContext *contextIO = NULL;
AVCodec *codecEncode = avcodec_find_encoder(AV_CODEC_ID_WMAV2);
AVStream *avStream = NULL;
AVCodecContext *codecContext = NULL;


/******************* demuxing **************************/

//open a media file
if(avformat_open_input(&formatContext,"h.mp3",NULL,NULL)!=0)
{
qDebug() << "paka ouve fichier";
return false;
}

//function which tries to read and decode a few frames to find missing
information.
if(avformat_find_stream_info(formatContext,NULL)<0)
{
qDebug()<<"paka find stream";
return false;
}


/**************** muxing *************************/

//The oformat field must be set to select the muxer that will be used.
muxingContext->oformat = outputFormat;

//Unless the format is of the AVFMT_NOFILE type, the pb field must be set to
//an opened IO context, either returned from avio_open2() or a custom one.
if(avio_open2(&contextIO,"out.wma",AVIO_FLAG_WRITE,NULL,NULL)<0)
{
qDebug() <<"paka kreye fichier soti";
return false;
}
muxingContext->pb = contextIO;

//Unless the format is of the AVFMT_NOSTREAMS type, at least
//one stream must be created with the avformat_new_stream() function.
avStream = avformat_new_stream(muxingContext,codecEncode);

//The caller should fill the stream codec context information,
//such as the codec type, id and other parameters
//(e.g. width / height, the pixel or sample format, etc.) as known

codecContext = avStream->codec;
codecContext->codec_type = AVMEDIA_TYPE_AUDIO;
codecContext->codec_id = AV_CODEC_ID_WMAV2;
codecContext->sample_fmt = codecEncode->sample_fmts[0];
codecContext->bit_rate = 128000;
codecContext->sample_rate = 44000;
codecContext->channels = 2;

//The stream timebase should be set to the timebase that the caller desires
//to use for this stream (note that the timebase actually used by the muxer
//can be different, as will be described later).

avStream->time_base = formatContext->streams[0]->time_base;
qDebug()<<formatContext->streams[0]->time_base.num <<"/"
<<formatContext- >streams[0]->time_base.den;


//When the muxing context is fully set up, the caller must call
//avformat_write_header()
//to initialize the muxer internals and write the file header

qDebug() << "does not crash yet";
if(avformat_write_header(muxingContext,NULL) <0)
{
qDebug()<<"cannot write header";
return false;
}
qDebug() << "OOps you can't see me (John Cena)";

///////////////////// Reading from an opened file //////////////////////////
while(av_read_frame(formatContext,&packet)==0)
{
//The data is then sent to the muxer by repeatedly calling
//av_write_frame() or av_interleaved_write_frame()
if(av_write_frame(muxingContext,&packet)<0)
qDebug()<<"paka write frame";
else
qDebug()<<"writing";
}

//Once all the data has been written, the caller must call
//av_write_trailer() to flush any buffered packets and finalize
//the output file, then close the IO context (if any) and finally
//free the muxing context with avformat_free_context().

if(av_write_trailer(muxingContext)!=0)
{
qDebug()<<"paka ekri trailer";
return false;
}


return true;
}

程序显示消息还没有崩溃。但不是糟糕,你看不到我(John Cena)

而且没有错误。我使用 MP3 文件作为输入,我想将其输出为 WMA。

最佳答案

使用 avformat_alloc_output_context2() 而不是 avformat_alloc_context()。这将设置 muxingContext->oformat。

关于c++ - (FFMPEG) avformat_write_header 崩溃 (MSVC2013) (C++) (Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908636/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com