📄 main.c
字号:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#ifdef _WIN32
#include <io.h>
#endif
#include "lame.h"
#ifdef __riscos__
//#include "asmstuff.h"
#endif
/************************************************************************
*
* main
*
* PURPOSE: MPEG-1,2 Layer III encoder with GPSYCHO
* psychoacoustic model.
*
************************************************************************/
int main(int argc, char **argv)
{
char mp3buffer[LAME_MAXMP3BUFFER];
short int Buffer[2][1152];
int iread,imp3;
lame_global_flags gf;
FILE *outf;
#ifdef __riscos__
int i;
#endif
lame_init(&gf); /* initialize libmp3lame */
if(argc==1) lame_usage(&gf,argv[0]); /* no command-line args, print usage, exit */
/* parse the command line arguments, setting various flags in the
* struct 'gf'. If you want to parse your own arguments,
* or call libmp3lame from a program which uses a GUI to set arguments,
* skip this call and set the values of interest in the gf struct.
* (see lame.h for documentation about these parameters)
*/
//lame_parse_args(&gf,argc, argv);
//设置编码器格式信息
gf.allow_diff_short = 0;
gf.ATHonly = 0;
gf.noATH=0;
gf.bWriteVbrTag = 1;
gf.cwlimit = 0;
gf.disable_reservoir = 0;
gf.experimentalX = 0;
gf.experimentalY = 0;
gf.experimentalZ = 0;
gf.frameNum = 0;
gf.quality = 2;
gf.input_format = sf_wave;
gf.filter_type = 0;
gf.lowpassfreq = 0;
gf.highpassfreq = 0;
gf.lowpasswidth = -1;
gf.highpasswidth = -1;
gf.lowpass1 = 0;
gf.lowpass2 = 0;
gf.highpass1 = 0;
gf.highpass2 = 0;
gf.lowpass_band = 32;
gf.highpass_band = -1;
gf.no_short_blocks = 0;
gf.resample_ratio = 1;
gf.padding_type = 2;
gf.padding = 0;
gf.swapbytes = 0;
gf.silent = 0;
gf.totalframes = 0;
gf.VBR = 0;
gf.VBR_q = 4;
gf.VBR_min_bitrate_kbps = 0;
gf.VBR_max_bitrate_kbps = 0;
gf.VBR_min_bitrate = 1;
gf.VBR_max_bitrate = 13;
//修改
gf.version = 1; /* =1 Default: MPEG-1 */
gf.in_samplerate = 44100;
gf.out_samplerate = 44100;
gf.num_channels = 2;
gf.mode = 1;
gf.mode_fixed = 0;
gf.force_ms = 0;
gf.brate = 0;
gf.copyright = 0;
gf.original = 1;
gf.extension = 0;
gf.error_protection = 0;
gf.emphasis = 0;
gf.num_samples = 0xFFFFFFFF;
gf.inPath = NULL;
gf.outPath = NULL;
// id3tag.used = 0;
//以标准的mp3音频质量格式初始化
gf.VBR_min_bitrate_kbps = 32; //最小位率
gf.VBR_max_bitrate_kbps = 256; //最大位率
gf.brate = 256; //正常位率
gf.lowpassfreq = -1;
gf.highpassfreq = -1;
gf.VBR_q = 0;
gf.VBR_min_bitrate_kbps = 112;
gf.VBR_max_bitrate_kbps = 320;
gf.mode = 0;
gf.mode_fixed = 1;
gf.quality = 2; //质量等级
gf.inPath="C:\\test\\test.wav";
gf.outPath="C:\\test\\test.wav.mp3";
if (!gf.gtkflag) {
/* open the MP3 output file */
if (!strcmp(gf.outPath, "-")) {
#ifdef __EMX__
_fsetmode(stdout,"b");
#elif (defined __BORLANDC__)
setmode(_fileno(stdout), O_BINARY);
#elif (defined __CYGWIN__)
setmode(fileno(stdout), _O_BINARY);
#elif (defined _WIN32)
_setmode(_fileno(stdout), _O_BINARY);
#endif
outf = stdout;
} else {
if ((outf = fopen(gf.outPath, "wb")) == NULL) {
fprintf(stderr,"Could not create \"%s\".\n", gf.outPath);
exit(1);
}
}
#ifdef __riscos__
/* Assign correct file type */
for (i = 0; gf.outPath[i]; i++)
if (gf.outPath[i] == '.') gf.outPath[i] = '/';
SetFiletype(gf.outPath, 0x1ad);
#endif
}
/* open the wav/aiff/raw pcm or mp3 input file. This call will
* open the file with name gf.inFile, try to parse the headers and
* set gf.samplerate, gf.num_channels, gf.num_samples.
* if you want to do your own file input, skip this call and set
* these values yourself.
*/
lame_init_infile(&gf);
/* Now that all the options are set, lame needs to analyze them and
* set some more options
*/
lame_init_params(&gf);
// lame_print_config(&gf); /* print usefull information about options being used */
#ifdef HAVEGTK
if (gf.gtkflag) gtk_init (&argc, &argv);
if (gf.gtkflag) gtkcontrol(&gf);
else
#endif
{
/* encode until we hit eof */
do {
/* read in 'iread' samples */
iread=lame_readframe(&gf,Buffer);
/* encode */
imp3=lame_encode_buffer(&gf,Buffer[0],Buffer[1],iread,
mp3buffer,(int)sizeof(mp3buffer));
/* was our output buffer big enough? */
if (imp3==-1) {
fprintf(stderr,"mp3 buffer is not big enough... \n");
exit(1);
}
if (fwrite(mp3buffer,1,imp3,outf) != imp3) {
fprintf(stderr,"Error writing mp3 output");
exit(1);
}
} while (iread);
}
//imp3=lame_encode_finish(&gf,mp3buffer,(int)sizeof(mp3buffer)); /* may return one more mp3 frame */
//fwrite(mp3buffer,1,imp3,outf);
fclose(outf);
//lame_close_infile(&gf); /* close the input file */
//lame_mp3_tags(&gf); /* add id3 or VBR tags to mp3 file */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -