@@ -1868,8 +1868,20 @@ void FFmpegReader::UpdatePTSOffset(bool is_video) {
18681868 // VIDEO PACKET
18691869 if (video_pts_offset == 99999 ) // Has the offset been set yet?
18701870 {
1871- // Find the difference between PTS and frame number (no more than 10 timebase units allowed)
1872- video_pts_offset = 0 - std::max (GetVideoPTS (), (int64_t ) info.video_timebase .ToInt () * 10 );
1871+ // Find the difference between PTS and frame number
1872+ video_pts_offset = 0 - GetVideoPTS ();
1873+
1874+ // Find the difference between PTS and frame number
1875+ // Also, determine if PTS is invalid (too far away from zero)
1876+ // We compare the PTS to the timebase value equal to 1 second (which means the PTS
1877+ // must be within the -1 second to +1 second of zero, otherwise we ignore it)
1878+ // TODO: Please see https://github.com/OpenShot/libopenshot/pull/565#issuecomment-690985272
1879+ // for ideas to improve this logic.
1880+ int64_t max_offset = info.video_timebase .Reciprocal ().ToFloat ();
1881+ if (video_pts_offset < -max_offset || video_pts_offset > max_offset) {
1882+ // Ignore PTS, it seems invalid
1883+ video_pts_offset = 0 ;
1884+ }
18731885
18741886 // debug output
18751887 ZmqLogger::Instance ()->AppendDebugMethod (" FFmpegReader::UpdatePTSOffset (Video)" , " video_pts_offset" , video_pts_offset, " is_video" , is_video);
@@ -1878,8 +1890,18 @@ void FFmpegReader::UpdatePTSOffset(bool is_video) {
18781890 // AUDIO PACKET
18791891 if (audio_pts_offset == 99999 ) // Has the offset been set yet?
18801892 {
1881- // Find the difference between PTS and frame number (no more than 10 timebase units allowed)
1882- audio_pts_offset = 0 - std::max (packet->pts , (int64_t ) info.audio_timebase .ToInt () * 10 );
1893+ // Find the difference between PTS and frame number
1894+ // Also, determine if PTS is invalid (too far away from zero)
1895+ // We compare the PTS to the timebase value equal to 1 second (which means the PTS
1896+ // must be within the -1 second to +1 second of zero, otherwise we ignore it)
1897+ // TODO: Please see https://github.com/OpenShot/libopenshot/pull/565#issuecomment-690985272
1898+ // for ideas to improve this logic.
1899+ audio_pts_offset = 0 - packet->pts ;
1900+ int64_t max_offset = info.audio_timebase .Reciprocal ().ToFloat ();
1901+ if (audio_pts_offset < -max_offset || audio_pts_offset > max_offset) {
1902+ // Ignore PTS, it seems invalid
1903+ audio_pts_offset = 0 ;
1904+ }
18831905
18841906 // debug output
18851907 ZmqLogger::Instance ()->AppendDebugMethod (" FFmpegReader::UpdatePTSOffset (Audio)" , " audio_pts_offset" , audio_pts_offset, " is_video" , is_video);
0 commit comments