在mp4容器中保存视频时,FPS太高 [英] FPS too high when saving video in mp4 container
问题描述
当我解码来自avi文件的帧,然后解码它们在x264和保存到mp4文件,输出文件的fps总是12,800。因此,文件播放非常快。但是,当我保存编码的h264帧在avi格式而不是mp4,所以fps是我想要的 - 25。
这可能是什么问题? p>
这里是我在VS2010中写的代码:
#includestdafx。 h
#includeinttypes.h
externC{
#includelibavcodec / avcodec.h
#includelibavformat / avformat。 h
#includelibavutil / avutil.h
#include< libswscale / swscale.h>
#include< libavutil / opt.h>
#include< libswscale / swscale.h>
#include< libavutil / imgutils.h>
}
#include< iostream>
using namespace std;
int main(int argc,char * argv [])
{
const char * inFileName =C:\\000227_C1_GAME.avi;
const char * outFileName =c:\\test.avi;
const char * outFileType =avi;
av_register_all();
AVFormatContext * inContainer = NULL;
if(avformat_open_input(& inContainer,inFileName,NULL,NULL)< 0)
exit(1);
if(avformat_find_stream_info(inContainer,NULL)< 0)
exit(1);
//查找视频流
int videoStreamIndex = -1;
for(unsigned int i = 0; i nb_streams; ++ i)
{
if(inContainer-> streams [i]& inContainer- > streams [i] - > codec&&
inContainer-> streams [i] - > codec-> codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i ;
break;
}
}
if(videoStreamIndex == -1)exit(1);
AVFormatContext * outContainer = NULL;
if(avformat_alloc_output_context2(& outContainer,NULL,outFileType,outFileName)< 0)
exit(1);
// ----------------------------
//解码器
/ / ----------------------------
AVStream const * const inStream = inContainer-> streams [videoStreamIndex];
AVCodec * const decoder = avcodec_find_decoder(inStream-> codec-> codec_id);
if(!decoder)
exit(1);
if(avcodec_open2(inStream-> codec,decoder,NULL)< 0)
exit(1);
// ----------------------------
//编码器
/ / ---------------------------
AVCodec * encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if(!encoder)
exit(1);
AVStream * outStream = avformat_new_stream(outContainer,encoder);
if(!outStream)
exit(1);
avcodec_get_context_defaults3(outStream-> codec,encoder);
//构造编码器
if(outContainer-> oformat-> flags& AVFMT_GLOBALHEADER)
outStream-> codec-> flags | = CODEC_FLAG_GLOBAL_HEADER;
outStream-> codec-> coder_type = AVMEDIA_TYPE_VIDEO;
outStream-> codec-> pix_fmt = AV_PIX_FMT_YUV420P;
outStream-> codec-> width = inStream-> codec-> width;
outStream-> codec-> height = inStream-> codec-> height;
outStream-> codec-> codec_id = encoder-> id;
outStream-> codec-> bit_rate = 500000;
// outStream-> codec-> rc_min_rate = 600000;
// outStream-> codec-> rc_max_rate = 800000;
outStream-> codec-> time_base.den = 25;
outStream-> codec-> time_base.num = 1;
outStream-> codec-> gop_size = 250; //关键帧间隔(= GOP长度)。确定I帧之间的最大距离距离
outStream-> codec-> keyint_min = 25; //最小GOP大小
outStream-> codec-> max_b_frames = 3; // 16; //非B帧之间的B帧的最大数目
outStream-> codec-> b_frame_strategy = 1; //决定要使用的最佳B帧数。 x264中的默认模式。
outStream-> codec-> scenechange_threshold = 40;
outStream-> codec-> refs = 6; //能够引用除当前帧之前的帧。指定可以使用的引用数量。
outStream-> CODEC-> qmin = 0; // 10;
outStream-> codec-> qmax = 69; // 51;
outStream-> codec-> qcompress = 0.6;
outStream-> codec-> max_qdiff = 4;
outStream-> codec-> i_quant_factor = 1.4; // 0.71;
outStream-> codec-> refs = 1; // 3;
outStream-> codec-> chromaoffset = -2;
outStream-> codec-> thread_count = 1;
outStream-> codec-> trellis = 1;
outStream-> codec-> me_range = 16;
outStream-> codec-> me_method = ME_HEX; // hex
outStream-> codec-> flags2 | = CODEC_FLAG2_FAST;
outStream-> codec-> coder_type = 1;
if(outStream-> codec-> codec_id == AV_CODEC_ID_H264)
{
av_opt_set(outStream-> codec-> priv_data, ,slow,0);
}
//打开编码器
if(avcodec_open2(outStream-> codec,encoder,NULL)< 0)
exit(1)
//打开输出容器
if(avio_open(& outContainer-> pb,outFileName,AVIO_FLAG_WRITE)< 0)
exit(1)
// close_o
AVFrame * decodedFrame = avcodec_alloc_frame();
if(!decodedFrame)
exit(1);
AVFrame * encodeFrame = avcodec_alloc_frame();
if(!encodeFrame)
exit(1);
encodeFrame-> format = outStream-> codec-> pix_fmt;
encodeFrame-> width = outStream-> codec-> width;
encodeFrame-> height = outStream-> codec-> height;
if(av_image_alloc(encodeFrame-> data,encodeFrame-> linesize,
outStream-> codec-> width,outStream-> codec-> height,
outStream- > codec_> pix_fmt,1)< 0)
exit(1);
av_dump_format(inContainer,0,inFileName,0);
//将头写入ouput容器
avformat_write_header(outContainer,NULL);
AVPacket decodePacket,encodedPacket;
int got_frame,len;
while(av_read_frame(inContainer,& decodePacket)> = 0)
{
if(decodePacket.stream_index == videoStreamIndex)
{
len = avcodec_decode_video2 in-stream,> codec,decodedFrame,& got_frame,& decodePacket);
if(len< 0)
exit(1);
if(got_frame)
{
av_init_packet(& encodedPacket);
encodedPacket.data = NULL;
encodedPacket.size = 0;
if(avcodec_encode_video2(outStream-> codec,& encodedPacket,decodedFrame,& got_frame)< 0)
exit(1)
if(got_frame)
{
if(outStream-> codec-> coded_frame-> key_frame)
encodedPacket.flags | = AV_PKT_FLAG_KEY;
encodedPacket.stream_index = outStream-> index;
if(av_interleaved_write_frame(outContainer,& encodedPacket)< 0)
exit(1);
av_free_packet(& encodedPacket);
}
}
}
av_free_packet(& decodePacket);
}
av_write_trailer(outContainer);
avio_close(outContainer-> pb);
avcodec_free_frame(& encodeFrame);
avcodec_free_frame(& decodedFrame);
avformat_free_context(outContainer);
av_close_input_file(inContainer);
return 0;
}
问题出在PTS DTS的包。在写数据包输出之前( av_interleaved_write_frame
命令之前)设置PTS和DTS如下
if(encodedPacket.pts!= AV_NOPTS_VALUE)
encodedPacket.pts = av_rescale_q(encodedPacket.pts,outStream-> codec-> time_base,outStream-> time_base);
if(encodedPacket.dts!= AV_NOPTS_VALUE)
encodedPacket.dts = av_rescale_q(encodedPacket.dts,outStream-> codec-> time_base,outStream-> time_base);
When I decode frames from avi file and then decode them in x264 and save to mp4 file, the fps of the output file is always 12,800. Therefore the file is played very fast. But, when I save the encoded in h264 frames in avi format and not mp4, so the fps is as I wanted - 25.
What could be the problem?
Here the code I wrote in VS2010:
#include "stdafx.h"
#include "inttypes.h"
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
const char* inFileName = "C:\\000227_C1_GAME.avi";
const char* outFileName = "c:\\test.avi";
const char* outFileType = "avi";
av_register_all();
AVFormatContext* inContainer = NULL;
if(avformat_open_input(&inContainer, inFileName, NULL, NULL) < 0)
exit(1);
if(avformat_find_stream_info(inContainer, NULL) < 0)
exit(1);
// Find video stream
int videoStreamIndex = -1;
for (unsigned int i = 0; i < inContainer->nb_streams; ++i)
{
if (inContainer->streams[i] && inContainer->streams[i]->codec &&
inContainer->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) exit(1);
AVFormatContext* outContainer = NULL;
if(avformat_alloc_output_context2(&outContainer, NULL, outFileType, outFileName) < 0)
exit(1);
// ----------------------------
// Decoder
// ----------------------------
AVStream const *const inStream = inContainer->streams[videoStreamIndex];
AVCodec *const decoder = avcodec_find_decoder(inStream->codec->codec_id);
if(!decoder)
exit(1);
if(avcodec_open2(inStream->codec, decoder, NULL) < 0)
exit(1);
// ----------------------------
// Encoder
// ----------------------------
AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if(!encoder)
exit(1);
AVStream *outStream = avformat_new_stream(outContainer, encoder);
if(!outStream)
exit(1);
avcodec_get_context_defaults3(outStream->codec, encoder);
// Construct encoder
if(outContainer->oformat->flags & AVFMT_GLOBALHEADER)
outStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
outStream->codec->coder_type = AVMEDIA_TYPE_VIDEO;
outStream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
outStream->codec->width = inStream->codec->width;
outStream->codec->height = inStream->codec->height;
outStream->codec->codec_id = encoder->id;
outStream->codec->bit_rate = 500000;
//outStream->codec->rc_min_rate = 600000;
//outStream->codec->rc_max_rate = 800000;
outStream->codec->time_base.den = 25;
outStream->codec->time_base.num = 1;
outStream->codec->gop_size = 250; // Keyframe interval(=GOP length). Determines maximum distance distance between I-frames
outStream->codec->keyint_min = 25; // minimum GOP size
outStream->codec->max_b_frames = 3;//16; // maximum number of B-frames between non-B-frames
outStream->codec->b_frame_strategy = 1; // decides the best number of B-frames to use. Default mode in x264.
outStream->codec->scenechange_threshold = 40;
outStream->codec->refs = 6; // abillity to reference frames other than the one immediately prior to the current frame. specify how many references can be used.
outStream->codec->qmin = 0;//10;
outStream->codec->qmax = 69;//51;
outStream->codec->qcompress = 0.6;
outStream->codec->max_qdiff = 4;
outStream->codec->i_quant_factor = 1.4;//0.71;
outStream->codec->refs=1;//3;
outStream->codec->chromaoffset = -2;
outStream->codec->thread_count = 1;
outStream->codec->trellis = 1;
outStream->codec->me_range = 16;
outStream->codec->me_method = ME_HEX; //hex
outStream->codec->flags2 |= CODEC_FLAG2_FAST;
outStream->codec->coder_type = 1;
if(outStream->codec->codec_id == AV_CODEC_ID_H264)
{
av_opt_set(outStream->codec->priv_data, "preset", "slow", 0);
}
// Open encoder
if(avcodec_open2(outStream->codec, encoder, NULL) < 0)
exit(1);
// Open output container
if(avio_open(&outContainer->pb, outFileName, AVIO_FLAG_WRITE) < 0)
exit(1);
//close_o
AVFrame *decodedFrame = avcodec_alloc_frame();
if(!decodedFrame)
exit(1);
AVFrame *encodeFrame = avcodec_alloc_frame();
if(!encodeFrame)
exit(1);
encodeFrame->format = outStream->codec->pix_fmt;
encodeFrame->width = outStream->codec->width;
encodeFrame->height = outStream->codec->height;
if(av_image_alloc(encodeFrame->data, encodeFrame->linesize,
outStream->codec->width, outStream->codec->height,
outStream->codec->pix_fmt, 1) < 0)
exit(1);
av_dump_format(inContainer, 0, inFileName,0);
//Write header to ouput container
avformat_write_header(outContainer, NULL);
AVPacket decodePacket, encodedPacket;
int got_frame, len;
while(av_read_frame(inContainer, &decodePacket)>=0)
{
if (decodePacket.stream_index == videoStreamIndex)
{
len = avcodec_decode_video2(inStream->codec, decodedFrame, &got_frame, &decodePacket);
if(len < 0)
exit(1);
if(got_frame)
{
av_init_packet(&encodedPacket);
encodedPacket.data = NULL;
encodedPacket.size = 0;
if(avcodec_encode_video2(outStream->codec, &encodedPacket, decodedFrame, &got_frame) < 0)
exit(1);
if(got_frame)
{
if (outStream->codec->coded_frame->key_frame)
encodedPacket.flags |= AV_PKT_FLAG_KEY;
encodedPacket.stream_index = outStream->index;
if(av_interleaved_write_frame(outContainer, &encodedPacket) < 0)
exit(1);
av_free_packet(&encodedPacket);
}
}
}
av_free_packet(&decodePacket);
}
av_write_trailer(outContainer);
avio_close(outContainer->pb);
avcodec_free_frame(&encodeFrame);
avcodec_free_frame(&decodedFrame);
avformat_free_context(outContainer);
av_close_input_file(inContainer);
return 0;
}
The problem was with PTS and DTS of the packet. Before writing the packet to output( before av_interleaved_write_frame
command) set PTS and DTS like this
if (encodedPacket.pts != AV_NOPTS_VALUE)
encodedPacket.pts = av_rescale_q(encodedPacket.pts, outStream->codec->time_base, outStream->time_base);
if (encodedPacket.dts != AV_NOPTS_VALUE)
encodedPacket.dts = av_rescale_q(encodedPacket.dts, outStream->codec->time_base, outStream->time_base);
这篇关于在mp4容器中保存视频时,FPS太高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!