ffmpeg的mp3音频编码器的使用方式问题

equalman318534 2012-08-16 02:06:46
现使用ffmpeg的MP3编码器卡壳,网上下载的代码可以正确将一个pcm文件转为mp3格式的,而我要实现从采集卡试试采集每次压缩16k的数据,我设计了一个函数A,这样就涉及重复调用A的问题,函数A内也有完整的初始化代码,可正确运行一段时间后就出错了,因windows下无法编译ffmpeg代码,更不用说调试,头大,也尝试过用lame编码,用lame.exe压出来的mp3走调
...全文
1144 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackfrank078 2014-03-25
  • 打赏
  • 举报
回复
我想问一个关于ffmepg编码的问题,ffmpeg将pcm压缩成mp3,有那些参数需要手动初始化的 我用的ffmpeg是0.11.5版本的,
神-气 2012-11-25
  • 打赏
  • 举报
回复
我不明白你为什么要每次都要初始化,windows下可以编译ffmpeg啊,用mingw。
LOVE小丹 2012-11-20
  • 打赏
  • 举报
回复
哥们你的demo能给我一分儿么?或者你给说个参考的地址,我现在连怎么编码为MP3都成问题了... jay8499198@126.com
equalman318534 2012-08-16
  • 打赏
  • 举报
回复
我试过只初始化一次编码库,可在调用压缩函数一次还行,第二次就出问题了,所以压缩函数每次都初始化了
equalman318534 2012-08-16
  • 打赏
  • 举报
回复
以上是我对我应用中调用方式的模拟,大家帮看啊,分不够就再加,吱一声
equalman318534 2012-08-16
  • 打赏
  • 举报
回复

#include "windows.h"

#include "libavcodec\\avcodec.h"

#pragma comment (lib,"..\\FFMPEG_lib\\avformat.lib")
#pragma comment (lib,"..\\FFMPEG_lib\\avutil.lib")
#pragma comment (lib,"..\\FFMPEG_lib\\swscale.lib")
#pragma comment (lib,"..\\FFMPEG_lib\\avcodec.lib")
#pragma comment (lib,"..\\FFMPEG_lib\\avdevice.lib")
#pragma comment (lib,"..\\FFMPEG_lib\\avfilter.lib")

#ifndef M_PI
#define M_PI 3.14159265358979323846
#define JVS_AUDIO_BUF_SIZE 16*1024

int numberframe=0;
int totalSize=0;
FILE * fin,*fout;

BOOL EncodeAudioData(char *pin,int len,unsigned char* pout,int lenOut)
{
AVCodec *codec;
AVCodecContext *codecContext= NULL;
short *samples=(short *)pin;
DWORD inBufferLen=(DWORD)len;
DWORD dwSampleLenInShort=0;
int inBufferIndex=0;
char msg[50]={0};
int tempSize=0;

int fileLength=0;
char * fileContent=NULL;
DWORD dwAudioEncodedLen=0;

/* 寻找编码器 */
codec = avcodec_find_encoder(CODEC_ID_MP3);
if (!codec)
{
return FALSE;
}
codecContext= avcodec_alloc_context();
/* 设置波特率及采样帧率 通道数*/
codecContext->bit_rate = 64000;
codecContext->sample_rate = 48000;
codecContext->channels = 1;

/* 每次编码都要打开一次 */
if (avcodec_open(codecContext, codec) < 0)
{
return FALSE;
}

dwSampleLenInShort=codecContext->frame_size*codecContext->channels;
dwAudioEncodedLen=0;
for(;;)
{
if (inBufferIndex>inBufferLen)
{
break;
}
/* 编码一帧 */
dwAudioEncodedLen += tempSize= avcodec_encode_audio(codecContext, pout+dwAudioEncodedLen, lenOut-dwAudioEncodedLen, samples);
//每次初始化的前两帧压缩都是0 所以要判断一下返回值
if (tempSize>0)
{
samples+=dwSampleLenInShort;
inBufferIndex+=(dwSampleLenInShort*2);

numberframe++;
totalSize+=tempSize;
sprintf(msg,"save frame Id %d,now tempSize=%d,totalSize=%d\n",numberframe,tempSize,totalSize);
OutputDebugString(msg);
}
}
fwrite(pout, dwAudioEncodedLen,1,fout);
avcodec_close(codecContext);
av_free(codecContext);
}

/*
* Audio encoding example
*/
void audio_encode_example(const char * inputfilename ,const char *outputfilename)
{
int size = 0;
int fileLength=0;
char * fileContent=NULL,*pFileContent=NULL;
short *samples=NULL;
UCHAR pAudioEncodedData[JVS_AUDIO_BUF_SIZE];
printf("Audio encoding\n");

fin = fopen(inputfilename, "rb+");
if (!fin)
{
fprintf(stderr, "could not open %s\n", inputfilename);
exit(1);
}

fout = fopen(outputfilename, "wb");
if (!fout)
{
fprintf(stderr, "could not open %s\n", outputfilename);
exit(1);
}

fseek(fin,0L,SEEK_END);
fileLength=ftell(fin);
fileContent=malloc(fileLength);
fseek(fin,0L,SEEK_SET);
size = fread(fileContent, fileLength,1, fin);
if (fileLength == 0)
{
return;
}

pFileContent=fileContent;
for(;;)
{
if (size>fileLength)
{
break;
}

EncodeAudioData(pFileContent,JVS_AUDIO_BUF_SIZE,pAudioEncodedData,JVS_AUDIO_BUF_SIZE);
pFileContent+=JVS_AUDIO_BUF_SIZE;
size+=JVS_AUDIO_BUF_SIZE;
}

fclose(fin);
fclose(fout);
free(fileContent);
}

int main()
{
const char *EncodeOutputFilename_Audio;
const char *EncodeInputFilename_Audio;

EncodeInputFilename_Audio = "c:\\test.asf";
EncodeOutputFilename_Audio = "c:\\analyse.mp3";

avcodec_init();
avcodec_register_all();

audio_encode_example(EncodeInputFilename_Audio,EncodeOutputFilename_Audio); //编码
return getchar();
}

2,542

社区成员

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

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