add:Rescale packet timestamp to container time base

Some containers force their own time base. For example, matroska
overwrite time_base to (AVRational) {1, 1000}.

Therefore, rescale our packet timestamps to the output stream time base.
This commit is contained in:
Barry 2019-06-12 09:07:31 +08:00
commit cf459bbd32
2 changed files with 11 additions and 4 deletions

View file

@ -12,6 +12,8 @@
# define LAVF_NEW_CODEC_API # define LAVF_NEW_CODEC_API
#endif #endif
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
Recorder::Recorder(const QString& fileName) Recorder::Recorder(const QString& fileName)
: m_fileName(fileName) : m_fileName(fileName)
{ {
@ -68,10 +70,7 @@ bool Recorder::open(AVCodec *inputCodec)
outStream->codec->pix_fmt = AV_PIX_FMT_YUV420P; outStream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
outStream->codec->width = m_declaredFrameSize.width(); outStream->codec->width = m_declaredFrameSize.width();
outStream->codec->height = m_declaredFrameSize.height(); outStream->codec->height = m_declaredFrameSize.height();
#endif #endif
// timestamps in us
outStream->time_base.num = 1;
outStream->time_base.den = 1000000;
int ret = avio_open(&m_formatCtx->pb, m_fileName.toUtf8().toStdString().c_str(), int ret = avio_open(&m_formatCtx->pb, m_fileName.toUtf8().toStdString().c_str(),
AVIO_FLAG_WRITE); AVIO_FLAG_WRITE);
@ -112,6 +111,7 @@ bool Recorder::write(AVPacket *packet)
} }
m_headerWritten = true; m_headerWritten = true;
} }
recorderRescalePacket(packet);
return av_write_frame(m_formatCtx, packet) >= 0; return av_write_frame(m_formatCtx, packet) >= 0;
} }
@ -161,3 +161,9 @@ bool Recorder::recorderWriteHeader(AVPacket *packet)
} }
return true; return true;
} }
void Recorder::recorderRescalePacket(AVPacket *packet)
{
AVStream *ostream = m_formatCtx->streams[0];
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
}

View file

@ -22,6 +22,7 @@ public:
private: private:
const AVOutputFormat* findMp4Muxer(); const AVOutputFormat* findMp4Muxer();
bool recorderWriteHeader(AVPacket* packet); bool recorderWriteHeader(AVPacket* packet);
void recorderRescalePacket(AVPacket *packet);
private: private:
QString m_fileName = ""; QString m_fileName = "";