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,不会混用这两个.字符串可以编码成字节包,而 ...
随机推荐
- 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card
水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【点分治】bzoj1316 树上的询问
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #defin ...
- 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠
<法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...
- 1.2(Mybatis学习笔记)Mybatis核心配置
一.Mybatis核心对象 1.1SqlSeesionFactory SqlSessionFactory主要作用是创建时SqlSession. SqlSessionFactory可通过SqlSessi ...
- 7.1(java学习笔记)InetAddress&InetScoketAddress
一.InetAddress 这个类主要表示IP地址.InetAddress封装了IP地址和域名.域名可以看做IP地址的一个别称,起这个别称的目的是为了便于记忆. 例如www.baidu.com 就是1 ...
- VS2017安装错误:工作负荷不完整,未能安装包“sqlcmdlnutils,version=15.1.61703.130,chip=x64,language=zh-CN”。
场景:已安装的VS2017维护安装MVC4时出现如下错误: 看问题描述是由于sqlcmdlnutils安装失败影响到其它组件的安装,于是单独下载此安装包进行安装,发现安装一切正常,继续维护VS2017 ...
- 【转】谈基于SOA的应用系统设计和开发
注:在网上看到这篇文档,觉得写得很好,清晰实用.该博客其它文章也写得不错 地址:http://blog.sina.com.cn/s/blog_493a84550101gswn.html 现在对 ...
- 获取配置文件中的key的value
获取App.config文件的key的value System.Configuration.ConfigurationManager.AppSettings["keyName"] ...
- Automatic Diagnostic Repository
转载自 http://www.eygle.com/archives/2007/08/11g_auto_diag_repository.html#comments Oracle Database 11g ...
- NFSv4 mount incorrectly shows all files with ownership as nobody:nobody
NFSv4 mount incorrectly shows all files with ownership as nobody:nobody https://access.redhat.com/ ...