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

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

生活随笔

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

综合教程

基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)

發(fā)布時(shí)間:2023/12/3 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

=====================================================

基于最簡(jiǎn)單的FFmpeg封裝工藝的系列文章上市:

最簡(jiǎn)單的基于FFmpeg的封裝格式處理:視音頻分離器簡(jiǎn)化版(demuxer-simple)

最簡(jiǎn)單的基于FFmpeg的封裝格式處理:視音頻分離器(demuxer)

最簡(jiǎn)單的基于FFmpeg的封裝格式處理:視音頻復(fù)用器(muxer)

最簡(jiǎn)單的基于FFMPEG的封裝格式處理:封裝格式轉(zhuǎn)換(remuxer)

=====================================================

簡(jiǎn)介

打算記錄一下基于FFmpeg的封裝格式處理方面的樣例。包括了視音頻分離,復(fù)用,封裝格式轉(zhuǎn)換。有關(guān)封轉(zhuǎn)格式轉(zhuǎn)換的樣例在之前的文章:《最簡(jiǎn)單的基于FFMPEG的封裝格式轉(zhuǎn)換器(無(wú)編解碼)》中已經(jīng)有過(guò)記錄。不再反復(fù)。

因此計(jì)劃寫(xiě)3篇文章分別記錄視音頻的復(fù)用器(Muxer)和分離器(Demuxer)。當(dāng)中視音頻分離器(Demuxer)記錄2篇:一篇簡(jiǎn)單的,一篇標(biāo)準(zhǔn)的。

簡(jiǎn)單的版本號(hào)更適合剛開(kāi)始學(xué)習(xí)的人學(xué)習(xí)。

本文是第1篇。

首先記錄一個(gè)基于FFmpeg的視音頻分離器簡(jiǎn)單版(Simplest FFmpeg Demuxer Simple)。視音頻分離器(Demuxer)即是將封裝格式數(shù)據(jù)(比如MKV)中的視頻壓縮數(shù)據(jù)(比如H.264)和音頻壓縮數(shù)據(jù)(比如AAC)分離開(kāi)。

如圖所看到的。在這個(gè)過(guò)程中并不涉及到編碼和解碼。


本文記錄的程序?qū)⒁粋€(gè)FLV封裝的文件(當(dāng)中視頻編碼為H.264,音頻編碼為MP3)分離成為兩個(gè)文件:一個(gè)H.264編碼的視頻碼流文件,一個(gè)MP3編碼的音頻碼流文件。

須要注意的是。本文介紹的是一個(gè)簡(jiǎn)單版的視音頻分離器(Demuxer)。

該分離器的優(yōu)點(diǎn)是代碼十分簡(jiǎn)單,非常好理解。

可是缺點(diǎn)是并不適用于一些格式。

對(duì)于MP3編碼的音頻是沒(méi)有問(wèn)題的??墒窃诜蛛xMP4/FLV/MKV等一些格式中的AAC編碼的碼流的時(shí)候,得到的AAC碼流是不能播放的。

原因是存儲(chǔ)AAC數(shù)據(jù)的AVPacket的data字段中的數(shù)據(jù)是不包括7字節(jié)ADTS文件頭的“砍頭”的數(shù)據(jù),是無(wú)法直接解碼播放的(當(dāng)然假設(shè)在這些數(shù)據(jù)前面手工加上7字節(jié)的ADTS文件頭的話,就能夠播放了)。

參考文章:使用FFMPEG類庫(kù)分離出多媒體文件里的音頻碼流

分離某些封裝格式中的H.264

分離某些封裝格式(比如MP4/FLV/MKV等)中的H.264的時(shí)候。須要首先寫(xiě)入SPS和PPS,否則會(huì)導(dǎo)致分離出來(lái)的數(shù)據(jù)沒(méi)有SPS、PPS而無(wú)法播放。H.264碼流的SPS和PPS信息存儲(chǔ)在AVCodecContext結(jié)構(gòu)體的extradata中。

須要使用ffmpeg中名稱為“h264_mp4toannexb”的bitstream filter處理。有兩種處理方式:

(1)使用bitstream filter處理每一個(gè)AVPacket(簡(jiǎn)單)

把每一個(gè)AVPacket中的數(shù)據(jù)(data字段)經(jīng)過(guò)bitstream filter“過(guò)濾”一遍。關(guān)鍵函數(shù)是av_bitstream_filter_filter()。演示樣例代碼例如以下。

	AVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb"); while(av_read_frame(ifmt_ctx, &pkt)>=0){if(pkt.stream_index==videoindex){av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);fwrite(pkt.data,1,pkt.size,fp_video);//...}av_free_packet(&pkt);}av_bitstream_filter_close(h264bsfc);  

上述代碼中。把a(bǔ)v_bitstream_filter_filter()的輸入數(shù)據(jù)和輸出數(shù)據(jù)(分別相應(yīng)第4,5,6,7個(gè)參數(shù))都設(shè)置成AVPacket的data字段就能夠了。

