FFmpeg封装格式处理:视音频复用器(muxer)
打算记录一下基于FFmpeg的封装格式处理方面的例子。包括了视音频分离,复用,封装格式转换。这是第3篇。 本文记录一个基于FFmpeg的视音频复用器(Simplest FFmpeg muxer)。视音频复用器(Muxer)即是将视频压缩数据(例如H.264)和音频压缩数据(例如AAC)合
打算记录一下基于FFmpeg的封装格式处理方面的例子。包括了视音频分离,复用,封装格式转换。这是第3篇。本文记录一个基于FFmpeg的视音频复用器(Simplest FFmpeg muxer)。视音频复用器(Muxer)即是将视频压缩数据(例如H.264)和音频压缩数据(例如AAC)合并到一个封装格式数据(例如MKV)中去。如图所示。在这个过程中并不涉及到编码和解码。

本文记录的程序将一个H.264编码的视频码流文件和一个MP3编码的音频码流文件,合成为一个MP4封装格式的文件。
,一共初始化了3个AVFormatContext,其中2个用于输入,1个用于输出。3个AVFormatContext初始化之后,通过avcodec_copy_context()函数可以将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体。然后分别调用视频输入流和音频输入流的av_read_frame(),从视频输入流中取出视频的AVPacket,音频输入流中取出音频的AVPacket,分别将取出的AVPacket写入到输出文件中即可。其间用到了一个不太常见的函数av_compare_ts(),是比较时间戳用的。通过该函数可以决定该写入视频还是音频。
本文介绍的视音频复用器,输入的视频不一定是H.264裸流文件,音频也不一定是纯音频文件。可以选择两个封装过的视音频文件作为输入。程序会从视频输入文件中“挑”出视频流,音频输入文件中“挑”出音频流,再将“挑选”出来的视音频流复用起来。 PS1:对于某些封装格式(例如MP4/FLV/MKV等)中的H.264,需要用到名称为“h264_mp4toannexb”的bitstream filter。
PS2:对于某些封装格式(例如MP4/FLV/MKV等)中的AAC,需要用到名称为“aac_adtstoasc”的bitstream filter。
简单介绍一下流程中各个重要函数的意义:
avformat_open_input():打开输入文件。avcodec_copy_context():赋值AVCodecContext的参数。
avformat_alloc_output_context2():初始化输出文件。
avio_open():打开输出文件。
avformat_write_header():写入文件头。
av_compare_ts():比较时间戳,决定写入视频还是写入音频。这个函数相对要少见一些。
av_read_frame():从输入文件读取一个AVPacket。
av_interleaved_write_frame():写入一个AVPacket到输出文件。
av_write_trailer():写入文件尾。
代码
下面贴上代码: <strong>[cpp]</strong> view plaincopy
-
/**
Copy after login -
* 最简单的基于FFmpeg的视音频复用器
Copy after login -
* Simplest FFmpeg Muxer
Copy after login -
*
Copy after loginCopy after loginCopy after loginCopy after login -
* 雷霄骅 Lei Xiaohua
Copy after login -
* leixiaohua1020@126.com
Copy after login -
* 中国传媒大学/数字电视技术
Copy after login -
* Communication University of China / Digital TV Technology
Copy after login -
* http://blog.csdn.net/leixiaohua1020
Copy after login -
*
Copy after loginCopy after loginCopy after loginCopy after login -
* 本程序可以将视频码流和音频码流打包到一种封装格式中。
Copy after login -
* 程序中将MP3编码的音频码流和H.264编码(MPEG2TS封装中)的视频码流打包成
Copy after login -
* MP4封装格式的文件。
Copy after login -
* 需要注意的是本程序并不改变视音频的编码格式。
Copy after login -
*
Copy after loginCopy after loginCopy after loginCopy after login -
* This software mux a video bitstream and a audio bitstream
Copy after login -
* together into a file.
Copy after login -
* In this example, it mux a H.264 bitstream (in MPEG2TS) and
Copy after login -
* a MP3 bitstream file together into MP4 format file.
Copy after login -
*
Copy after loginCopy after loginCopy after loginCopy after login -
*/
Copy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
#include <stdio.h></stdio.h>
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
extern "C"
Copy after login -
{
Copy after loginCopy after login -
#include "libavformat/avformat.h"
Copy after login -
};
Copy after login -
/*
Copy after loginCopy after login -
FIX: H.264 in some container format (FLV, MP4, MKV etc.) need
Copy after login -
"h264_mp4toannexb" bitstream filter (BSF)
Copy after login -
*Add SPS,PPS in front of IDR frame
Copy after login -
*Add start code ("0,0,0,1") in front of NALU
Copy after login -
H.264 in some container (MPEG2TS) don't need this BSF.
Copy after login -
*/
Copy after loginCopy after loginCopy after login -
//'1': Use H.264 Bitstream Filter
Copy after login -
#define USE_H264BSF 0
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
/*
Copy after loginCopy after login -
FIX:AAC in some container format (FLV, MP4, MKV etc.) need
Copy after login -
"aac_adtstoasc" bitstream filter (BSF)
Copy after login -
*/
Copy after loginCopy after loginCopy after login -
//'1': Use AAC Bitstream Filter
Copy after login -
#define USE_AACBSF 0
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
int main(int argc, char* argv[])
Copy after login -
{
Copy after loginCopy after login -
AVOutputFormat *ofmt = NULL;
Copy after login -
//输入对应一个AVFormatContext,输出对应一个AVFormatContext
Copy after login -
//(Input AVFormatContext and Output AVFormatContext)
Copy after login -
AVFormatContext *ifmt_ctx_v = NULL, *ifmt_ctx_a = NULL,*ofmt_ctx = NULL;
Copy after login -
AVPacket pkt;
Copy after login -
int ret, i;
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
char *in_filename_v = "cuc_ieschool.ts";//输入文件名(Input file URL)
Copy after login -
//char *in_filename_v = "cuc_ieschool.h264";
Copy after login -
//char *in_filename_a = "cuc_ieschool.mp3";
Copy after login -
//char *in_filename_a = "gowest.m4a";
Copy after login -
//char *in_filename_a = "gowest.aac";
Copy after login -
char *in_filename_a = "huoyuanjia.mp3";
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
char *out_filename = "cuc_ieschool.mp4";//输出文件名(Output file URL)
Copy after login -
av_register_all();
Copy after login -
//输入(Input)
Copy after login -
if ((ret = avformat_open_input(&ifmt_ctx_v, in_filename_v, 0, 0))
Copy after login -
printf( "Could not open input file.");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
if ((ret = avformat_find_stream_info(ifmt_ctx_v, 0))
Copy after login -
printf( "Failed to retrieve input stream information");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
if ((ret = avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0))
Copy after login -
printf( "Could not open input file.");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
if ((ret = avformat_find_stream_info(ifmt_ctx_a, 0))
Copy after login -
printf( "Failed to retrieve input stream information");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
printf("Input Information=====================\n");
Copy after login -
av_dump_format(ifmt_ctx_v, 0, in_filename_v, 0);
Copy after login -
av_dump_format(ifmt_ctx_a, 0, in_filename_a, 0);
Copy after login -
printf("======================================\n");
Copy after loginCopy after login -
//输出(Output)
Copy after login -
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
Copy after login -
if (!ofmt_ctx) {
Copy after login -
printf( "Could not create output context\n");
Copy after login -
ret = AVERROR_UNKNOWN;
Copy after loginCopy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
ofmt = ofmt_ctx->oformat;
Copy after login -
int videoindex_v=-1,videoindex_out=-1;
Copy after login -
for (i = 0; i nb_streams; i++) {
Copy after loginCopy after login -
//根据输入流创建输出流(Create output AVStream according to input AVStream)
Copy after loginCopy after login -
if(ifmt_ctx_v->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
Copy after login -
videoindex_v=i;
Copy after login -
AVStream *in_stream = ifmt_ctx_v->streams[i];
Copy after login -
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
Copy after loginCopy after login -
if (!out_stream) {
Copy after loginCopy after login -
printf( "Failed allocating output stream\n");
Copy after loginCopy after login -
ret = AVERROR_UNKNOWN;
Copy after loginCopy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
videoindex_out=out_stream->index;
Copy after login -
//复制AVCodecContext的设置(Copy the settings of AVCodecContext)
Copy after loginCopy after login -
if (avcodec_copy_context(out_stream->codec, in_stream->codec)
Copy after loginCopy after login -
printf( "Failed to copy context from input to output stream codec context\n");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
out_stream->codec->codec_tag = 0;
Copy after loginCopy after login -
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
Copy after loginCopy after login -
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
Copy after loginCopy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
int audioindex_a=-1,audioindex_out=-1;
Copy after login -
for (i = 0; i nb_streams; i++) {
Copy after loginCopy after login -
//根据输入流创建输出流(Create output AVStream according to input AVStream)
Copy after loginCopy after login -
if(ifmt_ctx_a->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
Copy after login -
audioindex_a=i;
Copy after login -
AVStream *in_stream = ifmt_ctx_a->streams[i];
Copy after login -
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
Copy after loginCopy after login -
if (!out_stream) {
Copy after loginCopy after login -
printf( "Failed allocating output stream\n");
Copy after loginCopy after login -
ret = AVERROR_UNKNOWN;
Copy after loginCopy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
audioindex_out=out_stream->index;
Copy after login -
//复制AVCodecContext的设置(Copy the settings of AVCodecContext)
Copy after loginCopy after login -
if (avcodec_copy_context(out_stream->codec, in_stream->codec)
Copy after loginCopy after login -
printf( "Failed to copy context from input to output stream codec context\n");
Copy after loginCopy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
out_stream->codec->codec_tag = 0;
Copy after loginCopy after login -
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
Copy after loginCopy after login -
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
Copy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
//输出一下格式------------------
Copy after login -
printf("Output Information====================\n");
Copy after login -
av_dump_format(ofmt_ctx, 0, out_filename, 1);
Copy after login -
printf("======================================\n");
Copy after loginCopy after login -
//打开输出文件(Open output file)
Copy after login -
if (!(ofmt->flags & AVFMT_NOFILE)) {
Copy after login -
if (avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE)
Copy after login -
printf( "Could not open output file '%s'", out_filename);
Copy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
//写文件头(Write file header)
Copy after login -
if (avformat_write_header(ofmt_ctx, NULL)
Copy after login -
printf( "Error occurred when opening output file\n");
Copy after login -
goto end;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
int frame_index=0;
Copy after login -
int64_t cur_pts_v=0,cur_pts_a=0;
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
//FIX
Copy after loginCopy after login -
#if USE_H264BSF
Copy after loginCopy after loginCopy after login -
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
#if USE_AACBSF
Copy after loginCopy after loginCopy after login -
AVBitStreamFilterContext* aacbsfc = av_bitstream_filter_init("aac_adtstoasc");
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
while (1) {
Copy after login -
AVFormatContext *ifmt_ctx;
Copy after login -
int stream_index=0;
Copy after login -
AVStream *in_stream, *out_stream;
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
//获取一个AVPacket(Get an AVPacket)
Copy after login -
if(av_compare_ts(cur_pts_v,ifmt_ctx_v->streams[videoindex_v]->time_base,cur_pts_a,ifmt_ctx_a->streams[audioindex_a]->time_base)
Copy after login -
ifmt_ctx=ifmt_ctx_v;
Copy after login -
stream_index=videoindex_out;
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
if(av_read_frame(ifmt_ctx, &pkt) >= 0){
Copy after loginCopy after login -
do{
Copy after loginCopy after login -
if(pkt.stream_index==videoindex_v){
Copy after login -
cur_pts_v=pkt.pts;
Copy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}while(av_read_frame(ifmt_ctx, &pkt) >= 0);
Copy after loginCopy after login -
}else{
Copy after loginCopy after loginCopy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}else{
Copy after loginCopy after loginCopy after login -
ifmt_ctx=ifmt_ctx_a;
Copy after login -
stream_index=audioindex_out;
Copy after login -
if(av_read_frame(ifmt_ctx, &pkt) >= 0){
Copy after loginCopy after login -
do{
Copy after loginCopy after login -
if(pkt.stream_index==audioindex_a){
Copy after login -
cur_pts_a=pkt.pts;
Copy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}while(av_read_frame(ifmt_ctx, &pkt) >= 0);
Copy after loginCopy after login -
}else{
Copy after loginCopy after loginCopy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
in_stream = ifmt_ctx->streams[pkt.stream_index];
Copy after login -
out_stream = ofmt_ctx->streams[stream_index];
Copy after login -
//FIX
Copy after loginCopy after login -
#if USE_H264BSF
Copy after loginCopy after loginCopy after login -
av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
#if USE_AACBSF
Copy after loginCopy after loginCopy after login -
av_bitstream_filter_filter(aacbsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
//FIX:No PTS (Example: Raw H.264)
Copy after login -
//Simple Write PTS
Copy after login -
if(pkt.pts==AV_NOPTS_VALUE){
Copy after login -
//Write PTS
Copy after login -
AVRational time_base1=in_stream->time_base;
Copy after login -
//Duration between 2 frames (us)
Copy after login -
int64_t calc_duration=(double)AV_TIME_BASE/av_q2d(in_stream->r_frame_rate);
Copy after login -
//Parameters
Copy after login -
pkt.pts=(double)(frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);
Copy after login -
pkt.dts=pkt.pts;
Copy after login -
pkt.duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);
Copy after login -
frame_index++;
Copy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
/* copy packet */
Copy after login -
//转换PTS/DTS(Convert PTS/DTS)
Copy after login -
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
Copy after login -
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));
Copy after login -
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
Copy after login -
pkt.pos = -1;
Copy after login -
pkt.stream_index=stream_index;
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
printf("Write 1 Packet. size:%5d\tpts:%8d\n",pkt.size,pkt.pts);
Copy after login -
//写入(Write)
Copy after login -
if (av_interleaved_write_frame(ofmt_ctx, &pkt)
Copy after login -
printf( "Error muxing packet\n");
Copy after login -
break;
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
av_free_packet(&pkt);
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
//写文件尾(Write file trailer)
Copy after login -
av_write_trailer(ofmt_ctx);
Copy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
#if USE_H264BSF
Copy after loginCopy after loginCopy after login -
av_bitstream_filter_close(h264bsfc);
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
#if USE_AACBSF
Copy after loginCopy after loginCopy after login -
av_bitstream_filter_close(aacbsfc);
Copy after login -
#endif
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login
-
end:
Copy after login -
avformat_close_input(&ifmt_ctx_v);
Copy after login -
avformat_close_input(&ifmt_ctx_a);
Copy after login -
/* close output */
Copy after login -
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
Copy after login -
avio_close(ofmt_ctx->pb);
Copy after login -
avformat_free_context(ofmt_ctx);
Copy after login -
if (ret
Copy after login -
printf( "Error occurred.\n");
Copy after login -
return -1;
Copy after login -
}
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login -
return 0;
Copy after login

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When listening to music or watching movies on a Win11 computer, if the speakers or headphones sound unbalanced, users can manually adjust the balance level according to their needs. So how do we adjust? In response to this problem, the editor has brought a detailed operation tutorial, hoping to help everyone. How to balance left and right audio channels in Windows 11? Method 1: Use the Settings app to tap the key and click Settings. Windows click System and select Sound. Choose more sound settings. Click on your speakers/headphones and select Properties. Navigate to the Levels tab and click Balance. Make sure "left" and

For as long as I can remember, I have had a pair of large floor-standing speakers at home. I have always believed that a TV can only be called a TV if it is equipped with a complete sound system. But when I first started working, I couldn’t afford professional home audio. After inquiring and understanding the product positioning, I found that the sound bar category is very suitable for me. It meets my needs in terms of sound quality, size and price. Therefore, I decided to go with the soundbar. After careful selection, I selected this panoramic soundbar product launched by Bose in early 2024: Bose home entertainment speaker Ultra. (Photo source: Photographed by Lei Technology) Generally speaking, if we want to experience the "original" Dolby Atmos effect, we need to install a measured and calibrated surround sound + ceiling at home.

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

QQ email: QQ number@qq.com, English QQ email: English or numbers@qq.com, foxmail email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: QQ Mailbox 6.3.3 Analysis 1QQ mailbox has four formats, commonly used QQ mailbox: QQ number@qq.com, English QQ mailbox: English or numbers@qq.com, foxmail Email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Supplement: What is qq mailbox? 1 The earliest QQ mailbox was only between QQ users

If you encounter RealtekHD audio driver failure error code 0x00005b3 on Windows 11/10 PC, please refer to the following steps to resolve the issue. We'll guide you through troubleshooting and resolving the error. Error code 0x00005b3 may be caused by audio driver installation issues. It may be that the current driver is corrupted or partially uninstalled, affecting the installation of the new driver. This issue can also be caused by insufficient disk space or an audio driver that is incompatible with your Windows version. Installation of RealtekHD audio driver failed! ! [Error Code: 0x00005B3] If there is a problem with the Realtek Audio Driver Installation Wizard, continue reading

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

According to news from this site on April 17, TrendForce recently released a report, believing that demand for Nvidia's new Blackwell platform products is bullish, and is expected to drive TSMC's total CoWoS packaging production capacity to increase by more than 150% in 2024. NVIDIA Blackwell's new platform products include B-series GPUs and GB200 accelerator cards integrating NVIDIA's own GraceArm CPU. TrendForce confirms that the supply chain is currently very optimistic about GB200. It is estimated that shipments in 2025 are expected to exceed one million units, accounting for 40-50% of Nvidia's high-end GPUs. Nvidia plans to deliver products such as GB200 and B100 in the second half of the year, but upstream wafer packaging must further adopt more complex products.

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe
