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

最简单的基于FFmpeg的封装格式处理系列文章列表:

最简单的基于FFmpeg的封装格式处理:视音频分离器简化版(demuxer-simple)

最简单的基于FFmpeg的封装格式处理:视音频分离器(demuxer)

最简单的基于FFmpeg的封装格式处理:视音频复用器(muxer)

最简单的基于FFMPEG的封装格式处理:封装格式转换(remuxer)

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

简介

打算记录一下基于FFmpeg的封装格式处理方面的例子。包括了视音频分离,复用,封装格式转换。这是第2篇。

本文记录一个基于FFmpeg的视音频分离器(Simplest FFmpeg demuxer)。视音频分离器(Demuxer)即是将封装格式数据(例如MKV)中的视频压缩数据(例如H.264)和音频压缩数据(例如AAC)分离开。如图所示。在这个过程中并不涉及到编码和解码。

本文记录的程序可以将一个MPEG2TS封装的视频文件(其中视频编码为H.264,音频编码为AAC)分离成为两个文件:一个H.264编码的视频码流文件,一个AAC编码的音频码流文件。
前一篇文章中,记录一个简单版的视音频分离器。相比于前一篇文中的分离器,本篇文章记录的分离器复杂了很多。相比于简单版的分离器,学习的难度大了一些。但是该分离器可以很好地处理FFmpeg支持的各种格式(例如分离AAC音频流),拥有更好的实用性。

流程图

程序的流程如下图所示。从流程图中可以看出,一共初始化了3个AVFormatContext,其中1个用于输入,另外2个分别用于视频输出和音频输出。3个AVFormatContext初始化之后,通过avcodec_copy_context()函数可以将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体。最后,通过av_read_frame()获取AVPacket,根据AVPacket类型的不同,分别使用av_interleaved_write_frame()写入不同的输出文件中即可。

单击查看更清晰的图片

PS:对于某些封装格式(例如MP4/FLV/MKV等)中的H.264,需要用到名称为“h264_mp4toannexb”的bitstream filter。这一点在前一篇文章《最简单的基于FFmpeg的封装格式处理:视音频分离器简化版(demuxer-simple)》中,已经有过详细叙述,这里不再重复。

简单介绍一下流程中各个重要函数的意义:

avformat_open_input():打开输入文件。
avcodec_copy_context():赋值AVCodecContext的参数。
avformat_alloc_output_context2():初始化输出文件。
avio_open():打开输出文件。
avformat_write_header():写入文件头。
av_read_frame():从输入文件读取一个AVPacket。
av_interleaved_write_frame():写入一个AVPacket到输出文件。
av_write_trailer():写入文件尾。

代码

下面贴上代码:

