怎么样用ffmpeg打开buffer-CSDN论坛
2个回答
展开全部
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
}
我写的这段代码是可以的了, 贴出来给你抄袭一下吧。 这仅仅是测试代码,所以你还得优化,得做修改,
{
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
}
我写的这段代码是可以的了, 贴出来给你抄袭一下吧。 这仅仅是测试代码,所以你还得优化,得做修改,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询