2,542
社区成员
发帖
与我相关
我的任务
分享
#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();
}