ffmpeg-從flv文件中提取AAC音頻數據保存為文件

AAC ADTS格式協議:
從flv文件中提取AAC音頻數據保存為文件。
如果需要詳細了解AAC ADTS格式,可以查詢文檔。

原文件:
在這里插入圖片描述
提取aac文件:
在這里插入圖片描述

main.c

#include <stdio.h>
#include <libavutil/log.h>>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>#define        ADTS_HEADER_LEN      7;
const int sampling_frequencies[] =
{96000, //0x088200, //0x164000, //0x248000, //0x344100, //0x432000, //0x524000, //0x622050, //0x716000, //0x812000,  // 0x911025,  // 0xa8000   // 0xb// 0xc d e f是保留的
};int adts_header(char* const p_adts_header, const int data_length,const int profile, const int samplerate, const int channels)
{int sampling_frequencies_index = 3; //默認使用48000int adtsLen = data_length + 7;//根據輸入文件的samplerate 獲取 相應的在ADTS中設置的索引int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);int i = 0;for(i = 0; i < frequencies_size; i++){if(samplerate == sampling_frequencies[i]){sampling_frequencies_index = i;break;}}if(sampling_frequencies_index >= frequencies_size){printf("unsupport samplerate:%d\n", samplerate);return -1;}//同步頭 總是0xFFF(12個bit),代表著一個ADTS幀的開始p_adts_header[0] = 0xff;p_adts_header[1] = 0xf0;//MPEG標識符,0標識MPEG-4,1標識MPEG-2(1個bit)p_adts_header[1] |= (0 << 3);//layer,總是0(2個bit)p_adts_header[1] |= (0 << 1);//protection_absent ,表示是否誤碼校驗,1表示 沒有, 0 表示有。(1個bit)//(注意:ADTS Header的長度在protection_absent = 0 時占9個字節, protection_absent = 1時占7個字節)p_adts_header[1] |= 1;//profile 使用aac的級別(質量)(2個bit)//MPEG-4 profile://MAIN  = 0//LC    = 1//SSR   = 2//LTP   = 3p_adts_header[2] = (profile)<<6;//采樣率的索引(4個bit)p_adts_header[2] |= (sampling_frequencies_index & 0x0f) << 2;//private bit: 0 (1個bit)p_adts_header[2] |= (0 << 1);//聲道(3個bit)p_adts_header[2] |= (channels & 0x04) >> 2;p_adts_header[3] = (channels & 0x03) << 6;//original_copy = 0 (1個bit)p_adts_header[3] |= (0 << 5);//home = 0 (1個bit)p_adts_header[3] |= (0 << 4);//copyright_identification_bit = 0 (1個bit)p_adts_header[3] |= (0 << 3);//copyright_identification_start = 0 (1個bit)p_adts_header[3] |= (0 << 2);//frame_length:1個ADTS幀的長度包括ADTS頭和AAC原始流(13bit)p_adts_header[3] |= ((adtsLen & 0x1800) >> 11);p_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);p_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5);//adts_buffer_fullness:0x7FF 說明是碼率可變的碼流p_adts_header[5] |= 0x1f;p_adts_header[6] = 0xfc;//最后還有兩個bit:number_of_raw_data_blocks_in_frame//表示這個ADTS幀有幾個AAC數據塊//計算方法://number_of_raw_data_blocks_in_frame + 1個AAC原始幀。//所以說number_of_raw_data_blocks_in_frame == 0 表示說ADTS幀中有?個//AAC數據塊。p_adts_header[6] &= 0xfc;//其實上面p_adts_header[6] = 0xfc的操作這2個bit已經為0了return 0;
}int main()
{int ret = -1;char errors[1024];char* in_filename = "in_file.flv";char* aac_filename = "test_out.aac";FILE* aac_fd = NULL;int audio_index = -1;int len = 0;AVFormatContext* ifmat_ctc = NULL;AVPacket pkt;//設置打印級別av_log_set_level(AV_LOG_DEBUG);aac_fd = fopen(aac_filename, "wb");if(!aac_fd){av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", aac_filename);return -1;}//打開輸入文件if((ret = avformat_open_input(&ifmat_ctc, in_filename, NULL, NULL)) < 0){av_strerror(ret, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",in_filename,ret,errors);return -1;}//獲取解碼器信息if((ret = avformat_find_stream_info(ifmat_ctc, NULL)) < 0){av_strerror(ret, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",in_filename,ret,errors);return -1;}//dump媒體信息av_dump_format(ifmat_ctc, 0, in_filename, 0);//初始化packetav_init_packet(&pkt);//查找audio對應的stream indexaudio_index = av_find_best_stream(ifmat_ctc, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);if(audio_index < 0){av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",av_get_media_type_string(AVMEDIA_TYPE_AUDIO),in_filename);return AVERROR(EINVAL);}//打印aac級別printf("audio profile:%d , FF_PROFILE_AAC_LOW:%d\n",ifmat_ctc->streams[audio_index]->codecpar->profile,FF_PROFILE_AAC_LOW);if(ifmat_ctc->streams[audio_index]->codecpar->codec_id != AV_CODEC_ID_AAC){printf("the media file no contain AAC stream, it's codec_id is %d\n",ifmat_ctc->streams[audio_index]->codecpar->codec_id);goto END;}//讀取媒體文件,并把aac數據幀寫入本地文件while (av_read_frame(ifmat_ctc, &pkt) >=0 ){if(pkt.stream_index == audio_index){char adts_header_buf[7] = {0};//獲取ADTS幀頭信息adts_header(adts_header_buf, pkt.size,ifmat_ctc->streams[audio_index]->codecpar->profile,ifmat_ctc->streams[audio_index]->codecpar->sample_rate,ifmat_ctc->streams[audio_index]->codecpar->channels);//寫入adts header,ts流不適用,ts流分離出來的packet帶了adts headerfwrite(adts_header_buf, 1, 7, aac_fd);len = fwrite(pkt.data, 1, pkt.size, aac_fd);//寫入adts dataif(len != pkt.size){av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",len,pkt.size);}}av_packet_unref(&pkt);}END:if(ifmat_ctc)avformat_close_input(&ifmat_ctc);if(aac_fd)fclose(aac_fd);return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379195.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379195.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379195.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python-統計《水調歌頭·明月幾時有》字符出現次數。

統計《水調歌頭明月幾時有》字符出現次數。 明月幾時有&#xff0c;把酒問青天。 不知天上宮闕&#xff0c;今夕是何年&#xff1f; 我欲乘風歸去&#xff0c;又恐瓊樓玉宇&#xff0c;高處不勝寒。 起舞弄清影&#xff0c;何似在人間&#xff01; 轉朱閣&#xff0c;低綺戶&am…

Linux網絡編程入門 (轉載)

(一)Linux網絡編程--網絡知識介紹 Linux網絡編程--網絡知識介紹客戶端和服務端 網絡程序和普通的程序有一個最大的區別是網絡程序是由兩個部分組成的--客戶端和服務器端. 客戶端 在網絡程序中&#xff0c;如果一個程序主動和外面的程序通信&#xff0c;那么我們…

在Python中將字符串拆分為字符數組

Given a string and we have to split into array of characters in Python. 給定一個字符串&#xff0c;我們必須在Python中拆分為字符數組。 將字符串拆分為字符 (Splitting string to characters) 1) Split string using for loop 1)使用for循環分割字符串 Use for loop t…

SQL表值函數和標量值函數的區別 [轉]

SQL表值函數和標量值函數的區別 寫sql存儲過程經常需要調用一些函數來使處理過程更加合理&#xff0c;也可以使函數復用性更強&#xff0c;不過在寫sql函數的時候可能會發現&#xff0c;有些函數是在表值函數下寫的有些是在標量值下寫的&#xff0c;區別是表值函數只能返回一個…

N Queen(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; main.c #include <stdio.h>/* 程序描述&#xff1a;輸出N*N中符合左右對角線與上下左右方向都沒被使用的位置在每一行的所有情況使用檢測左上角&#xff0c;正上角&#xff0c;右上…

kotlin 計算平方_Kotlin程序計算自然數之和

kotlin 計算平方Given a number number, and we have to calculate the sum of all natural numbers from 1 to number. 鑒于一些數字 &#xff0c;我們必須從1計算所有自然數的總和數量 。 Example: 例&#xff1a; Input:number 15Output:120用于計算Kotlin中自然數之和的…

Python-身份證核對

中華人民共和國居民身份證號碼由17 位數字和1位校驗碼組成。其中&#xff0c;前6位為所在地編號&#xff0c;第7~14 位為出生年月日&#xff0c;第15~17位為登記流水號&#xff0c;其中第17位偶數為女性&#xff0c;奇數為男性。校驗碼的生成規則如下: 將前面的身份證號碼17位數…

VC 加載套接字庫

//加載套接字庫 WORD wVersionRequested;//套接字庫版本信息 WSADATA wsaData; int err; wVersionRequested MAKEWORD(1,1); err WSAStartup(wVersionRequested,&wsaData); if(err ! 0){ //加載失敗 return; } if(LOBYTE(wsaData.wVersion) ! 1 || //判斷是不是所請求的…

統計各種字符個數

#include <stdio.h> #include <conio.h>int main(int argc, char * argv[]) {char ch;int letters 0, space 0, digit 0, others 0;printf("請輸入一組字符串:\n");while((chgetchar())!\n){if(ch>a && ch < z || ch >A &&…

樹存儲結構(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; LinkList.h LinkList.c 線性表 GTree.h #ifndef _GTREE_H_ #define _GTREE_H_typedef void GTree;//定義樹類型 typedef void GTreeData;//定義節點中存放數據的類型 typedef void (GTre…

Python-《twinkle twinkle little star》統計單詞出現次數

統計英文兒歌《twinkle twinkle little star》中&#xff0c;使用到的單詞及其出現次數。要求去除單詞大小寫的影響&#xff0c;不統計標點符號的個數&#xff0c;并按降序輸出。 Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like…

二元矩陣峰值搜索_好斗的牛(二元搜索)

二元矩陣峰值搜索A farmer has built a long barn with N stalls. The stalls are placed in a straight manner at positions from x1, x2, ...xN. But his cows (C) are aggressive and don’t want to be near other cows. To prevent cows from hurting each other, he wan…

WinForm Paenl里面添加Form

Form7 f7 new Form7();f7.TopLevel false;f7.Parent this.panel1;this.panel1.Controls.Add(f7);f7.Show();轉載于:https://www.cnblogs.com/Haibocai/archive/2012/10/30/2746003.html

跳躍表SkipList

跳躍表(Skip List)是一種隨機化數據結構&#xff0c;基于并聯的鏈表&#xff0c;其效率可比擬于二叉查找樹(對于大多數操作需要O(log n)平均時間)。 基本上&#xff0c;跳躍列表是對有序的鏈表增加上附加的前進鏈接&#xff0c;增加是以隨機化的方式進行的&#xff0c;所以在列…

Python---冒泡排序、選擇排序

冒泡排序 依次輸入n個數&#xff0c;進行冒泡排序 冒泡排序法&#xff0c;即兩個相鄰的進行比較&#xff0c;比較之后換位置 def bubbleSort(arr):n len(arr)for i in range(n):for j in range(0, n-i-1):if arr[j] > arr[j1] :arr[j], arr[j1] arr[j1], arr[j]arr[] n…

react js 添加樣式_如何在React JS Application中添加圖像?

react js 添加樣式Hello! In this article, we will learn how to add images in React JS? I remember when I just started coding in React JS, I thought adding images would be done exactly as it is in HTML. I later realized that it was different. 你好&#xff0…

二叉樹(多路平衡搜索樹)-(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; BTree.h #ifndef _BTREE_H_ #define _BTREE_H_#define BT_LEFT 0 //定義左子節點標識 #define BT_RIGHT 1 //定義右子節點標識typedef void BTree;//定義樹類型 typedef unsigned long lo…

window service服務安裝錯誤

今天按照園子里面的文章&#xff0c;弄了一個系統服務&#xff0c;可是一直裝不上去&#xff0c; 正在運行事務處理安裝。 正在開始安裝的“安裝”階段。查看日志文件的內容以獲得 D:\TecCreateSvc\TecJsCreateService.exe 程序集的進度。該文件位于 D:\TecCreateSvc\TecJsCre…

DM9000調試記錄

最近在調試DM9000&#xff0c;遇到了很多問題&#xff0c;在網上幾乎也能找到同樣的問題&#xff0c;但是答案千變萬化&#xff0c;弄的我這樣不行&#xff0c;那樣也不行。 1、遇到的第一個問題&#xff0c;網卡不識別&#xff0c;出現的調試信息就是&#xff1a; dm9000 dm90…

Python---二分法查找

輸入n個數&#xff0c;通過二分法查找該數的下標 def binarySearch(arr,value):m 0#開始n len(arr#最后)while m<n:mid(mn)//2#計算中間位置if valuearr[mid]:#查找成功&#xff0c;返回元素對應的位置return midelif value>arr[mid]:#在后面一半元素中繼續查找mmid1e…