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

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


冉冉说
浏览 941回答 1
1回答

精慕HU

void CustomIoContext(int argc, char **argv){av_register_all();#if 1const char *srcfile = argv[1], *dstfile = argv[2];const int iBufSize = BUF_SIZE;unsigned char*&nbsp;pBuffer = new unsigned char[iBufSize];AVInputFormat *piFmt =&nbsp;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, "",&nbsp;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&nbsp;= pIOCtx;pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;//step4:打开流if (avformat_open_input(&pFormatCtx, "", piFmt,&nbsp;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.&nbsp;Play&nbsp;the output file with the command:\n""ffplay&nbsp;-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}
打开App,查看更多内容
随时随地看视频慕课网APP