#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. 【记忆化搜索】bzoj1048 [HAOI2007]分割矩阵

    标准差=√(Σ(xi-xba)2/n)=Σ(xi)2+xba*n-2*xba*sum.只需最小化每个分割出来的矩阵的平方和即可. #include<cstdio> #include< ...

  2. 网络编程-tcp

    一.简单的demo (1)客户端 package com.songyan.tcp; import java.io.IOException; import java.io.InputStream; im ...

  3. git强行推送到远端

    回到某一个节点git reset --hard 5db4eddeca2然后把现在这个节点,强行推送到远端git push -f origin master

  4. 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架之工作流开发随笔三

    前言 “厂长,APP的那几个功能都差不多了,接下来要做工作流,工作流这东西我完全没概念啊.” “查尔斯,一般来说工作流就是指将指定的数据.文件.任务按照预定的规则进行传递流转.比如说你要请假,拿个请假 ...

  5. TELNET终端类型选项

    转:http://www.cnpaf.net/Class/Telnet/200408/5.html 1. 命令名称及编号TERMINAL-TYPE242.命令含义IACWILLTERMINAL-TYP ...

  6. 推荐10个免费的HTML编辑器

    如果你想开发一个网站,你肯定想要一个很棒的HTML编辑器,一个好的编辑器可以让代码更加整齐格式化,前端显示也会更好,从而提升你的工作效率.下面就为开发者推荐10个免费的HTML编辑器,你可以尝试使用. ...

  7. 关于在.NET中 DAL+IDAL+Model+BLL+Web

    其实三层架构是一个程序最基本的 在.Net开发中通常是多层开发比如说    BLL 就是business Logic laywer(业务逻辑层) 他只负责向数据提供者也就是DAL调用数据 然后传递给 ...

  8. ODATA4 及实现

    ODATA4 的JAVASCRIPT 实现:     http://jaydata.org/ ODATA4 的JAVA 项目  Apache Olingo:http://olingo.incubato ...

  9. shell用法 (cat << EOF)

    下面的语句会创建不存在的secure.config,如果存在直接追加,然后把多行内容: [database]        password = gerrit 写入文件secure.config ca ...

  10. Windows 2003 R2

    微软发布Windows Server 2003 R2版的目的是希望透过它填补Windows Server 2003 SP1和Longhorn Server之间的产品发布时间间隔. 微软向产品测试人员表 ...