須要注意的是bitstream filter須要初始化和銷毀,分別通過(guò)函數(shù)av_bitstream_filter_init()和av_bitstream_filter_close()。

經(jīng)過(guò)上述代碼處理之后,AVPacket中的數(shù)據(jù)有例如以下變化:

*每一個(gè)AVPacket的data加入了H.264的NALU的起始碼{0,0,0,1}

*每一個(gè)IDR幀數(shù)據(jù)前面加入了SPS和PPS

(2)手工加入SPS。PPS(略微復(fù)雜)

將AVCodecContext的extradata數(shù)據(jù)經(jīng)過(guò)bitstream filter處理之后得到SPS、PPS??截愔撩恳粋€(gè)IDR幀之前。以下代碼演示樣例了寫(xiě)入SPS、PPS的過(guò)程。

FILE *fp=fopen("test.264","ab");
AVCodecContext *pCodecCtx=...  
unsigned char *dummy=NULL;   
int dummy_len;  
AVBitStreamFilterContext* bsfc =  av_bitstream_filter_init("h264_mp4toannexb");    
av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0);  
fwrite(pCodecCtx->extradata,pCodecCtx-->extradata_size,1,fp);  
av_bitstream_filter_close(bsfc);    
free(dummy);  

然后改動(dòng)AVPacket的data。把前4個(gè)字節(jié)改為起始碼。

演示樣例代碼例如以下所看到的。

char nal_start[]={0,0,0,1};
memcpy(packet->data,nal_start,4);

經(jīng)過(guò)上述兩步也能夠得到能夠播放的H.264碼流,相對(duì)于第一種方法來(lái)說(shuō)復(fù)雜一些。

參考文章:使用FFMPEG類庫(kù)分離出多媒體文件里的H.264碼流

當(dāng)封裝格式為MPEG2TS的時(shí)候,不存在上述問(wèn)題。

流程

程序的流程例如以下圖所看到的。

從流程圖中能夠看出,將每一個(gè)通過(guò)av_read_frame()獲得的AVPacket中的數(shù)據(jù)直接寫(xiě)入文件就可以。

簡(jiǎn)介一下流程中各個(gè)重要函數(shù)的意義:
avformat_open_input():打開(kāi)輸入文件。
av_read_frame():獲取一個(gè)AVPacket。
fwrite():依據(jù)得到的AVPacket的類型不同。分別寫(xiě)入到不同的文件里。

代碼

以下貼上代碼:

/*** 最簡(jiǎn)單的基于FFmpeg的視音頻分離器(簡(jiǎn)化版)* Simplest FFmpeg Demuxer Simple** 雷霄驊 Lei Xiaohua* leixiaohua1020@126.com* 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù)* Communication University of China / Digital TV Technology* http://blog.csdn.net/leixiaohua1020** 本程序能夠?qū)⒎庋b格式中的視頻碼流數(shù)據(jù)和音頻碼流數(shù)據(jù)分離出來(lái)。* 在該樣例中, 將FLV的文件分離得到H.264視頻碼流文件和MP3* 音頻碼流文件。** 注意:* 這個(gè)是簡(jiǎn)化版的視音頻分離器。與原版的不同在于,沒(méi)有初始化輸出* 視頻流和音頻流的AVFormatContext。而是直接將解碼后的得到的* AVPacket中的的數(shù)據(jù)通過(guò)fwrite()寫(xiě)入文件。這樣做的優(yōu)點(diǎn)是流程比* 較簡(jiǎn)單。

壞處是對(duì)一些格式的視音頻碼流是不適用的,比方說(shuō) * FLV/MP4/MKV等格式中的AAC碼流(上述封裝格式中的AAC的AVPacket中 * 的數(shù)據(jù)缺失了7字節(jié)的ADTS文件頭)。 * * * This software split a media file (in Container such as * MKV, FLV, AVI...) to video and audio bitstream. * In this example, it demux a FLV file to H.264 bitstream * and MP3 bitstream. * Note: * This is a simple version of "Simplest FFmpeg Demuxer". It is * more simple because it doesn't init Output Video/Audio stream's * AVFormatContext. It write AVPacket's data to files directly. * The advantages of this method is simple. The disadvantages of * this method is it's not suitable for some kind of bitstreams. For * example, AAC bitstream in FLV/MP4/MKV Container Format(data in * AVPacket lack of 7 bytes of ADTS header). * */ #include <stdio.h> #define __STDC_CONSTANT_MACROS #ifdef _WIN32 //Windows extern "C" { #include "libavformat/avformat.h" }; #else //Linux... #ifdef __cplusplus extern "C" { #endif #include <libavformat/avformat.h> #ifdef __cplusplus }; #endif #endif //'1': Use H.264 Bitstream Filter #define USE_H264BSF 1 int main(int argc, char* argv[]) { AVFormatContext *ifmt_ctx = NULL; AVPacket pkt; int ret, i; int videoindex=-1,audioindex=-1; const char *in_filename = "cuc_ieschool.flv";//Input file URL const char *out_filename_v = "cuc_ieschool.h264";//Output file URL const char *out_filename_a = "cuc_ieschool.mp3"; av_register_all(); //Input if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) { printf( "Could not open input file."); return -1; } if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) { printf( "Failed to retrieve input stream information"); return -1; } videoindex=-1; for(i=0; i<ifmt_ctx->nb_streams; i++) { if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){ videoindex=i; }else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){ audioindex=i; } } //Dump Format------------------ printf("\nInput Video===========================\n"); av_dump_format(ifmt_ctx, 0, in_filename, 0); printf("\n======================================\n"); FILE *fp_audio=fopen(out_filename_a,"wb+"); FILE *fp_video=fopen(out_filename_v,"wb+"); /* FIX: H.264 in some container format (FLV, MP4, MKV etc.) need "h264_mp4toannexb" bitstream filter (BSF) *Add SPS,PPS in front of IDR frame *Add start code ("0,0,0,1") in front of NALU H.264 in some container (MPEG2TS) don't need this BSF. */ #if USE_H264BSF AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb"); #endif while(av_read_frame(ifmt_ctx, &pkt)>=0){ if(pkt.stream_index==videoindex){ #if USE_H264BSF av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0); #endif printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts); fwrite(pkt.data,1,pkt.size,fp_video); }else if(pkt.stream_index==audioindex){ /* AAC in some container format (FLV, MP4, MKV etc.) need to add 7 Bytes ADTS Header in front of AVPacket data manually. Other Audio Codec (MP3...) works well. */ printf("Write Audio Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts); fwrite(pkt.data,1,pkt.size,fp_audio); } av_free_packet(&pkt); } #if USE_H264BSF av_bitstream_filter_close(h264bsfc); #endif fclose(fp_video); fclose(fp_audio); avformat_close_input(&ifmt_ctx); if (ret < 0 && ret != AVERROR_EOF) { printf( "Error occurred.\n"); return -1; } return 0; }

