开发环境
操作系统:win10
IDE:vs2019
第三方库:ffmpeg5
代码
/**
* 最简单的基于FFmpeg的视音频分离器(简化版)
* Simplest FFmpeg Demuxer Simple
*
* 雷霄骅 Lei Xiaohua
* leixiaohua1020@126.com
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
* 本程序可以将封装格式中的视频码流数据和音频码流数据分离出来。
* 在该例子中, 将FLV的文件分离得到H.264视频码流文件和MP3
* 音频码流文件。
*
* 注意:
* 这个是简化版的视音频分离器。与原版的不同在于,没有初始化输出
* 视频流和音频流的AVFormatContext。而是直接将解码后的得到的
* AVPacket中的的数据通过fwrite()写入文件。这样做的好处是流程比
* 较简单。坏处是对一些格式的视音频码流是不适用的,比如说
* FLV/MP4/MKV等格式中的AAC码流(上述封装格式中的AAC的AVPacket中
* 的数据缺失了7字节的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>
#include <cstdint>
#define ADTS_HEADER__LEN 7
const int sampling_frequencies[] = {
96000, // 0x0
88200, // 0x1
64000, // 0x2
48000, // 0x3
44100, // 0x4
32000, // 0x5
24000, // 0x6
22050, // 0x7
16000, // 0x8
12000, // 0x9
11025, // 0xa
8000 // 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; // 默认使用48000hz
int 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 高8bits
p_adts_header[1] = 0xf0; //syncword:0xfff 低4bits
p_adts_header[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit
p_adts_header[1] |= (0 << 1); //Layer:0 2bits
p_adts_header[1] |= 1; //protection absent:1 1bit
p_adts_header[2] = (profile) << 6; //profile:profile 2bits
p_adts_header[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index 4bits
p_adts_header[2] |= (0 << 1); //private bit:0 1bit
p_adts_header[2] |= (channels & 0x04) >> 2; //channel configuration:channels 高1bit
p_adts_header[3] = (channels & 0x03) << 6; //channel configuration:channels 低2bits
p_adts_header[3] |= (0 << 5); //original:0 1bit
p_adts_header[3] |= (0 << 4); //home:0 1bit
p_adts_header[3] |= (0 << 3); //copyright id bit:0 1bit
p_adts_header[3] |= (0 << 2); //copyright id start:0 1bit
p_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bits
p_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bits
p_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bits
p_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bits
p_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;
}
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/bsf.h>
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif
int main(int argc, char* argv[])
{
AVFormatContext* ifmt_ctx = NULL;
AVPacket* pkt;
int ret, i;
int videoindex = -1, audioindex = -1;
const char* in_filename = "../resources/gx.flv";//Input file URL
const char* out_filename_v = "cuc_ieschool.h264";//Output file URL
const char* out_filename_a = "cuc_ieschool.mp3";
//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]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoindex = i;
}
else if (ifmt_ctx->streams[i]->codecpar->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_s(&fp_audio, out_filename_a, "wb+");
FILE* fp_video;
fopen_s(&fp_video, 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.
*/
AVBSFContext* bsf_ctx;
const AVBitStreamFilter* filter = av_bsf_get_by_name("h264_mp4toannexb");
if (!filter)
{
av_log(NULL, AV_LOG_ERROR, "Unkonw bitstream filter");
return -1;
}
ret = av_bsf_alloc(filter, &bsf_ctx);
// 3 添加解码器属性
avcodec_parameters_copy(bsf_ctx->par_in, ifmt_ctx->streams[videoindex]->codecpar);
av_bsf_init(bsf_ctx);
pkt = av_packet_alloc();
while (av_read_frame(ifmt_ctx, pkt) >= 0) {
if (pkt->stream_index == videoindex) {
ret = av_bsf_send_packet(bsf_ctx, pkt);
if (ret < 0)
{
break;
}
av_packet_unref(pkt);
while (av_bsf_receive_packet(bsf_ctx, pkt) == 0)
{
printf("Write Video Packet. size:%d\tpts:%lld\n", pkt->size, pkt->pts);
fwrite(pkt->data, 1, pkt->size, fp_video);
av_packet_unref(pkt);
}
}
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.
*/
char adts_header_buf[7] = { 0 };
adts_header(adts_header_buf, pkt->size,
ifmt_ctx->streams[audioindex]->codecpar->profile,
ifmt_ctx->streams[audioindex]->codecpar->sample_rate,
ifmt_ctx->streams[audioindex]->codecpar->ch_layout.nb_channels);
fwrite(adts_header_buf, 1, 7, fp_audio);
printf("Write Audio Packet. size:%d\tpts:%lld\n", pkt->size, pkt->pts);
fwrite(pkt->data, 1, pkt->size, fp_audio);
}
av_packet_unref(pkt);
}
av_packet_free(&pkt);
av_bsf_free(&bsf_ctx);
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;
}
注意事项
视频
分离某些封装格式(例如MP4/FLV/MKV等)中的H.264的时候,需要首先写入SPS和PPS,否则会导致分离出来的数据没有SPS、PPS而无法播放。H.264码流的SPS和PPS信息存储在AVCodecContext结构体的extradata中。需要使用ffmpeg中名称为“h264_mp4toannexb”的bitstream filter处理
音频
音频需要添加adts头才能播放