ffmpeg庫音頻解碼示例

#include <stdio.h>
#include <stdlib.h>
extern "C"{//
#include "avcodec.h"
#include "avformat.h"
}


int main(char arg,char *argv[])
{
????char *filename ="02.swf";
????
????av_register_all();//注冊所有可解碼類型
????AVFormatContext *pInFmtCtx=NULL;//文件格式
????AVCodecContext *pInCodecCtx=NULL;//編碼格式
????if (av_open_input_file(&pInFmtCtx,filename,NULL, 0, NULL)!=0)//獲取文件格式
????????printf("av_open_input_file error\n");
????if(av_find_stream_info(pInFmtCtx) < 0)//獲取文件內音視頻流的信息
????????printf("av_find_stream_info error\n");
????
????unsigned int j;
????// Find the first audio stream

????int????audioStream = -1;
????for(j=0; j<pInFmtCtx->nb_streams; j++)//找到音頻對應的stream
????????if(pInFmtCtx->streams[j]->codec->codec_type==CODEC_TYPE_AUDIO)
????????{
????????????audioStream=j;
????????????break;
????????}
????????if(audioStream==-1)
????????{
????????????printf("input file has no audio stream\n");
????????????return 0; // Didn't find a audio stream

????????}
????????printf("audio stream num: %d\n",audioStream);
????????pInCodecCtx = pInFmtCtx->streams[audioStream]->codec;//音頻的編碼上下文
????????AVCodec *pInCodec=NULL;
????????/* FILE *file3 = fopen("codec_private_data_size.txt","w");
????????for(int i = 0; i <200000; i++)
????????{
????????pInCodec = avcodec_find_decoder((CodecID)i);
????????if (pInCodec!=NULL)
????????{
????????fprintf(file3,"%s %d\n",pInCodec->name,pInCodec->priv_data_size );
????????}
????????}
????????fclose(file3);
????????system("pause");
????????*/

????????pInCodec = avcodec_find_decoder(pInCodecCtx->codec_id);//根據編碼ID找到用于解碼的結構體
????????if(pInCodec==NULL)
????????{
????????????printf("error no Codec found\n");
????????????return -1 ; // Codec not found
????????}


????????//使用test的代替pInCodecCtx也可以完成解碼,可以看出只要獲取以下幾個重要信息就可以實現解碼和重采樣
????????AVCodecContext *test = avcodec_alloc_context();
????????test->bit_rate = pInCodecCtx->bit_rate;//重采樣用
????????test->sample_rate = pInCodecCtx->sample_rate;//重采樣用
????????test->channels = pInCodecCtx->channels;//重采樣用
????????test->extradata = pInCodecCtx->extradata;//若有則必有
????????test->extradata_size = pInCodecCtx->extradata_size;//若有則必要
????????test->codec_type = CODEC_TYPE_AUDIO;//不必要
????????test->block_align = pInCodecCtx->block_align ;//必要

????????
????????if(avcodec_open(test, pInCodec)<0)//將兩者結合以便在下面的解碼函數中調用pInCodec中的對應解碼函數
????????{
????????????printf("error avcodec_open failed.\n");
????????????return -1; // Could not open codec

????????}
????????
????????if(avcodec_open(pInCodecCtx, pInCodec)<0)
????????{
????????????printf("error avcodec_open failed.\n");
????????????return -1; // Could not open codec

????????}
????????
????????static AVPacket packet;
????????
????????printf(" bit_rate = %d \r\n", pInCodecCtx->bit_rate);
????????printf(" sample_rate = %d \r\n", pInCodecCtx->sample_rate);
????????printf(" channels = %d \r\n", pInCodecCtx->channels);
????????printf(" code_name = %s \r\n", pInCodecCtx->codec->name);
????????//printf("extra data size: %d :data%x %x %x %x\n",pInCodecCtx->extradata_size,pInCodecCtx->extradata[0]
????????//???? ,pInCodecCtx->extradata[1],pInCodecCtx->extradata[2],pInCodecCtx->extradata[3]);
????????printf(" block_align = %d\n",pInCodecCtx->block_align);
????????
????????//system("pause");
????????//

????????uint8_t *pktdata;
????????int pktsize;
????????int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE*100;
????????uint8_t * inbuf = (uint8_t *)malloc(out_size);
????????FILE* pcm,*packetinfo;
????????packetinfo = fopen("packetinfo.txt","w");
????????pcm = fopen("result.pcm","wb");
????????long start = clock();
????????while(av_read_frame(pInFmtCtx, &packet)>=0)//pInFmtCtx中調用對應格式的packet獲取函數
????????{
????????????//fprintf(packetinfo," packet { pts=%d; dts =%d;%x,%x,%x,%x; size=%d; stream_index=%d; pos=%d;}\n",

????????????// packet.pts,packet.dts,packet.data[0],packet.data[1],packet.data[2],packet.data[3],packet.size,packet.stream_index,packet.pos);

????????????if(packet.stream_index==audioStream)//如果是音頻
????????????{
????????????????pktdata = packet.data;
????????????????pktsize = packet.size;
????????????????while(pktsize>0)
????????????????{
????????????????????//fprintf(packetinfo,"packet data:%x %x %x %x %x\n",pktdata[0],pktdata[1],pktdata[2],pktdata[3],pktdata[4]);

????????????????????//fprintf(packetinfo,"packet size:%d\n\n",pktsize);

????????????????????out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE*100;
????????????????????//解碼
????????????????????int len = avcodec_decode_audio2(pInCodecCtx,(short*)inbuf,&out_size,pktdata,pktsize);
????????????????????if (len<0)
????????????????????{
????????????????????????printf("Error while decoding.\n");
????????????????????????break;
????????????????????}
????????????????????if(out_size>0)
????????????????????{
????????????????????????fwrite(inbuf,1,out_size,pcm);//pcm記錄
????????????????????????fflush(pcm);
????????????????????}
????????????????????pktsize -= len;
????????????????????pktdata += len;
????????????????}
????????????}
????????????av_free_packet(&packet);
????????}
????????long end = clock();
????????printf("cost time :%f\n",double(end-start)/(double)CLOCKS_PER_SEC);
????????free(inbuf);
????????fclose(pcm);
????????fclose(packetinfo);
????????if (pInCodecCtx!=NULL)
????????{
????????????avcodec_close(pInCodecCtx);
????????}
????????if (test!=NULL)
????????{
????????????avcodec_close(test);
????????}
????????av_free(test);
????????av_close_input_file(pInFmtCtx);
????????return 0;
}

?

一個英文版的例子(有講解)
ffmpeg的一些使用例子
mpeg and SDL Tutorial
ffmpeg編譯相關
ffmpeg工作組(中文)

?

?

?

?

?

轉自:

http://blog.chinaunix.net/u3/108358/showart_2132123.html

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

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

相關文章

SQL Where in list 問題

不過,這種做法有兩個缺陷1.Oracle In列表的數目有限制(1000)2.不能復用執行計劃,每次幾乎都是硬解析.3.In拼接可能存在SQL注入的風險

readn writen實現linux下socket緩沖區讀寫

socket上的read write 操作不同與一般的文件IO操作&#xff0c;socket上的用read write讀寫的字節數可能比要求的少,但這并不是錯誤&#xff0c;原因是socket的緩沖區可能已經達到了極限。此時所需要的就是再次調用read write 以寫入或輸出剩余的字符。這種情況在socket中很常見…

傅里葉變換進行缺陷檢測detect_indent_fft.hdev(源代碼與詳細解析)

文章目錄簡介程序解析處理結果預覽算法講解簡介 detect_indent_fft.hdev是halcon的示例程序&#xff0c;是傅里葉變換進行缺陷檢測的一個例子&#xff0c;主要是傅里葉變換在復雜背景下的缺陷檢測。 這個程序展示了如何利用快速傅里葉變換&#xff08;FFT&#xff09;對塑料制…

lua環境搭建

前言 Linux & Mac上安裝 Lua 安裝非常簡單&#xff0c;只需要下載源碼包并在終端解壓編譯即可&#xff0c;本文介紹Linux 系統上&#xff0c;lua5.3.0版本安裝步驟&#xff1a; Linux 系統上安裝 [rootgitlab ~]# mkdir /app/tools/lua -p [rootgitlab ~]# cd /app/tools/l…

八、job管理

查看用法&#xff1a; [rootsuper65 ~]# salt-run -d|grep jobsjobs.active:                      #查看當前執行的job Return a report on all actively running jobs from a job id centric salt-run jobs.activejobs.list_job: salt-run jobs.list_j…

pthread_join/pthread_exit用法實例

函數pthread_join用來等待一個線程的結束。函數原型為&#xff1a;   extern int pthread_join __P ((pthread_t __th, void **__thread_return));   第一個參數為被等待的線程標識符&#xff0c;第二個參數為一個用戶定義的指針&#xff0c;它可以用來存儲被等待線程的返回…

thinkphp5 內置接口開發與使用

最近的一個項目在用tp5&#xff0c;對于tp3都幾乎沒用過的我來說~~~ tp5最好的一點就是對接口的單獨封裝&#xff0c;只要嚴格按照要求一步一步來就可以成功了 開啟命令行&#xff1a;配置環境變量安裝tp5項目cmd進入項目目錄&#xff0c;運行php think&#xff0c;出現如下內容…

Halcon2019軟件安裝教程

文章目錄1、halcon介紹2、安裝halcon-19.11.0.0-windows.exe1、下載halcon-19.11.0.0-windows.exe安裝包2、halcon-19.11.0.0-windows.exe軟件安裝3、驗證Halcon安裝1、halcon介紹 HALCON是德國MVtec公司開發的一套完善的標準的機器視覺算法包&#xff0c;擁有應用廣泛的機器視…

爬蟲常用庫的安裝

請求庫(requests,selenium)、解析庫(beautifulsop)、存儲庫、工具庫等 urelib re 上面這兩個是python自帶的庫 需要自己安裝額庫&#xff1a; (在windows下&#xff0c;使用pip install 命令) requests selenium用來驅動瀏覽器&#xff0c;做自動化測試&#xff0c;一些被js…

Python: 編程遇到的一些問題以及網上解決辦法?

0.Python: TypeError: str does not support the buffer interface,(點我) fp.write(url.encode("utf-8")) 1.Python:object of type Response has no len()&#xff0c;如何解決&#xff1f;(點我) Traceback (most recent call last):File "F:/Python/TD.py&q…

快排簡要介紹

<!DOCTYPE html><html lang"en"><head> <meta charset"UTF-8"> <title>Title</title></head> <body> <script> var arr [6,10,2,9,3,8,11,4,5]; function quickSort(data, start, end) { // 確定要…

在django中使用celery

前言: 針對高延時任務, 直接在一次網絡請求中處理完畢會導致很不好的體驗, celery則可以不阻塞請求后臺處理這些任務, 并且可以使用django的models進行數據庫操作.環境 python models: celery-4.1.1redis-2.10.6django-1.11.7其他: redis-3.2.9macospython3.6創建django工程 dj…

關于pragma pack的用法(一)

一個很重要的參數#pragma pack(n)數據邊界對齊方式:以如下結構為例: struct {char a;WORD b;DWORD c;char d;}在Windows默認結構大小: sizeof(struct) 444416;與 #pragma pack(4)一樣若設為 #pragma pack(1), 則結構大小: sizeof(struct) 12418;若設為 #pragma pack(2), 則…

TCL語言筆記:TCL中的String命令

一、介紹 字符串是 Tcl 中的基本數據類型&#xff0c;所以有大量的字符串操作命令。一個比較重要的問題就是模式匹配&#xff0c;通過模式匹配將字符串與指定的模式&#xff08;格式&#xff09;相匹配來進行字符串的比較、搜索等操作。 二、string命令列表 命 令 說 …

一文學會,膠位偏移、缺膠、斷膠、溢膠檢測

文章目錄檢測任務檢測思路點膠質量檢測代碼及解析圖示處理思路博主寫作不容易&#xff0c;孩子需要您鼓勵 萬水千山總是情 , 先點個贊行不行 檢測任務 點膠檢查檢測以下缺陷&#xff1a; 1.缺少粘合膠的部分&#xff08;斷膠&#xff09; 2.粘合劑過多或過少的部分&#x…

『轉載』hadoop2.x常用端口、定義方法及默認端口

『轉載』hadoop2.x常用端口、定義方法及默認端口1.問題導讀 DataNode的http服務的端口、ipc服務的端口分別是哪個&#xff1f;NameNode的http服務的端口、ipc服務的端口分別是哪個&#xff1f;journalnode的http服務的端口、ipc服務的端口分別是哪個&#xff1f;ResourceManage…

宏定義和內聯函數的學習

宏定義可以提高效率&#xff0c;但是宏不是函數。 預編譯通過代碼復制的方式代替函數調用&#xff0c;省去了諸如函數壓棧等系統過程&#xff0c;從而提高了效率。但是由于宏定義僅僅是代碼替換&#xff0c;所以引起很多問題。#define MAX(a&#xff0c;b) (a) > (b) ? (…

HAProxy介紹及配置文件詳解

一、HAProxy簡介 HAProxy是一個開源的、高性能的、基于TCP和HTTP應用的負載均衡軟件&#xff0c;借助HAProxy可快速、可靠地提供基于TCP和HTTP應用的負載均衡解決方案 二、HAProxy優點 1) 可靠性和穩定性非常好&#xff0c;可以與硬件的F5相媲美2) 最高可以同時維護40000--5000…

unbutu安裝搜狗輸入法【轉載】

安裝支持庫 sudo apt-get install fcitx libssh2-1 如果安裝過程中出現錯誤失敗&#xff0c;運行apt-get -f install 查看支持庫是否安裝成功 dpkg -l | grep fcitx dpkg -l | grep libssh 下載搜狗輸入法 wget "http://pinyin.sogou.com/linux/download.php?flinux&…

深入理解halcon相機標定

目錄相機標定簡介深度說明1、相機標定參數介紹2、標定板詳細介紹問題1&#xff1a;halcon是否只能使用halcon專用的標定板&#xff1f;問題2&#xff1a;halcon標定板如何生成&#xff1f;問題3&#xff1a;halcon標定板如何擺放&#xff0c;拍照數量有無限制&#xff1f;標定步…