python C example:encode mp3 code
#include <stdio.h> #include <stdlib.h> #include <lame.h> #define INBUFSIZE 4096 #define MP3BUFSIZE (int)(1.25 * INBUFSIZE) + 7200 int encode(char *inpath, char *outpath) { int status = 0; lame_global_flags *gfp; int ret_code; FILE *intfp; FILE *outfp; short *input_buffer; int input_samples; char *mp3_buffer; int mp3_bytes; /* Initialize the library.*/ gfp = lame_init(); if(gfp == NULL) { printf("lame_init returned NULL\n"); status = -1; goto exit; } /*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; goto close_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; goto free_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: return status; } int main(int argc, char * 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 //windows cl /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*/ int encode(char*, char*); static PyObject *pylame1_encode(PyObject *self, PyObject *args) { int status; char *inpath; char *outpath; if(!PyArg_ParseTuple(args,"ss", &inpath, &outpath)) { return NULL; } status = encode(inpath, outpath); return Py_BuildValue("i", status); //Py_RETURN_NONE; } static PyMethodDef 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 //windows cl /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 */
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· 一个适用于 .NET 的开源整洁架构项目模板
· API 风格选对了,文档写好了,项目就成功了一半!
· 【开源】C#上位机必备高效数据转换助手
· .NET 9.0 使用 Vulkan API 编写跨平台图形应用
· MyBatis中的 10 个宝藏技巧!