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 = ;
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 = -;
goto exit;
} /*Set the encoding parameters.*/
ret_code = lame_init_params(gfp);
if(ret_code < )
{
printf("lame_init_params returned %d\n", ret_code);
status = -;
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 *);
mp3_buffer = (char*)malloc(MP3BUFSIZE); /*Read from the input file, encode, and write to be output file.*/
do{
input_samples = fread(input_buffer, , INBUFSIZE, intfp);
if(input_samples > )
{
mp3_bytes = lame_encode_buffer_interleaved(
gfp,
input_buffer,
input_samples / ,
mp3_buffer,
MP3BUFSIZE
);
if(mp3_bytes < )
{
printf("lame_encode_buffer_interleaved returned %d\n", mp3_bytes);
status = -;
goto free_buffers;
}else if(mp3_bytes > )
{
fwrite(mp3_buffer, , 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 > )
{
printf("writing %d mp3 bytes\n", mp3_bytes);
fwrite(mp3_buffer, , 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 < )
{
printf("usage: clame rewinfile mp3outfile\n");
exit();
}
encode(argv[], argv[]);
return ;
}
/*
// 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, , 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
*/
python C example:encode mp3 code的更多相关文章
- Python字符串的encode与decode研究心得——解决乱码问题
转~Python字符串的encode与decode研究心得——解决乱码问题 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x8 ...
- Python编码介绍——encode和decode
在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,所以 ...
- 【转 记录】python中的encode以及decode
字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...
- Python字符串的encode与decode研究心得乱码问题解决方法
为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...
- Python字符串的encode与decode
首先要搞清楚,字符串在Python内部的表示是unicode编码. 因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unic ...
- Python decode与encode
字符串在Python内部的表示是unicode编码(8-bit string),因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicod ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- Python字符串的encode与decode研究心得 乱码问题解决方法
以下摘自:http://www.jb51.net/article/17560.htm 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x ...
- 关于python decode()和 encode()
1.先收集一下这几天看到的关于decode()解码和encode()编码的用法 bytes和str是字节包和字符串,python3中会区分bytes和str,不会混用这两个.字符串可以编码成字节包,而 ...
随机推荐
- (转)关于Unity3D的编辑器崩溃时的线索定位
今天在Unity3D编辑器中进行功能测试的时候,编辑器突然崩溃了(就是整个窗口突然消失,进程直接结束)之后也没有任何错误报告信息提示.好吧,应该是偶现问题,我侥幸地想,我用的好歹也是正版啊,不应该总出 ...
- HashMap源码-使用说明部分
/* * Implementation notes. * 使用说明 * * This map usually acts as a binned (bucketed) hash table, but * ...
- REST和SOAP区别
转载于: http://blog.csdn.net/idafish/article/details/6308916 REST似乎在一夜间兴起了,这可能引起一些争议,反对者可以说REST是WEB诞生之 ...
- 【Git】GitHub for Windows使用(2) 分支
目录 1.回看客户端相关功能 2.新建一个分支 3.在新分支上修改文件 4.上传新建分支上的修改,并合并分支 5.删除分支 1.回看客户端相关功能 看看设置中的以下内容 2.新建一个分支 3.在新分支 ...
- ios(iphone/ipad)一个简单的用代码判断当前设备的方法
直接NSLog(@"current_device:%@",[UIDevice currentDevice].model); 即可看出它输出的是当前设备,所以根据这个字符串可简单的判 ...
- Jigsaw 项目:Java 模块系统新手引导
前言 随着 2017 年 10 月 Java 9 的发布,Java 能够使用模块系统了,但是中文互联网上的资料太少,许多关于 Java 模块系统的文章都只是介绍了模块系统的好处,或者给了一些毫无组织的 ...
- DEDECMS之0day入侵总结
1.查看dedecms最后升级版本:http://xxx.com/data/admin/ver.txt 2.利用网上公开之0day进行对应版本之入侵 ps:dedecms默认CMS后台:http:// ...
- MariaDB数据库管理系统
MYSQL数据库管理系统被Oracle公司收购后从开源换向到了封闭,导致许多Linux发行版选择了MariaDB. MYSQL是一款大家都非常熟知的数据库管理系统,技术成熟.配置简单.开源免费并且 ...
- cinemachine unity
scrips extending timeline Extending Timeline with your own playables Data Audio ----playable beh ...
- http://www.oschina.net/question/1019034_153316
http://www.oschina.net/question/1019034_153316 http://www.oschina.net/question/97503_212116?sort=tim ...