結(jié)果

輸入文件為:
cuc_ieschool.flv:FLV封裝格式數(shù)據(jù)。

輸出文件為:
cuc_ieschool.h264:H.264視頻碼流數(shù)據(jù)。
cuc_ieschool.mp3:Mp3音頻碼流數(shù)據(jù)。

下載

simplest ffmpeg format

項(xiàng)目主頁(yè)

SourceForge:https://sourceforge.net/projects/simplestffmpegformat/

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_format

開(kāi)源中國(guó):http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_format

CSDN下載地址:

http://download.csdn.net/detail/leixiaohua1020/8005317

工程中包括4個(gè)樣例:

simplest_ffmpeg_demuxer_simple:視音頻分離器(簡(jiǎn)化版)。

simplest_ffmpeg_demuxer:視音頻分離器。

simplest_ffmpeg_muxer:視音頻復(fù)用器。

simplest_ffmpeg_remuxer:封裝格式轉(zhuǎn)換器。

更新-1.1==================================================

修復(fù)了以下問(wèn)題:
(1)Release版本號(hào)下的執(zhí)行問(wèn)題
(2)simplest_ffmpeg_muxer封裝H.264裸流的時(shí)候丟失聲音的錯(cuò)誤

CSDN下載

http://download.csdn.net/detail/leixiaohua1020/8284309

更新-1.2 (2015.2.13)=========================================

這次考慮到了跨平臺(tái)的要求,調(diào)整了源碼。經(jīng)過(guò)這次調(diào)整之后。源碼能夠在以下平臺(tái)編譯通過(guò):

VC++:打開(kāi)sln文件就可以編譯。無(wú)需配置。

cl.exe:打開(kāi)compile_cl.bat就可以命令行下使用cl.exe進(jìn)行編譯,注意可能須要依照VC的安裝路徑調(diào)整腳本里面的參數(shù)。編譯命令例如以下。

::VS2010 Environment
call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
::include
@set INCLUDE=include;%INCLUDE%
::lib
@set LIB=lib;%LIB%
::compile and link
cl simplest_ffmpeg_demuxer_simple.cpp /link avcodec.lib avformat.lib avutil.lib ^
avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib /OPT:NOREF

MinGW:MinGW命令行下執(zhí)行compile_mingw.sh就可以使用MinGW的g++進(jìn)行編譯。編譯命令例如以下。

g++ simplest_ffmpeg_demuxer_simple.cpp -g -o simplest_ffmpeg_demuxer_simple.exe \
-I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil

GCC:Linux或者M(jìn)acOS命令行下執(zhí)行compile_gcc.sh就可以使用GCC進(jìn)行編譯。編譯命令例如以下。

gcc simplest_ffmpeg_demuxer_simple.cpp -g -o simplest_ffmpeg_demuxer_simple.out \
-I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil

PS:相關(guān)的編譯命令已經(jīng)保存到了工程目錄中

CSDN下載地址:http://download.csdn.net/detail/leixiaohua1020/8445303

SourceForge它已被更新了。

版權(quán)聲明:本文博主原創(chuàng)文章,博客,未經(jīng)同意不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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