为了账号安全,请及时绑定邮箱和手机立即绑定

怎么样用ffmpeg打开buffer-CSDN论坛

怎么样用ffmpeg打开buffer-CSDN论坛

冉冉说 2019-02-12 12:21:50
怎么样用ffmpeg打开buffer-CSDN论坛
查看完整描述

1 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

void CustomIoContext(int argc, char **argv)
{
av_register_all();

#if 1
const char *srcfile = argv[1], *dstfile = argv[2];

const int iBufSize = BUF_SIZE;
unsigned char* pBuffer = new unsigned char[iBufSize];
AVInputFormat *piFmt = NULL;

FILE* fp1= fopen(argv[1],"rb");

// Allocate the AVIOContext:
AVIOContext* pIOCtx = avio_alloc_context(pBuffer, iBufSize,
0,
fp1,
ReadFunc,
0,
0);

//step2:探测流格式
if (av_probe_input_buffer(pIOCtx, &piFmt, "", NULL, 0, 0) < 0) {
fprintf(stderr, "probe failed!\n");
return ;
} else {
fprintf(stdout, "probe success!\n");
fprintf(stdout, "format: %s[%s]\n", piFmt->name, piFmt->long_name);
}
// Allocate the AVFormatContext:
AVFormatContext* pFormatCtx = avformat_alloc_context();
// Set the IOContext:
pFormatCtx->pb = pIOCtx;
pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;

//step4:打开流
if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0) {
fprintf(stderr, "avformat open failed.\n");
return ;
} else {
fprintf(stdout, "open stream success!\n");
}

if(av_find_stream_info(pFormatCtx)<0)
HandleError("av_find_stream_info",__FUNCTION__, __LINE__);
av_dump_format(pFormatCtx, 0, argv[1], false);

int i = -1;
int index = -1;
for(i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
index = i;
break;
}
}
if (index == -1)
{
printf("there is not any audio stream in [%s]",srcfile);
return;
}

AVCodecContext *codeContext = pFormatCtx->streams[i]->codec;
AVCodec *codec = avcodec_find_decoder(codeContext->codec_id);
if (!codec)
{
printf("find codec for codec_id[%d] fail, file[%s]",pFormatCtx->audio_codec_id, srcfile);
return;
}

int ret = avcodec_open2(codeContext, codec, NULL);
if (ret >= 0)
{
printf("open codec[name:%s] for stream [id:%d] of file [%s]\n",codec->name,index,srcfile);
}

FILE *fp = fopen(argv[2],"wb");
AVPacket packet;
av_init_packet(&packet);
AVFrame *frame;

while(av_read_frame(pFormatCtx,&packet) >= 0)
{
int got_frame = 0;
frame = avcodec_alloc_frame();
ret = avcodec_decode_audio4(codeContext,frame, &got_frame, &packet);
if (ret < 0 )
{
printf("decode error \n");
exit(0);
}

if (got_frame)
{
int data_size = av_samples_get_buffer_size(NULL, codeContext->channels, frame->nb_samples, codeContext->sample_fmt, 1);
fwrite(frame->extended_data[0],1,frame->linesize[0],fp);
}
avcodec_free_frame(&frame);
av_free_packet(&packet);
}

const char *fmt;
get_format_from_sample_fmt(&fmt,codeContext->sample_fmt);
fprintf(stderr, "convert succeeded. Play the output file with the command:\n"
"ffplay -f %s -channel_layout %lld -channels %d -ar %d %s\n",
fmt, codeContext->channel_layout , codeContext->channels, codeContext->sample_rate, argv[2]);
avcodec_close(codeContext);
fclose(fp1);
fclose(fp);
delete []pBuffer;
avformat_free_context(pFormatCtx);
av_free(pIOCtx);
#endif
}




查看完整回答
反对 回复 2019-02-16
  • 1 回答
  • 0 关注
  • 912 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信