• 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
    ");
            status = -1;
            goto exit;
        }
    
        /*Set the encoding parameters.*/
        ret_code = lame_init_params(gfp);
        if(ret_code < 0)
        {
            printf("lame_init_params returned %d
    ", 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
    ", 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
    ", 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
    ");
            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.2include clame.c 
       C:lame-3.98.2libmp3lameReleaselibmp3lame.lib 
       C:lame-3.98.2mpglibReleasempglib.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:Pyton31include /IC:lame-3.98.2include pylame1.c clame.c 
       C:Python31libspython31.lib 
       C:lame-3.98.2libmp3lameReleaselibmp3lame.lib 
       C:lame-3.98.2mpglibReleasempglib.lib
    */
  • 相关阅读:
    Spring自定义注解简单使用四步走
    关于Mybaits映射一点心得
    设置UIButton文字大小颜色不同
    AFNetworking上传文件
    解决UITableView头部空白
    iOS获取文件和文件夹大小
    编译ffmpeg(iOS)
    让MySql支持Emoji表情
    MySQL重置密码(OSX)
    iOS多线程总结
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/8685941.html
Copyright © 2020-2023  润新知