cuốn sách gpt4 ai đã làm

FPS quá cao khi lưu video vào vùng chứa mp4

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-02 23:21:59 25 4
mua khóa gpt4 Nike

Khi tôi giải mã các khung hình từ tệp avi, sau đó giải mã chúng ở x264 và lưu vào tệp mp4, khung hình/giây của tệp đầu ra luôn là 12.800. Vì vậy file phát rất nhanh. Tuy nhiên, khi tôi lưu các khung hình h264 được mã hóa dưới dạng avi thay vì mp4, khung hình/giây là thứ tôi muốn - 25.

可能是什么问题?

Đây là mã tôi đã viết trong VS2010:

#include "stdafx.h"
#include "inttypes.h"

bên ngoài "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include
#include
#include
#include
}

#include
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);

// Tìm luồng video
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;
phá vỡ;
}
}
if (videoStreamIndex == -1) exit(1);

AVFormatContext* outContainer = NULL;
if(avformat_alloc_output_context2(&outContainer, NULL, outFileType, outFileName) < 0)
exit(1);

//-----------------------------
// Bộ giải mã
//-----------------------------
AVStream const *const inStream = inContainer->streams[videoStreamIndex];
AVCodec *bộ giải mã const = avcodec_find_decoding(inStream->codec->codec_id);
nếu(! Bộ giải mã)
exit(1);
if(avcodec_open2(inStream->codec, bộ giải mã, NULL) < 0)
exit(1);

//-----------------------------
// Bộ mã hóa
//-----------------------------
AVCodec *bộ mã hóa = avcodec_find_encoding(AV_CODEC_ID_H264);
nếu (! Bộ mã hóa)
exit(1);
AVStream *outStream = avformat_new_stream(outContainer, bộ mã hóa);
if(!outStream)
exit(1);
avcodec_get_context_defaults3(outStream->codec, bộ mã hóa);

//Xây dựng bộ mã hóa
if(outContainer->oformat->flags & AVFMT_GLOBALHEADER)
outStream->codec->cờ |= CODEC_FLAG_GLOBAL_HEADER;

outStream->codec->code_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 = bộ mã hóa->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; // Khoảng thời gian khung hình chính(=độ dài GOP) Xác định khoảng cách tối đa giữa các khung hình I.
outStream->codec->keyint_min = 25; // kích thước GOP tối thiểu
outStream->codec->max_b_frames = 3;//16; // số lượng khung hình B tối đa giữa các khung hình không phải B
outStream->codec->b_frame_strategy = 1; // quyết định số lượng khung B tốt nhất để sử dụng.
outStream->codec->scenechange_threshold = 40;
outStream->codec->refs = 6; // khả năng tham chiếu các khung khác với khung ngay trước khung hiện tại chỉ định số lượng tham chiếu có thể được sử dụng.
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->codec->code_type = 1;


if(outStream->codec->codec_id == AV_CODEC_ID_H264)
{
av_opt_set(outStream->codec->priv_data, "đặt trước", "chậm", 0);
}

// Mở bộ mã hóa
if(avcodec_open2(outStream->codec, bộ mã hóa, NULL) < 0)
exit(1);

// Mở vùng chứa đầu ra
if(avio_open(&outContainer->pb, outFileName, AVIO_FLAG_WRITE) < 0)
exit(1);

//đóng_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->chiều rộng, outStream->codec->chiều cao,
outStream->codec->pix_fmt, 1) < 0)
exit(1);

av_dump_format(inContainer, 0, inFileName,0);

// Ghi tiêu đề vào vùng chứa đầu ra
avformat_write_header(outContainer, NULL);

AVPacket giải mãPacket, EncodedPacket;
int got_frame, len;
while(av_read_frame(inContainer, &decodePacket)>=0)
{
nếu (decodePacket.stream_index == videoStreamIndex)
{
len = avcodec_decode_video2(inStream->codec, decodedFrame, &got_frame, &decodePacket);
nếu(len < 0)
exit(1);
nếu(got_frame)
{
av_init_packet(&encodedPacket);
mã hóaPacket.data = NULL;
mã hóaPacket.size = 0;
if(avcodec_encode_video2(outStream->codec, &encodedPacket, decodedFrame, &got_frame) < 0)
exit(1);
nếu(got_frame)
{
if (outStream->codec->coded_frame->key_frame)
được mã hóaPacket.flags |= AV_PKT_FLAG_KEY;

mã hóaPacket.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;
}

câu trả lời hay nhất

Vấn đề nằm ở PTS và DTS của gói. Trước khi ghi gói tin vào đầu ra (trong av_interleaved_write_frame trước lệnh) đặt PTS và DTS như thế này

nếu (encodedPacket.pts != AV_NOPTS_VALUE)
được mã hóaPacket.pts = av_rescale_q(encodedPacket.pts, outStream->codec->time_base, outStream->time_base);
nếu (encodedPacket.dts != AV_NOPTS_VALUE)
được mã hóaPacket.dts = av_rescale_q(encodedPacket.dts, outStream->codec->time_base, outStream->time_base);

Về c++ - FPS quá cao khi lưu video vào vùng chứa mp4, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/13565062/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress