linux下ffmpeg实现wav转mp3,原来的9秒声音变成了一秒

cxxy328 2011-10-19 02:27:14
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include <stdio.h>
#include "libavutil/avutil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(int argc,char **argv)
{
const char *input_file_name="test.wav";
av_register_all();
AVFormatContext *ic;
ic=avformat_alloc_context();
if(av_open_input_file(&ic,input_file_name,NULL,FFM_PACKET_SIZE,NULL)!=0)
{
printf("can't open the file %s\n",input_file_name);
exit(1);
}
if(av_find_stream_info(ic)<0)
{
printf("can't find suitable codec parameters\n");
exit(1);
}
dump_format(ic,0,input_file_name,0);
int i;
int audioindex=-1;
for(i=0;i<ic->nb_streams;i++)
{
if(ic->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
{
audioindex=i;
break;
}
}
if(audioindex==-1)
{
printf("can't find audio stream\n");
exit(1);
}
AVCodecContext *aCodecCtx;
aCodecCtx=ic->streams[audioindex]->codec;
AVCodec *aCodec;
aCodec=avcodec_find_decoder(aCodecCtx->codec_id);
if(aCodec==NULL)
{
printf("can't find suitable audio decoder\n");
exit(1);
}
if(avcodec_open(aCodecCtx,aCodec)<0)
{
printf("can't open the audio decoder\n");
exit(1);
}
const char *output_file_name="out.mp3";
AVOutputFormat *fmt;
AVFormatContext *oc;
AVCodecContext *oAcc;
AVCodec *oAc;
AVStream *audio_st;
fmt=guess_format(NULL,output_file_name,NULL);
if(!fmt)
{
printf("could not deduce output format from outfile extension\n");
exit(0);
}
oc=avformat_alloc_context();
if(!oc)
{
printf("Memory error\n");
exit(0);
}
oc->oformat=fmt;
strncpy(oc->filename,output_file_name,sizeof(oc->filename));
audio_st=av_new_stream(oc,oc->nb_streams);
if(!audio_st)
{
printf("could not alloc audio stream\n");
exit(0);
}
avcodec_get_context_defaults2(audio_st->codec,CODEC_TYPE_AUDIO);
oAcc=avcodec_alloc_context();
oAcc=audio_st->codec;
oAcc->codec_id=CODEC_ID_MP3;
oAcc->codec_type=CODEC_TYPE_AUDIO;
oAcc->bit_rate=aCodecCtx->bit_rate;
oAcc->sample_rate=aCodecCtx->sample_rate;
oAcc->channels=1;
oAcc->block_align=0;

if (av_set_parameters(oc, NULL) < 0)
{
printf( "Invalid output format parameters\n");
exit(0);
}
strcpy(oc->title,"music of c/c++");
strcpy(oc->author,"yzg");
strcpy(oc->copyright,"linux");
strcpy(oc->comment,"fedora");
strcpy(oc->album,"test");
oc->year=2011;
oc->track=1;
strcpy(oc->genre,"dota");
dump_format(oc,0,output_file_name,1);
oAc=avcodec_find_encoder(CODEC_ID_MP3);
if(!oAc)
{
printf("can't find suitable audio encoder\n");
exit(0);
}
if(avcodec_open(oAcc,oAc)<0)
{
printf("can't open the output audio codec");
exit(0);
}
if (!(oc->flags & AVFMT_NOFILE))
{
if(url_fopen(&oc->pb,output_file_name,URL_WRONLY)<0)
{
printf("can't open the output file %s\n",output_file_name);
exit(0);
}
}
if(!oc->nb_streams)
{
fprintf(stderr,"output file dose not contain any stream\n");
exit(0);
}
if(av_write_header(oc)<0)
{
fprintf(stderr, "Could not write header for output file\n");
exit(1);
}
#if 1
AVPacket packet;
uint8_t *ptr,*out_buf;
int out_size;
static short *samples=NULL;
static unsigned int samples_size=0;
uint8_t *audio_outbuf;
int audio_outbuf_size;
audio_outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
audio_outbuf = av_malloc(audio_outbuf_size);
samples_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
samples = av_malloc(samples_size);

int len = 0;
int ret = 0;
while(av_read_frame(ic,&packet)>=0)
{
if(packet.stream_index==audioindex)
{
len=packet.size;
ptr=packet.data;


while(len>0)
{
out_buf=NULL;
out_size=0;
if(&packet)
samples=av_fast_realloc(samples,&samples_size,FFMAX(packet.size*sizeof
(*samples),AVCODEC_MAX_AUDIO_FRAME_SIZE));
out_size=samples_size;
ret=avcodec_decode_audio2(aCodecCtx,samples,&out_size,ptr,len);//ÈôΪÒôƵ°ü£¬½âÂë¸ÃÒôƵ°ü
if(ret<0)
{
printf("while decode audio failure\n");
break;
}
fflush(stdout);
ptr+=ret;
len-=ret;
if(out_size<=0)
continue;
out_buf=(uint8_t *)samples;
AVPacket pkt;
av_init_packet(&pkt);
pkt.size= avcodec_encode_audio(oAcc, audio_outbuf, audio_outbuf_size, samples);
pkt.pts= av_rescale_q(oAcc->coded_frame->pts, oAcc->time_base, audio_st->time_base);
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= audioindex;
pkt.data= audio_outbuf;
if (av_write_frame(oc, &pkt) != 0)
{
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
}
av_free_packet(&packet);
}
av_write_trailer(oc);
...全文
323 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Love_云 2011-10-19
  • 打赏
  • 举报
回复
bug

哥来给你推荐两位高手:
百度、google
Love_云 2011-10-19
  • 打赏
  • 举报
回复
靠,这还不简单
问我啊
cxxy328 2011-10-19
  • 打赏
  • 举报
回复
求有经验的大大们指点下
cxxy328 2011-10-19
  • 打赏
  • 举报
回复
续:
for(i = 0; i < oc->nb_streams; i++)
{
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
url_fclose(oc->pb);
av_free(oc);
av_free(out_buf);
avcodec_close(aCodecCtx);
av_close_input_file(ic);
}
#endif

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