专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 多媒体/流媒体开发

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

发布时间:2011-06-27 19:29:19 文章来源:www.iduyao.cn 采编人员:星星草
linux下ffmpeg实现wav转mp3,原来的9秒声音变成了一秒
#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);
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: