python C example:encode mp3 code

來源:互聯網
上載者:User

標籤:fread   UI   leave   enc   bytes   fwrite   init   color   int   

#include <stdio.h>#include <stdlib.h>#include <lame.h>#define INBUFSIZE 4096#define MP3BUFSIZE (int)(1.25 * INBUFSIZE) + 7200int 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//windowscl /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//windowscl /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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.