#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. python3开发进阶-Django框架中的ORM的常用(增,删,改,查)操作

    阅读目录 如何在Django终端打印SQL语句 如何在Python脚本中调用Django环境 操作方法 单表查询之神奇的下划线 ForeignKey操作 ManyToManyField 聚合查询和分组 ...

  2. Mysql-库的基本操作

    一 .系统数据库 二 .创建数据库 三 .数据库相关操作 一. 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信 ...

  3. [CF160D]Edges in MST

    [CF160D]Edges in MST 题目大意: 一个\(n(n\le10^5)\)个点,\(m(m\le10^5)\)条边的连通图.对于图中的每条边,判断它与该图最小生成树的关系: 在该图所有的 ...

  4. [CodeChef-LVGFT]Lovers Gift

    题目大意: 给定一个$n(n\le10^5)$个结点的树,初始全为白点.$m(m\le10^5)$次操作,每次将点$x$染成黑色或询问从$x$出发至少经过一个黑点能到达的点中,编号次大的点. 思路: ...

  5. 6.4(java学习笔记)转换流

    一.乱码问题 我们来看下列例子: public class ConStream { //当前平台默认采用GBK public static void main(String[] args){ Stri ...

  6. tomcat访问(access)日志配置、记录Post请求参数(转)

    一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve className="org.apache.catalina.va ...

  7. asp.net mvc视图引擎

    继上周介绍了Razor之后,ASP.NET MVC 现在已有四种主要的视图引擎.其他三种引擎是Spark.NHaml和传统的ASPX文件模板.本文将大致介绍这四种引擎,并着重讨论新的Razor引 擎. ...

  8. 【枚举】【SDOI 2011】【bzoj 2241】打地鼠

    2241: [SDOI2011]打地鼠 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 877 Solved: 557 Description 打地鼠是 ...

  9. prometheus的坑

    prometheus是一个用于监控k8s集群状态的工具.今天在主机上配置这个东西,遇到了一个坑,调查了一段时间才解决,记之. 首先,根据网上的教程,利用helm安装这个东西很方便,只要三条指令(ref ...

  10. python——二进制/十进制等转换

    To 十进制 八进制: >>> int('10', 8) ->8   To 十六进制: 十进制: >>> hex(12) ->‘0xc’ 二进制: &g ...