日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【开源项目】基于FFmpeg的PCM数据编码为AAC

發(fā)布時(shí)間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【开源项目】基于FFmpeg的PCM数据编码为AAC 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/* * 一笑奈何 * cn-yixiaonaihe.blog.csdn.net */#include <iostream> #include <thread> extern "C" { #include "libavformat/avformat.h" #include "libavcodec/avcodec.h" #include "libswscale/swscale.h" #include "libswresample/swresample.h" } using namespace std; static double r2d(AVRational r) {return r.den == 0 ? 0 : (double)r.num / (double)r.den; } void XSleep(int ms) {//c++ 11chrono::milliseconds du(ms);this_thread::sleep_for(du); } int main(int argc, char *argv[]) {cout << "Test Demux FFmpeg.club" << endl;const char *url = "test.pcm";const char *outfile = "test.aac";//初始化封裝庫(kù)av_register_all();//初始化網(wǎng)絡(luò)庫(kù) (可以打開(kāi)rtsp rtmp http 協(xié)議的流媒體視頻)avformat_network_init();//注冊(cè)解碼器avcodec_register_all();//1 打開(kāi)音頻編碼器AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC);if (!codec){cout << "avcodec_find_encoder NO" << endl;return -1;}AVCodecContext *c = avcodec_alloc_context3(codec);if (!codec){cout << "avcodec_alloc_context3 NO" << endl;return -1;}c->bit_rate = 64000;c->sample_rate = 44100;c->sample_fmt = AV_SAMPLE_FMT_FLTP;c->channel_layout = AV_CH_LAYOUT_STEREO;c->channels = 2;c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;int ret = avcodec_open2(c, codec, NULL);if (ret < 0){cout << "avcodec_open2 NO" << endl;return -1;}cout << "avcodec_open2 OK" << endl;//2 打開(kāi)輸出封裝上下文AVFormatContext *oc = NULL;avformat_alloc_output_context2(&oc, NULL,NULL,outfile);if (!oc){cout << "avformat_alloc_output_context2 NO" << endl;return -1;}AVStream *st = avformat_new_stream(oc, NULL);st->codecpar->codec_tag = 0;avcodec_parameters_from_context(st->codecpar, c);av_dump_format(oc, 0, outfile, 1);//3 open io & write head ret=avio_open(&oc->pb, outfile, AVIO_FLAG_WRITE);if (ret<0){cout << "avio_open NO" << endl;return -1;}ret = avformat_write_header(oc,NULL);//44100 16位 雙通道//4 創(chuàng)建音頻重采樣上下文//音頻重采樣 上下文初始化SwrContext *actx = swr_alloc();actx = swr_alloc_set_opts(actx,c->channel_layout, //輸出格式c->sample_fmt, //輸出樣本格式c->sample_rate, //輸出采樣率AV_CH_LAYOUT_STEREO,//輸入格式AV_SAMPLE_FMT_S16,44100,0, 0);ret = swr_init(actx);if (ret != 0){char buf[1024] = { 0 };av_strerror(ret, buf, sizeof(buf) - 1);cout << "swr_init failed! :" << buf << endl;return -1;}//5 打開(kāi)輸入音頻文件進(jìn)行重采樣AVFrame *frame = av_frame_alloc();frame->format = AV_SAMPLE_FMT_FLTP;frame->channels = 2;frame->channel_layout = AV_CH_LAYOUT_STEREO;frame->nb_samples = 1024; //一幀音頻存放的樣本數(shù)量ret=av_frame_get_buffer(frame, 0);if (ret < 0){cout << "av_frame_get_buffer No :" << endl;return -1;}char *pcm = NULL;int readSize = frame->nb_samples * 2 * 2;pcm = new char[readSize];FILE *fp = fopen(url,"rb");for (;;){int len = fread(pcm, 1, readSize, fp);if (len <= 0)break;const uint8_t *data[1];data[0] = (uint8_t*)pcm;ret = swr_convert(actx,frame->data, frame->nb_samples, //輸出data, frame->nb_samples); //輸入if (len <= 0)break;AVPacket pkt;av_init_packet(&pkt);//音頻編碼ret=avcodec_send_frame(c, frame);if (ret != 0)continue;ret = avcodec_receive_packet(c, &pkt);if (ret != 0)continue;//7 音頻封裝進(jìn)AAC文件pkt.stream_index = 0;pkt.pts = 0;pkt.dts = 0;ret = av_interleaved_write_frame(oc, &pkt);cout << "[" << len << "]";}delete pcm;//寫(xiě)入視頻索引尾部信息av_write_trailer(oc);//主動(dòng)關(guān)閉后才可以把緩沖區(qū)的內(nèi)容寫(xiě)到文件avio_close(oc->pb);//清理空間av_frame_free(&frame);//先關(guān)閉后釋放swr_close(actx);swr_free(&actx);//清理封裝輸出上下文avformat_free_context(oc);//關(guān)閉編碼器avcodec_close(c);//清理編碼器上下文avcodec_free_context(&c);fclose(fp);cout << "==================end===============" << endl;return 0;}

?

總結(jié)

以上是生活随笔為你收集整理的【开源项目】基于FFmpeg的PCM数据编码为AAC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。