我刚刚下载了FFMPEG,现在我试图在Qt中使用MSVC2013编译器。
为了理解它是如何工作的,我开始阅读文档和API。 根据这个figure,我试图用libavformat做一点测试。
我做了他们在demuxing module中说的所有内容,然后是复用模块。但是,当我调用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;
}
程序显示消息尚未崩溃。但不是 OOps你不能看到我(John Cena)
没有错误。我使用MP3文件作为输入,我想在WMA中输出它。