#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的更多相关文章

  1. Python字符串的encode与decode研究心得——解决乱码问题

    转~Python字符串的encode与decode研究心得——解决乱码问题 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x8 ...

  2. Python编码介绍——encode和decode

    在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,所以 ...

  3. 【转 记录】python中的encode以及decode

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  4. Python字符串的encode与decode研究心得乱码问题解决方法

    为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...

  5. Python字符串的encode与decode

    首先要搞清楚,字符串在Python内部的表示是unicode编码. 因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unic ...

  6. Python decode与encode

      字符串在Python内部的表示是unicode编码(8-bit string),因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicod ...

  7. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  8. Python字符串的encode与decode研究心得 乱码问题解决方法

    以下摘自:http://www.jb51.net/article/17560.htm 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x ...

  9. 关于python decode()和 encode()

    1.先收集一下这几天看到的关于decode()解码和encode()编码的用法 bytes和str是字节包和字符串,python3中会区分bytes和str,不会混用这两个.字符串可以编码成字节包,而 ...

随机推荐

  1. 【计算几何】【极角序】【前缀和】bzoj1132 [POI2008]Tro

    把点按纵坐标排序,依次枚举,把它作为原点,然后把之后的点极角排序,把叉积的公式稍微化简一下,处理个后缀和统计答案. #include<cstdio> #include<cmath&g ...

  2. 5.7(java学习笔记)Vector、Enumeration

    一.Vector Vector类实现一个可扩展的数组对象.与数组一样,它包含可以使用整数索引访问. 它的基本操作方法add(int index, E element),get(int index),i ...

  3. 解决Post提交乱码问题

    在web.xml里面配置 <filter> <filter-name>charac</filter-name> <filter-class>org.sp ...

  4. 使用urlretrieve下载图片

    示例代码: from urllib.request import urlretrieve from urllib.request import urlopen from bs4 import Beau ...

  5. Android开发工具

    Android开发工具: AndroidDevTools: 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. ...

  6. 国内流行的开源.net微信公众平台SDK对比分析

    一.引言 目前微信公众平台正如火如荼的进行中,微信虽然在海外市场不敌WhatsApp,但是已经俘获了国内绝大部分用户的心.作为国内最大的,超级"app",微信已算是成功问鼎了.公众 ...

  7. c# datatable.select() 支持group by

    不支持group by ,支持order by.如果要使用group by的话,可以使用linq,这是C#3.0的内容.给你个示例static void Main(string[] args){ Da ...

  8. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  9. RedisTemplate SerializationFailedException: Failed to deserialize payload 异常解决

    问题描述: 使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调 ...

  10. nyoj237 游戏高手的烦恼(最小点覆盖)

    题目237 题目信息 pid=237" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 ...