/*** 最简单的基于FFmpeg的视音频分离器* Simplest FFmpeg Demuxer** 雷霄骅 Lei Xiaohua* leixiaohua1020@126.com* 中国传媒大学/数字电视技术* Communication University of China / Digital TV Technology* http://blog.csdn.net/leixiaohua1020** 本程序可以将封装格式中的视频码流数据和音频码流数据分离出来。* 在该例子中, 将MPEG2TS的文件分离得到H.264视频码流文件和AAC* 音频码流文件。** This software split a media file (in Container such as * MKV, FLV, AVI...) to video and audio bitstream.* In this example, it demux a MPEG2TS file to H.264 bitstream* and AAC bitstream.*/#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/*
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.
*/
//'1': Use H.264 Bitstream Filter
#define USE_H264BSF 0int main(int argc, char* argv[])
{AVOutputFormat *ofmt_a = NULL,*ofmt_v = NULL;//(Input AVFormatContext and Output AVFormatContext)AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx_a = NULL, *ofmt_ctx_v = NULL;AVPacket pkt;int ret, i;int videoindex=-1,audioindex=-1;int frame_index=0;const char *in_filename  = "cuc_ieschool.ts";//Input file URL//char *in_filename  = "cuc_ieschool.mkv";const char *out_filename_v = "cuc_ieschool.h264";//Output file URL//char *out_filename_a = "cuc_ieschool.mp3";const char *out_filename_a = "cuc_ieschool.aac";av_register_all();//Inputif ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {printf( "Could not open input file.");goto end;}if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {printf( "Failed to retrieve input stream information");goto end;}//Outputavformat_alloc_output_context2(&ofmt_ctx_v, NULL, NULL, out_filename_v);if (!ofmt_ctx_v) {printf( "Could not create output context\n");ret = AVERROR_UNKNOWN;goto end;}ofmt_v = ofmt_ctx_v->oformat;avformat_alloc_output_context2(&ofmt_ctx_a, NULL, NULL, out_filename_a);if (!ofmt_ctx_a) {printf( "Could not create output context\n");ret = AVERROR_UNKNOWN;goto end;}ofmt_a = ofmt_ctx_a->oformat;for (i = 0; i < ifmt_ctx->nb_streams; i++) {//Create output AVStream according to input AVStreamAVFormatContext *ofmt_ctx;AVStream *in_stream = ifmt_ctx->streams[i];AVStream *out_stream = NULL;if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){videoindex=i;out_stream=avformat_new_stream(ofmt_ctx_v, in_stream->codec->codec);ofmt_ctx=ofmt_ctx_v;}else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){audioindex=i;out_stream=avformat_new_stream(ofmt_ctx_a, in_stream->codec->codec);ofmt_ctx=ofmt_ctx_a;}else{break;}if (!out_stream) {printf( "Failed allocating output stream\n");ret = AVERROR_UNKNOWN;goto end;}//Copy the settings of AVCodecContextif (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {printf( "Failed to copy context from input to output stream codec context\n");goto end;}out_stream->codec->codec_tag = 0;if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;}//Dump Format------------------printf("\n==============Input Video=============\n");av_dump_format(ifmt_ctx, 0, in_filename, 0);printf("\n==============Output Video============\n");av_dump_format(ofmt_ctx_v, 0, out_filename_v, 1);printf("\n==============Output Audio============\n");av_dump_format(ofmt_ctx_a, 0, out_filename_a, 1);printf("\n======================================\n");//Open output fileif (!(ofmt_v->flags & AVFMT_NOFILE)) {if (avio_open(&ofmt_ctx_v->pb, out_filename_v, AVIO_FLAG_WRITE) < 0) {printf( "Could not open output file '%s'", out_filename_v);goto end;}}if (!(ofmt_a->flags & AVFMT_NOFILE)) {if (avio_open(&ofmt_ctx_a->pb, out_filename_a, AVIO_FLAG_WRITE) < 0) {printf( "Could not open output file '%s'", out_filename_a);goto end;}}//Write file headerif (avformat_write_header(ofmt_ctx_v, NULL) < 0) {printf( "Error occurred when opening video output file\n");goto end;}if (avformat_write_header(ofmt_ctx_a, NULL) < 0) {printf( "Error occurred when opening audio output file\n");goto end;}#if USE_H264BSFAVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb");
#endifwhile (1) {AVFormatContext *ofmt_ctx;AVStream *in_stream, *out_stream;//Get an AVPacketif (av_read_frame(ifmt_ctx, &pkt) < 0)break;in_stream  = ifmt_ctx->streams[pkt.stream_index];if(pkt.stream_index==videoindex){out_stream = ofmt_ctx_v->streams[0];ofmt_ctx=ofmt_ctx_v;printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
#if USE_H264BSFav_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
#endif}else if(pkt.stream_index==audioindex){out_stream = ofmt_ctx_a->streams[0];ofmt_ctx=ofmt_ctx_a;printf("Write Audio Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);}else{continue;}//Convert PTS/DTSpkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);pkt.pos = -1;pkt.stream_index=0;//Writeif (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {printf( "Error muxing packet\n");break;}//printf("Write %8d frames to output file\n",frame_index);av_free_packet(&pkt);frame_index++;}#if USE_H264BSFav_bitstream_filter_close(h264bsfc);
#endif//Write file trailerav_write_trailer(ofmt_ctx_a);av_write_trailer(ofmt_ctx_v);
end:avformat_close_input(&ifmt_ctx);/* close output */if (ofmt_ctx_a && !(ofmt_a->flags & AVFMT_NOFILE))avio_close(ofmt_ctx_a->pb);if (ofmt_ctx_v && !(ofmt_v->flags & AVFMT_NOFILE))avio_close(ofmt_ctx_v->pb);avformat_free_context(ofmt_ctx_a);avformat_free_context(ofmt_ctx_v);if (ret < 0 && ret != AVERROR_EOF) {printf( "Error occurred.\n");return -1;}return 0;
}

结果

输入文件为:
cuc_ieschool.ts:MPEG2TS封装格式数据。

输出文件为:
cuc_ieschool.h264:H.264视频码流数据。
cuc_ieschool.aac:AAC音频码流数据。

下载

simplest ffmpeg format

项目主页

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

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

开源中国:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_format

CSDN下载地址:
http://download.csdn.net/detail/leixiaohua1020/8005317

工程中包含4个例子:

simplest_ffmpeg_demuxer_simple:视音频分离器(简化版)。

simplest_ffmpeg_demuxer:视音频分离器。

simplest_ffmpeg_muxer:视音频复用器。

simplest_ffmpeg_remuxer:封装格式转换器。

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

