ffmpeg 实现音频aac编码
2013-12-23 00:16
573 查看
1、编译ffmepg
./configure --disable-yasm --enable-nonfree --enable-libfaac --prefix=/home/ffmpeg/1_ffmpeg-2.1.1/install
2、编译audio_enc.c
makefile:
注意:在这里编译的时候需要加上aac库,可能会找不到库函数undefined reference to `faacEncEncode'等
程序运行时,需要提供一个和程序参数一致的wav音频文件:
./configure --disable-yasm --enable-nonfree --enable-libfaac --prefix=/home/ffmpeg/1_ffmpeg-2.1.1/install
2、编译audio_enc.c
makefile:
#!/bin/sh INCLUDE = ../include LIB_DIR = ../lib LDFLAGS = -lfaac -lavcodec -lavformat -lavdevice -lavfilter -lavutil -lswresample -pthread -ldl -lswscale -lasound -lz -lm -lbz2 SRC=audio_enc.c all:$(SRC) gcc -g -Wall $(SRC) -o target -I $(INCLUDE) -L $(LIB_DIR) $(LDFLAGS)
注意:在这里编译的时候需要加上aac库,可能会找不到库函数undefined reference to `faacEncEncode'等
程序运行时,需要提供一个和程序参数一致的wav音频文件:
/* * Copyright(C), 2013, Ubuntu Inc. * File name: audio_enc.c * Author: xubinbin 徐彬彬 (Beijing China) * Version: 1.0 * Date: 2013.12.23 * Description: Use ffmpeg achieve aac audio coding. * Function List: * Email: xubbwd@gmail.com */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include <libavutil/opt.h> #include <libavutil/mathematics.h> #include <libavformat/avformat.h> FILE * fp_in = NULL; FILE * fp_out = NULL; static int frame_count; int main(int argc, char **argv) { int ret; AVCodec *audio_codec; AVCodecContext *c; AVFrame *frame; AVPacket pkt; int got_output; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); avcodec_register_all(); //avdevice_register_all(); audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC); c = avcodec_alloc_context3(audio_codec); c->codec_id = AV_CODEC_ID_AAC; c->sample_fmt = AV_SAMPLE_FMT_S16; c->sample_rate = 44100; c->channels = 2; c->channel_layout = AV_CH_LAYOUT_STEREO; c->bit_rate = 64000; /* open the codec */ ret = avcodec_open2(c, audio_codec, NULL); if (ret < 0) { fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret)); exit(1); } /* allocate and init a re-usable frame */ frame = avcodec_alloc_frame(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); } frame->nb_samples = c->frame_size; frame->format = c->sample_fmt; frame->channels = c->channels; frame->channel_layout = c->channel_layout; frame->linesize[0] = 4096; frame->extended_data = frame->data[0] = av_malloc((size_t)frame->linesize[0]); av_init_packet(&pkt); fp_in = fopen("in.wav","rb"); fp_out= fopen("out.aac","wb"); //printf("frame->nb_samples = %d\n",frame->nb_samples); while(1) { frame_count++; bzero(frame->data[0],frame->linesize[0]); ret = fread(frame->data[0],frame->linesize[0],1,fp_in); if(ret <= 0) { printf("read over !\n"); break; } ret = avcodec_encode_audio2(c, &pkt, frame, &got_output); if (ret < 0) { fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret)); exit(1); } if(got_output > 0) { //printf("pkt.size = %d\n",pkt.size); fwrite(pkt.data,pkt.size,1,fp_out); av_free_packet(&pkt); } #if 0 if(frame_count > 10) { printf("break @@@@@@@@@@@@\n"); break; } #endif } avcodec_close(c); av_free(c); avcodec_free_frame(&frame); fclose(fp_in); fclose(fp_out); return 0; }
相关文章推荐
- FFmpeg 音频编码(PCM数据编码成AAC android)
- ffmpeg实战教程(三)音频PCM采样为AAC,视频YUV编码为H264/HEVC
- 流媒体技术学习笔记之(六)FFmpeg官方文档先进音频编码(AAC)
- 多媒体开发之音频编码---ffmpeg 编码aac
- 基于FFmpeg的音频编码(PCM数据编码成AAC android)
- 编码音频aac的时候 (avcodec_encode_audio2)提示FFMPEG more samples than frame size 的错误
- 实现用FFmpeg接收RTSP,把H264视频和AAC音频录制成MP4文件(附完整代码)
- C++实现RTMP协议发送H.264编码及AAC编码的音视频
- ffmpeg--音频的解码和编码
- fdk_aac编码,每帧音频的采样率
- [原创]桓泽学音频编解码(4):MP3 和 AAC 中反量化原理,优化设计与参考代码中实现
- 音频编码方案之间音质比较(AAC,MP3,WMA等)
- FAAC源码阅读(1)——AAC和音频编码的介绍
- FFmpeg3.3.2+SDL2实现流媒体音频播放
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
- javaCV开发详解之7:让音频转换更加简单,实现通用音频编码格式转换、重采样等音频参数的转换功能(以pcm16le编码的wav转mp3为例)
- 使用FFmpeg类库实现YUV视频序列编码为视频
- 使用FFMPEG实现音频播放器
- iOS开发 ( iPhone/iPad):利用ffmpeg 实现音频解码、声音播放
- FFmpeg浅尝辄止(四)——音频的解码和编码