From da5aa894c78f230e278ffc2e22c0662a3ce37647 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 12 Jul 2024 16:12:18 +0800 Subject: [PATCH] Improve:time stamp bug. --- utils/MediaBase/src/FfmpegEncoder.cpp | 12 +++++++----- utils/MediaBase/src/FfmpegOutputStream.cpp | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/utils/MediaBase/src/FfmpegEncoder.cpp b/utils/MediaBase/src/FfmpegEncoder.cpp index bb33f11..a40b048 100644 --- a/utils/MediaBase/src/FfmpegEncoder.cpp +++ b/utils/MediaBase/src/FfmpegEncoder.cpp @@ -225,7 +225,7 @@ int FfmpegEncoder::EncodeData(AVFrame *frame, AVStream *stream, std::functiontime_base.num: %d\n", stream->time_base.num); // LogInfo("Write stream->time_base.den: %d\n", stream->time_base.den); mTmpPkt->stream_index = stream->index; - LogInfo("Write frame mTmpPkt->pts: %llu\n", mTmpPkt->pts); + LogInfo("aaaaaaaaaaaaaaaaaaaaaaa Write frame mTmpPkt->pts: %llu\n", mTmpPkt->pts); if (callback) { // save_code_stream_file(mTmpPkt->data, mTmpPkt->size); @@ -355,10 +355,12 @@ AVFrame *FfmpegEncoder::ConvertAudioFrame(AVFrame *decodeFrame, struct SwrContex LogError("Error while converting\n"); return nullptr; } - - mFrame->pts = av_rescale_q(decodeFrame->pts, (AVRational){1, SOURCE_AUDIO_SAMPEL_RATE}, mCodecCtx->time_base); - LogInfo("decodeFrame->pts = %d\n", decodeFrame->pts); - LogInfo("mFrame->pts = %d\n", mFrame->pts); + LogInfo("mCodecCtx->time_base.num = %d, mCodecCtx->time_base.den=%d\n", + mCodecCtx->time_base.num, + mCodecCtx->time_base.den); + mFrame->pts = av_rescale_q(decodeFrame->pts, (AVRational){1, 1000000}, mCodecCtx->time_base); + // LogInfo("decodeFrame->pts = %d\n", decodeFrame->pts); + // LogInfo("mFrame->pts = %d\n", mFrame->pts); mSamplesCount += dst_nb_samples; return mFrame; } diff --git a/utils/MediaBase/src/FfmpegOutputStream.cpp b/utils/MediaBase/src/FfmpegOutputStream.cpp index 5718672..fa5385c 100644 --- a/utils/MediaBase/src/FfmpegOutputStream.cpp +++ b/utils/MediaBase/src/FfmpegOutputStream.cpp @@ -68,7 +68,7 @@ bool FfmpegOutputStream::Init(AVFormatContext *outputFormat) * @brief There is no need to set time_base here, time_base will be automatically corrected inside ffmpeg. * */ - // mStream->time_base = (AVRational){1, 15}; + mStream->time_base = (AVRational){1, 15}; mStream->codecpar->codec_id = AV_CODEC_ID_H264; mStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; mStream->codecpar->width = 1920; @@ -109,8 +109,9 @@ void FfmpegOutputStream::WriteSourceData(const void *data, const size_t &size, c constexpr int64_t ZERO_MEANS_UNKNOWN = 0; tmpPkt->duration = ZERO_MEANS_UNKNOWN; // tmpPkt->pts = u64Interval * 1000; // ת���� us - tmpPkt->pts = av_rescale_q(pts, (AVRational){1, 15}, mStream->time_base); - // LogInfo("pts:%llu, duration:%d\n", tmpPkt->pts, tmpPkt->duration); + tmpPkt->pts = av_rescale_q(pts, (AVRational){1, 1000000}, mStream->time_base); + LogInfo("vvvvvvvvvvvvvvvvvvvvvvvvv num:%d, den:%d\n", mStream->time_base.num, mStream->time_base.den); + LogInfo("vvvvvvvvvvvvvvvvvvvvvvvvv pts:%llu, duration:%d\n", tmpPkt->pts, tmpPkt->duration); // tmpPkt->pts = pts; u64Interval++; tmpPkt->dts = tmpPkt->pts;