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 = 0;
    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 = -1;
        goto exit;
    }

    /*Set the encoding parameters.*/
    ret_code = lame_init_params(gfp);
    if(ret_code < 0)
    {
        printf("lame_init_params returned %d\n", ret_code);
        status = -1;
        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 *2);
    mp3_buffer = (char*)malloc(MP3BUFSIZE);

    /*Read from the input file, encode, and write to be output file.*/
    do{
        input_samples = fread(input_buffer, 2, INBUFSIZE, intfp);
        if(input_samples > 0)
        {
            mp3_bytes = lame_encode_buffer_interleaved(
                gfp,
                input_buffer,
                input_samples / 2,
                mp3_buffer,
                MP3BUFSIZE
            );
            if(mp3_bytes < 0)
            {
                printf("lame_encode_buffer_interleaved returned %d\n", mp3_bytes);
                status = -1;
                goto free_buffers;
            }else if(mp3_bytes > 0)
            {
                fwrite(mp3_buffer, 1, 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 > 0)
    {
        printf("writing %d mp3 bytes\n", mp3_bytes);
        fwrite(mp3_buffer, 1, 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 < 3)
    {
        printf("usage: clame rewinfile mp3outfile\n");
        exit(1);
    }
    encode(argv[1], argv[2]);
    return 0;
}
/*
// 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, 0, 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
*/

 

转载于:https://www.cnblogs.com/zhangdewang/p/8685941.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值