faac 例子

/*
 * FAAC - Freeware Advanced Audio Coder
 * Copyright (C) 2001 Menno Bakker
 * Copyright (C) 2002-2017 Krzysztof Nikiel
 * Copyright (C) 2004 Dan Villiom P. Christiansen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <signal.h>

#include <faac.h>


int main(int argc, char *argv[])
{

	faacEncHandle hEncoder;
	faacEncConfigurationPtr myFormat;

	unsigned long samplesInput, maxBytesOutput;

	char *audioFileName = NULL;
	char *aacFileName = NULL;
	int16_t *tmpbuf;
	unsigned char *bitbuf;
	FILE *f;
	FILE *outfile = NULL;
    int rawChans = 1;           // disabled by default
    int rawBits = 16;
    int rawRate = 8000;


    /* begin process command line */
    if (argc < 3)
    {
        printf("Usage: %s input_file output_file\n", argv[0]);
        return 1;
    }

	audioFileName = argv[1];
	aacFileName = argv[2];
  
	f = fopen(audioFileName,"rb");

    /* open the encoder library */
    hEncoder = faacEncOpen(rawRate, rawChans,
                           &samplesInput, &maxBytesOutput);

    if (hEncoder == NULL)
    {
        fprintf(stderr, "Couldn't open encoder instance for input file %s\n",
                audioFileName);
        fclose(f);
        return 1;
    }

	tmpbuf = (int16_t *) malloc(samplesInput);
    bitbuf = (unsigned char *) malloc(maxBytesOutput * sizeof(unsigned char));

    /* put the options in the configuration struct */
    myFormat = faacEncGetCurrentConfiguration(hEncoder);
    myFormat->aacObjectType = LOW;
    myFormat->mpegVersion = MPEG2;
    myFormat->useTns = 0;
	myFormat->bitRate = 64000;
    myFormat->bandWidth = rawRate/2;
    myFormat->outputFormat = ADTS_STREAM;
    myFormat->inputFormat = FAAC_INPUT_16BIT;
    if (!faacEncSetConfiguration(hEncoder, myFormat))
    {
        fprintf(stderr, "Unsupported output format!\n");
        return 1;
    }

    outfile = fopen(aacFileName, "wb");
    if (!outfile)
    {
        fprintf(stderr, "Couldn't create output file %s\n", aacFileName);
        return 1;
    }

    /* encoding loop */

    while (1)
    {
        int bytesWritten, samplesRead;

		samplesRead = fread(tmpbuf, 2, samplesInput, f);

		if(samplesRead<=0)
		{
			perror(":");
			break;
		}

        /* call the actual encoding routine */
        bytesWritten = faacEncEncode(hEncoder,
                                     (int32_t *) tmpbuf,
                                     samplesRead, bitbuf, maxBytesOutput);


        if (bytesWritten > 0)
        {
            fwrite(bitbuf, 1, bytesWritten, outfile);
        }
		
    }

//	free(tmpbuf);
//	free(bitbuf);
    

    fclose(f);
	fclose(outfile);

	faacEncClose(hEncoder);

    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值