windows?使用ffmpeg?.a靜態(tài)庫讀取Wav音頻并保存PCM的方法
ffmpeg讀取Wav音頻并保存PCM(源代碼保存成 c/c++ 文件需要與編譯命令一致):
// test_ffmpeg.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。 // //#include <iostream> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> //#pragma comment(lib, "libavdevice.a") //#pragma comment(lib, "libavformat.a") //#pragma comment(lib, "libavutil.a") //#pragma comment(lib, "libavcodec.a") //#pragma comment(lib, "libavfilter.a") //#pragma comment(lib, "libswresample.a") //#pragma comment(lib, "libswscale.a") int main(int arvc, char*argv[]) { //av_register_all(); printf("start...\n"); AVFormatContext* formatCtx = NULL; const AVInputFormat* inputFmt = av_find_input_format(arvc > 1 ? argv[1] : "FLAC"); int ret = 0; printf("format name: %s, long name; %s\n", inputFmt->name, inputFmt->long_name); // 打開輸入流 if ((ret = avformat_open_input(&formatCtx, "D:\\project\\ffmpeg-6.1.1\\test_in.wav", inputFmt, NULL)) != 0) { printf("Unable to open input stream: %s\n", av_err2str(ret)); return -1; } // 查找音頻流信息 if (avformat_find_stream_info(formatCtx, NULL) < 0) { printf("Unable to find audio stream information: %s\n", av_err2str(ret)); return -1; } // 尋找第一個(gè)音頻流索引 int audioStreamIndex = av_find_best_stream(formatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if (audioStreamIndex < 0) { printf("Unable to find audio stream: %s\n", av_err2str(ret)); return -1; } //const AVCodecContext* codecCtx = formatCtx->streams[audioStreamIndex]->codec; AVCodecContext* pCodecCtx = avcodec_alloc_context3(NULL); if (pCodecCtx == NULL) { printf("Could not allocate AVCodecContext: %s\n", av_err2str(ret)); return -1; } avcodec_parameters_to_context(pCodecCtx, formatCtx->streams[audioStreamIndex]->codecpar); // 打開解碼器 const AVCodec* codec = avcodec_find_decoder(pCodecCtx->codec_id); if (avcodec_open2(pCodecCtx, codec, NULL) < 0) { printf("Unable to open decoder: %s\n", av_err2str(ret)); return -1; } // 創(chuàng)建輸出文件 FILE* outputFile = fopen("output.pcm", "wb"); AVPacket packet; printf("start read frame...\n"); while ((ret = av_read_frame(formatCtx, &packet)) == 0) { // 如果數(shù)據(jù)包是音頻數(shù)據(jù) if (packet.stream_index == audioStreamIndex) { AVFrame* frame = av_frame_alloc(); int gotFrame = 0; printf("read frame...index %d\n", audioStreamIndex); // 解碼音頻幀 //avcodec_decode_audio4(pCodecCtx, frame, &gotFrame, &packet); if (avcodec_send_packet(pCodecCtx, &packet) < 0 || (gotFrame = avcodec_receive_frame(pCodecCtx, frame)) < 0) { return -1; } printf("read frame...gotFrame = %d, frame size %d, frame pointer 0x%x\n", (ret), frame->linesize[0], frame->data[0]); if (gotFrame == 0) { // 處理解碼后的音頻數(shù)據(jù),例如寫入文件等 fwrite(frame->data[0], 1, frame->linesize[0], outputFile); } av_frame_free(&frame); } //av_free_packet(&packet); av_packet_unref(&packet); } printf("read frame end. ret = %s\n", av_err2str(ret)); fclose(outputFile); avformat_close_input(&formatCtx); return 0; } // 運(yùn)行程序: Ctrl + F5 或調(diào)試 >“開始執(zhí)行(不調(diào)試)”菜單 // 調(diào)試程序: F5 或調(diào)試 >“開始調(diào)試”菜單 // 入門使用技巧: // 1. 使用解決方案資源管理器窗口添加/管理文件 // 2. 使用團(tuán)隊(duì)資源管理器窗口連接到源代碼管理 // 3. 使用輸出窗口查看生成輸出和其他消息 // 4. 使用錯(cuò)誤列表窗口查看錯(cuò)誤 // 5. 轉(zhuǎn)到“項(xiàng)目”>“添加新項(xiàng)”以創(chuàng)建新的代碼文件,或轉(zhuǎn)到“項(xiàng)目”>“添加現(xiàn)有項(xiàng)”以將現(xiàn)有代碼文件添加到項(xiàng)目 // 6. 將來,若要再次打開此項(xiàng)目,請(qǐng)轉(zhuǎn)到“文件”>“打開”>“項(xiàng)目”并選擇 .sln 文件
編譯命令:
gcc -c test.c -o test.o -I./ffmpeg-bin/include gcc -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -Wl,--nxcompat,--dynamicbase -Wl,--high-entropy-va -Wl,--as-needed -Wl,--warn-common -Wl,-rpath-link=:libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil -Wl,--image-base,0x140000000 -o test_g test.o -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lpsapi -lole32 -lstrmiids -luuid -loleaut32 -lshlwapi -lgdi32 -lm -latomic -lvfw32 -lm -latomic -lm -latomic -lz -lsecur32 -lws2_32 -liconv -lm -latomic -lmfuuid -lole32 -lstrmiids -lole32 -luser32 -lz -lm -latomic -lm -latomic -lm -luser32 -lbcrypt -latomic -lole32 -lpsapi -lshell32 strip -o test_1.exe test_g.exe # 使用upx壓縮應(yīng)用(需要windows終端執(zhí)行) upx.exe -9 -o test.exe test_1.exe rm -rf test_g.exe test_1.exe ./test.exe
到此這篇關(guān)于windows 使用ffmpeg .a靜態(tài)庫讀取Wav音頻并保存PCM的方法的文章就介紹到這了,更多相關(guān)windows 使用ffmpeg .a靜態(tài)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QTimer與QTime實(shí)現(xiàn)電子時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了QTimer與QTime實(shí)現(xiàn)電子時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07C++實(shí)現(xiàn)WebSocket服務(wù)器的案例分享
WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的通信協(xié)議,與HTTP協(xié)議不同,它允許服務(wù)器主動(dòng)向客戶端發(fā)送數(shù)據(jù),而不需要客戶端明確地請(qǐng)求,本文主要給大家介紹了C++實(shí)現(xiàn)WebSocket服務(wù)器的案例,需要的朋友可以參考下2024-05-05C++之關(guān)于string對(duì)象的大小比較
這篇文章主要介紹了C++之關(guān)于string對(duì)象的大小比較方式,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11C++11中l(wèi)onglong超長(zhǎng)整型和nullptr初始化空指針
本文介紹?C++11?標(biāo)準(zhǔn)中新添加的?long?long?超長(zhǎng)整型和?nullptr?初始化空指針,在?C++11?標(biāo)準(zhǔn)下,相比?NULL?和?0,使用?nullptr?初始化空指針可以令我們編寫的程序更加健壯,本文結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友跟隨小編一起看看吧2022-12-12Linux中rm命令使用以及C/C++代碼實(shí)現(xiàn)
m 是remove 的縮寫,Linux中 rm 命令的功能為刪除一個(gè)目錄中的一個(gè)或多個(gè)文件或目錄,它也可以將某個(gè)目錄及其下的所有文件及子目錄均刪除,這篇文章主要給大家介紹了關(guān)于Linux中rm命令使用以及C/C++代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-04-04C語言實(shí)現(xiàn)手寫Map(數(shù)組+鏈表+紅黑樹)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)手寫Map(數(shù)組+鏈表+紅黑樹),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定借鑒價(jià)值,需要的可以參考一下2022-09-09