Davion
Davion

Reputation: 51

Hls.js convert video PTS from units to seconds

Short

I would like to add a time shift to the streaming video player using PTS, but do not know how should correctly do a conversion from units to seconds.

Long

I'm trying to make a simple streaming video player using hls.js and I would like to show in browsers player difference between real streaming start time and time a user connects to the stream. For example - a streaming video goes for 2 minutes 15 sec, if now a user opens my player it should show not "0 - 36" (36 - is time for several already uploaded fragments), but "2:15 - 2:51". Simply saying, I would like to add a time shift to the video player. Since it is MPEG-ts it contains PTS in PES header that is connected to 90khz time clock. In hls.js already done the job with getting PTS from packets. So, it should be easy to set new var equal to PTS after checks and bitwise operations and convert new var from units to seconds. And there is my problem - I don't know how to do the conversion in the right way. I tried to divide got PTS onto 90000, but the resulted value is bigger than video time on 1.46 sec and grows with video duration (I checked videos up to 2.5 hrs and at the end of the video it was about 6sec greater than real video time). In this case, I think that because of division I will always get a value that bigger than real on 1 sec - can set crutch by subtraction by 1 in the code. But this does not solve the problem of growing time.

From the beginning it gives me:

PTS = 132000

Next I'm trying to divide it by 90khz:

132000/90000 = 1,466666667 sec

Help me please, what am I doing wrong?

Upvotes: 0

Views: 2154

Answers (1)

szatmary
szatmary

Reputation: 31120

Dividing by 90000 is correct, but the file does not start at PTS 0. You need subtract the first PTS of the video from the current PTS, then divide.

Upvotes: 0

Related Questions