#include <stdio.h>#include<stdlib.h>#include<lame.h>#defineInbufsize 4096#defineMp3bufsize (int) (1.25 * inbufsize) + 7200intEncodeChar*inpath,Char*Outpath) { intStatus =0; Lame_global_flags*GFP; intRet_code; FILE*INTFP; FILE*OUTFP; Short*Input_buffer; intInput_samples; Char*Mp3_buffer; intmp3_bytes; /*Initialize the library.*/GFP=Lame_init (); if(GFP = =NULL) {printf ("Lame_init returned null\n"); Status= -1; Gotoexit; } /*Set the encoding parameters.*/Ret_code=Lame_init_params (GFP); if(Ret_code <0) {printf ("Lame_init_params returned%d\n", Ret_code); Status= -1; GotoClose_lame; } /*Open Our input and output files.*/INTFP= fopen (Inpath,"RB"); OUTFP= fopen (Outpath,"WB"); /*Allocate some buffers.*/Input_buffer= ( Short*)malloc(Inbufsize *2); Mp3_buffer= (Char*)malloc(mp3bufsize); /*Read from the input file, encode, and write to be output file.*/ Do{input_samples= Fread (Input_buffer,2, Inbufsize, INTFP); if(Input_samples >0) {mp3_bytes=lame_encode_buffer_interleaved (GFP, Input_buffer, Input_samples/2, Mp3_buffer, mp3bufsize); if(Mp3_bytes <0) {printf ("Lame_encode_buffer_interleaved returned%d\n", mp3_bytes); Status= -1; Gotofree_buffers; }Else if(Mp3_bytes >0) {fwrite (Mp3_buffer,1, Mp3_bytes, OUTFP); } } } while(Input_samples = =inbufsize); /*Flush The encoder of any remaining bytes.*/mp3_bytes= Lame_encode_flush (GFP, Mp3_buffer,sizeof(Mp3_buffer)); if(Mp3_bytes >0) {printf ("writing%d mp3 bytes\n", mp3_bytes); Fwrite (Mp3_buffer,1, Mp3_bytes, OUTFP); } /*Clean up .*/free_buffers: Free(Mp3_buffer); Free(Input_buffer); Fclose (OUTFP); Fclose (INTFP); Close_lame:lame_close (GFP); Exit:returnstatus;}intMainintargcChar*argv[]) { if(ARGC <3) {printf ("usage:clame rewinfile mp3outfile\n"); Exit (1); } Encode (argv[1], argv[2]); return 0;}/*//Unix ro linux:gcc-i/usr/include/lame clame.c-lmp3lame-o clame//windowscl/ic:\lame-3.98.2\include clame.c c \ Lame-3.98.2\libmp3lame\release\libmp3lame.lib C:\lame-3.98.2\mpglib\Release\mpglib.lib*/
#include <Python.h>#include<lame.h>/*defined in clame.c*/intEncodeChar*,Char*);StaticPyobject *pylame1_encode (Pyobject *self, Pyobject *args) { intstatus; Char*Inpath; Char*Outpath; if(! Pyarg_parsetuple (args,"SS", &inpath, &Outpath)) { returnNULL; } Status=encode (Inpath, outpath); returnPy_buildvalue ("I", status); //Py_return_none;}StaticPymethoddef pylame1_methods[] = { {"encode", (pycfunction) Pylame1_encode, Meth_varargs, null}, {null, NULL,0, NULL}}; Pymodinit_func initpylame1 () {Py_initmodule3 ("pylame1", Pylame1_methods,"My First LAME module.");}/*//Unix ro linux:gcc-shared-i/usr/include/pyton3.1-i/usr/include/lame pylame1.c clame.c-lmp3lame-o pylame1.so// Windowscl/ld/ic:\pyton31\include/ic:\lame-3.98.2\include pylame1.c clame.c C:\Python31\libs\python31.lib C:\lame- 3.98.2\libmp3lame\release\libmp3lame.lib C:\lame-3.98.2\mpglib\Release\mpglib.lib*/
Python C example:encode mp3 code