Skip to content

Commit 6054e0d

Browse files
committed
fix: improve packet timestamp calculation for streams without PTS
1 parent 2e0d950 commit 6054e0d

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

lib/web-demuxer/web_demuxer.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,14 @@ inline std::string safe_str(const char* str) {
140140
void gen_web_packet(WebAVPacket &web_packet, AVPacket *packet, AVStream *stream)
141141
{
142142
double packet_timestamp = 0;
143+
bool has_b_frames = stream->codecpar->video_delay > 0;
143144

144145
if (packet->pts != AV_NOPTS_VALUE) {
145146
packet_timestamp = packet->pts * av_q2d(stream->time_base);
146147
}
147-
else if (stream->avg_frame_rate.num > 0 && stream->avg_frame_rate.den > 0) {
148-
int64_t frame_count = 0;
149-
150-
double frame_duration = (double)stream->avg_frame_rate.den / stream->avg_frame_rate.num;
151-
152-
double base_time = (stream->start_time != AV_NOPTS_VALUE) ?
153-
(stream->start_time * av_q2d(stream->time_base)) : 0;
154-
155-
// some videos like avi have no pts, so we need to calculate the pts
156-
// from the frame count and the average frame rate
157-
packet_timestamp = base_time + (frame_count++ * frame_duration);
148+
else if (packet->dts != AV_NOPTS_VALUE && !has_b_frames) {
149+
// Some formats such as AVI do not have PTS and use DTS instead
150+
packet_timestamp = packet->dts * av_q2d(stream->time_base);
158151
}
159152

160153
web_packet.keyframe = packet->flags & AV_PKT_FLAG_KEY;

0 commit comments

Comments
 (0)