一、概述
實現了讀取mp4
文件,提取出h264
和aac
文件,可以直接播放
二、實現過程
準備文件
在build
路徑下添加mp4
文件
同時,添加main
函數參數,表示輸入文件和輸出文件
打開文件
- 打開輸入文件,初始化格式上下文
char *in_filename = argv[1];
ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL);if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("avformat_open_input failed:%d\n", ret);printf("avformat_open_input failed:%s\n", errors);avformat_close_input(&ifmt_ctx);// go failed;return -1;}
- 打開輸出文件
char *h264_filename = argv[2];
char *aac_filename = argv[3];
FILE *aac_fd = NULL;
FILE *h264_fd = NULL;h264_fd = fopen(h264_filename, "wb");
if(!h264_fd) {printf("fopen %s failed\n", h264_filename);return -1;
}aac_fd = fopen(aac_filename, "wb");
if(!aac_fd) {printf("fopen %s failed\n", aac_filename);return -1;
}
解封裝
- 對于
mp4
文件,不需要使用av_find_stream_info
這個函數,如果是flv
則需要,因為在mp4
的頭部信息中已經有足夠的信息拷貝到格式上下文了。 - 直接使用
avformat_find_best_stream
找音頻流和視頻流即可
video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if(video_index == -1) {printf("av_find_best_stream video_index failed\n");avformat_close_input(&ifmt_ctx);return -1;
}audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if(audio_index == -1) {printf("av_find_best_stream audio_index failed\n");avformat_close_input(&ifmt_ctx);return -1;
}
寫入頭部
對于mp4
文件,解碼出來的音頻和視頻數據包都不帶頭部,因此我們都需要手動寫入頭部
-
對于
h264
,使用比特流過濾器直接將mp4
模式轉為annexb
模式即可,它會自動寫入sps pps starCode
-
對于
aac
,手動寫入ADTS
即可,格式就是固定頭+可變頭,長度為7
個字節(不帶校驗) -
初始化比特流過濾器
AVBSFContext *bsf_ctx = NULL; // 對應面向對象的變量
ret = av_bsf_alloc(bsfilter, &bsf_ctx);
if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_alloc failed:%s\n", errors);avformat_close_input(&ifmt_ctx);return -1;
}
ret = avcodec_parameters_copy(bsf_ctx->par_in, ifmt_ctx->streams[video_index]->codecpar);
if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("avcodec_parameters_copy failed:%s\n", errors);avformat_close_input(&ifmt_ctx);av_bsf_free(&bsf_ctx);return -1;
}
ret = av_bsf_init(bsf_ctx);
if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_init failed:%s\n", errors);avformat_close_input(&ifmt_ctx);av_bsf_free(&bsf_ctx);return -1;
}
- 循環從格式上下文中解碼出
H264
包和AAC
包
ret = av_read_frame(ifmt_ctx, pkt); // 不會去釋放pkt的buf,如果我們外部不去釋放,就會出現內存泄露if(ret < 0 ) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_read_frame failed:%s\n", errors);break;}
根據packet
中的索引判斷是音頻包還是視頻包
- 如果是視頻包,先將數據包發送到比特流過濾器,然后再從比特流過濾器中拿出已經有頭部的
h264
數據包,將帶有頭部的數據包寫入文件 - 要使用循環來接收數據包,因為可能有多個數據包,根據不同返回值進行判斷接收情況
if(pkt->stream_index == video_index) {// 處理視頻ret = av_bsf_send_packet(bsf_ctx, pkt); // 內部把我們傳入的buf轉移到自己bsf內部if(ret < 0) { // 基本不會進入該邏輯av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_send_packet failed:%s\n", errors);av_packet_unref(pkt);continue;}while (1) {ret = av_bsf_receive_packet(bsf_ctx, pkt);if(ret != 0) {break;}size_t size = fwrite(pkt->data, 1, pkt->size, h264_fd);if(size != pkt->size){av_log(NULL, AV_LOG_DEBUG, "h264 warning, length of writed data isn't equal pkt->size(%d, %d)\n",size,pkt->size);}av_packet_unref(pkt);}
}
- 如果是音頻包,則寫入
ADTS
,使用的是MPEG-2
的標準,因此這里的profile
不需要改變
else if(pkt->stream_index == audio_index) {// 處理音頻char adts_header_buf[7] = {0};adts_header(adts_header_buf, pkt->size,ifmt_ctx->streams[audio_index]->codecpar->profile,ifmt_ctx->streams[audio_index]->codecpar->sample_rate,ifmt_ctx->streams[audio_index]->codecpar->channels);fwrite(adts_header_buf, 1, 7, aac_fd); // 寫adts header , ts流不適用,ts流分離出來的packet帶了adts headersize_t size = fwrite( pkt->data, 1, pkt->size, aac_fd); // 寫adts dataif(size != pkt->size){av_log(NULL, AV_LOG_DEBUG, "aac warning, length of writed data isn't equal pkt->size(%d, %d)\n",size,pkt->size);}av_packet_unref(pkt);
}
adts_header
函數
int adts_header(char * const p_adts_header, const int data_length,const int profile, const int samplerate,const int channels)
{int sampling_frequency_index = 3; // 默認使用48000hzint adtsLen = data_length + 7;int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);int i = 0;for(i = 0; i < frequencies_size; i++){if(sampling_frequencies[i] == samplerate){sampling_frequency_index = i;break;}}if(i >= frequencies_size){printf("unsupport samplerate:%d\n", samplerate);return -1;}p_adts_header[0] = 0xff; //syncword:0xfff 高8bitsp_adts_header[1] = 0xf0; //syncword:0xfff 低4bitsp_adts_header[1] |= (1 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bitp_adts_header[1] |= (0 << 1); //Layer:0 2bitsp_adts_header[1] |= 1; //protection absent:1 1bitp_adts_header[2] = (profile)<<6; //profile:profile 2bitsp_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index 4bitsp_adts_header[2] |= (0 << 1); //private bit:0 1bitp_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels 高1bitp_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bitsp_adts_header[3] |= (0 << 5); //original:0 1bitp_adts_header[3] |= (0 << 4); //home:0 1bitp_adts_header[3] |= (0 << 3); //copyright id bit:0 1bitp_adts_header[3] |= (0 << 2); //copyright id start:0 1bitp_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bitsp_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中間8bitsp_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bitsp_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bitsp_adts_header[6] = 0xfc; //11111100 //buffer fullness:0x7ff 低6bits// number_of_raw_data_blocks_in_frame:// 表示ADTS幀中有number_of_raw_data_blocks_in_frame + 1個AAC原始幀。return 0;
}
釋放內存與關閉文件
- 釋放格式上下文、數據包內存
if(ifmt_ctx)avformat_close_input(&ifmt_ctx);
if(pkt)av_packet_free(&pkt);
- 關閉輸出文件
if(h264_fd) {fclose(h264_fd);
}
if(aac_fd) {fclose(aac_fd);
}
完整代碼
main.c
#include <stdio.h>#include "libavutil/log.h"
#include "libavformat/avformat.h"#define ERROR_STRING_SIZE 1024#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_frequency_index = 3; // 默認使用48000hzint adtsLen = data_length + 7;int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);int i = 0;for(i = 0; i < frequencies_size; i++){if(sampling_frequencies[i] == samplerate){sampling_frequency_index = i;break;}}if(i >= frequencies_size){printf("unsupport samplerate:%d\n", samplerate);return -1;}p_adts_header[0] = 0xff; //syncword:0xfff 高8bitsp_adts_header[1] = 0xf0; //syncword:0xfff 低4bitsp_adts_header[1] |= (1 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bitp_adts_header[1] |= (0 << 1); //Layer:0 2bitsp_adts_header[1] |= 1; //protection absent:1 1bitp_adts_header[2] = (profile)<<6; //profile:profile 2bitsp_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index 4bitsp_adts_header[2] |= (0 << 1); //private bit:0 1bitp_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels 高1bitp_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bitsp_adts_header[3] |= (0 << 5); //original:0 1bitp_adts_header[3] |= (0 << 4); //home:0 1bitp_adts_header[3] |= (0 << 3); //copyright id bit:0 1bitp_adts_header[3] |= (0 << 2); //copyright id start:0 1bitp_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bitsp_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中間8bitsp_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bitsp_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bitsp_adts_header[6] = 0xfc; //11111100 //buffer fullness:0x7ff 低6bits// number_of_raw_data_blocks_in_frame:// 表示ADTS幀中有number_of_raw_data_blocks_in_frame + 1個AAC原始幀。return 0;
}// 程序本身 input.mp4 out.h264 out.aac
int main(int argc, char **argv)
{// 判斷參數if(argc != 4) {printf("usage app input.mp4 out.h264 out.aac");return -1;}char *in_filename = argv[1];char *h264_filename = argv[2];char *aac_filename = argv[3];FILE *aac_fd = NULL;FILE *h264_fd = NULL;h264_fd = fopen(h264_filename, "wb");if(!h264_fd) {printf("fopen %s failed\n", h264_filename);return -1;}aac_fd = fopen(aac_filename, "wb");if(!aac_fd) {printf("fopen %s failed\n", aac_filename);return -1;}AVFormatContext *ifmt_ctx = NULL;int video_index = -1;int audio_index = -1;AVPacket *pkt = NULL;int ret = 0;char errors[ERROR_STRING_SIZE+1]; // 主要是用來緩存解析FFmpeg api返回值的錯誤stringifmt_ctx = avformat_alloc_context();if(!ifmt_ctx) {printf("avformat_alloc_context failed\n");// fclose(aac_fd);return -1;}ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL);if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("avformat_open_input failed:%d\n", ret);printf("avformat_open_input failed:%s\n", errors);avformat_close_input(&ifmt_ctx);// go failed;return -1;}video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);if(video_index == -1) {printf("av_find_best_stream video_index failed\n");avformat_close_input(&ifmt_ctx);return -1;}audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);if(audio_index == -1) {printf("av_find_best_stream audio_index failed\n");avformat_close_input(&ifmt_ctx);return -1;}// h264_mp4toannexbconst AVBitStreamFilter *bsfilter = av_bsf_get_by_name("h264_mp4toannexb"); // 對應面向對象的方法if(!bsfilter) {avformat_close_input(&ifmt_ctx);printf("av_bsf_get_by_name h264_mp4toannexb failed\n");return -1;}AVBSFContext *bsf_ctx = NULL; // 對應面向對象的變量ret = av_bsf_alloc(bsfilter, &bsf_ctx);if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_alloc failed:%s\n", errors);avformat_close_input(&ifmt_ctx);return -1;}ret = avcodec_parameters_copy(bsf_ctx->par_in, ifmt_ctx->streams[video_index]->codecpar);if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("avcodec_parameters_copy failed:%s\n", errors);avformat_close_input(&ifmt_ctx);av_bsf_free(&bsf_ctx);return -1;}ret = av_bsf_init(bsf_ctx);if(ret < 0) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_init failed:%s\n", errors);avformat_close_input(&ifmt_ctx);av_bsf_free(&bsf_ctx);return -1;}pkt = av_packet_alloc();av_init_packet(pkt);while (1) {ret = av_read_frame(ifmt_ctx, pkt); // 不會去釋放pkt的buf,如果我們外部不去釋放,就會出現內存泄露if(ret < 0 ) {av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_read_frame failed:%s\n", errors);break;}// av_read_frame 成功讀取到packet,則外部需要進行buf釋放if(pkt->stream_index == video_index) {// 處理視頻ret = av_bsf_send_packet(bsf_ctx, pkt); // 內部把我們傳入的buf轉移到自己bsf內部if(ret < 0) { // 基本不會進入該邏輯av_strerror(ret, errors, ERROR_STRING_SIZE);printf("av_bsf_send_packet failed:%s\n", errors);av_packet_unref(pkt);continue;}
// av_packet_unref(pkt); // 這里不需要去釋放內存while (1) {ret = av_bsf_receive_packet(bsf_ctx, pkt);if(ret != 0) {break;}size_t size = fwrite(pkt->data, 1, pkt->size, h264_fd);if(size != pkt->size){av_log(NULL, AV_LOG_DEBUG, "h264 warning, length of writed data isn't equal pkt->size(%d, %d)\n",size,pkt->size);}av_packet_unref(pkt);}} else if(pkt->stream_index == audio_index) {// 處理音頻char adts_header_buf[7] = {0};adts_header(adts_header_buf, pkt->size,ifmt_ctx->streams[audio_index]->codecpar->profile,ifmt_ctx->streams[audio_index]->codecpar->sample_rate,ifmt_ctx->streams[audio_index]->codecpar->channels);fwrite(adts_header_buf, 1, 7, aac_fd); // 寫adts header , ts流不適用,ts流分離出來的packet帶了adts headersize_t size = fwrite( pkt->data, 1, pkt->size, aac_fd); // 寫adts dataif(size != pkt->size){av_log(NULL, AV_LOG_DEBUG, "aac warning, length of writed data isn't equal pkt->size(%d, %d)\n",size,pkt->size);}av_packet_unref(pkt);} else {av_packet_unref(pkt); // 釋放buffer}}printf("while finish\n");
failed:if(h264_fd) {fclose(h264_fd);}if(aac_fd) {fclose(aac_fd);}if(pkt)av_packet_free(&pkt);if(ifmt_ctx)avformat_close_input(&ifmt_ctx);printf("Hello World!\n");return 0;
}
更多資料:https://github.com/0voice