修复了以下问题:
(1)Release版本下的运行问题
(2)simplest_ffmpeg_muxer分装H.264裸流的时候丢失声音的错误

CSDN下载地址:

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

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

这次考虑到了跨平台的要求,调整了源代码。经过这次调整之后,源代码可以在以下平台编译通过:

VC++:打开sln文件即可编译,无需配置。

cl.exe:打开compile_cl.bat即可命令行下使用cl.exe进行编译,注意可能需要按照VC的安装路径调整脚本里面的参数。编译命令如下。

::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.cpp /link avcodec.lib avformat.lib avutil.lib ^
avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib /OPT:NOREF

MinGW:MinGW命令行下运行compile_mingw.sh即可使用MinGW的g++进行编译。编译命令如下。

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

GCC:Linux或者MacOS命令行下运行compile_gcc.sh即可使用GCC进行编译。编译命令如下。

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

PS:相关的编译命令已经保存到了工程文件夹中

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

SourceForge上已经更新。

最简单的基于FFmpeg的封装格式处理:视音频分离器(demuxer)相关推荐

  1. 最简单的基于FFMPEG的封装格式转换器(无编解码)

    2019独角兽企业重金招聘Python工程师标准>>> 本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(对应. ...

  2. 最简单的基于FFmpeg的封装格式处理:视音频复用器(muxer)

    ===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...

  3. 最简单的基于FFMPEG的封装格式转换器(C++Qt 版)

    最简单的基于FFMPEG的封装格式转换器(C++Qt 版) 这篇博客是我上篇博客的延续.建议大家先看看我上篇博客: https://blog.csdn.net/liyuanbhu/article/de ...

  4. 最简单的基于FFMPEG的封装格式转换器

    最简单的基于FFMPEG的封装格式转换器(无编解码) https://blog.csdn.net/leixiaohua1020/article/details/25422685 =========== ...

  5. 基于FFmpeg的封装格式MP4(TS)

    一. 封装MP4原理: 每一帧音频或视频都有一个持续时间:duration: 采样频率是指将模拟声音波形进行数字化时,每秒钟抽取声波幅度样本的次数. .正常人听觉的频率范围大约在20Hz~20kHz之 ...

  6. 基于FFMPEG的封装格式转换器

    简介 本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(对应.avi,.flv,.mkv,.mp4文件).需要注意的是,本程序并不 ...

  7. 最简单的基于FFmpeg的移动端例子:Android 推流器

    ===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...

  8. 【开源项目】基于FFmpeg的封装格式转换

    /* * 一笑奈何 * cn-yixiaonaihe.blog.csdn.net */#include <iostream> #include <thread> extern ...

  9. 最简单的基于FFmpeg的移动端例子:IOS 推流器

    https://blog.csdn.net/leixiaohua1020/article/details/47072519 http://www.cocoachina.com/articles/118 ...

最新文章

  1. 贪心:expedition 最优加油方法
  2. 每日一九度之 题目1083:特殊乘法
  3. C# 2.0中泛型编程初级入门教程
  4. 使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构
  5. request获取url的参数编码问题
  6. Android使用BaseAdapter绑定ListView实现不同item的TextView多种文字变色
  7. 阿里大数据运维新成员——24小时待命!有求必应!能说会做!...
  8. itchat使用教程
  9. 体系结构学习11-VLIW处理器
  10. 黑客组织“洋葱狗”潜伏3年终曝光 定期偷袭能源及交通行业
  11. 对于女生来说,软件测试和前端,学哪一个更好啊
  12. Redis基础篇(万丈高楼平地起):核心底层数据结构
  13. n*m的格子中正方形个数和长方形个数
  14. Mybatis从入门到精通(全)
  15. 【转】地心历险记 2:神秘岛 迅雷 下载 地址|神秘岛 高清 下载地址
  16. PTA数据库题目集第一章
  17. 矢量点积与矢量叉乘的微分
  18. diy nas配置推荐2019_Server2019+htpc+NAS搭建家庭数据中心+远程唤醒开关机+晒晒桌面...
  19. numpy.ndarray类型方法
  20. 英伟达两个最新元宇宙布局

热门文章

  1. [恢]hdu 2042
  2. MooTools官方插件 - Chain.Wait
  3. Visual Studio 2010 美女与程序员的爱情网剧+《耀和你一起》壁纸包
  4. UVA11876 N + NOD (N)【欧拉筛法+前缀和】
  5. 2018中国大学生程序设计竞赛-网络选拔赛题解
  6. JSK-12 最后一个单词的长度【入门】
  7. HDU1233 还是畅通工程【Kruskal算法+并查集】
  8. 计算机科学的知识领域
  9. 平常学习与实际应用场景
  10. C/C++ —— 字典(dictionary)接口的设计