Kranti
Kranti

Reputation: 192

Calculate TS File Duration

I am working on a media player application : Which plays ISDB-T audio and video. I am using GStreamer for decoding & rendering.

For AV Sync to work perfectly, I should regulate file reads: so that data will be not be pushed to Gstreamer neither too fast nor too slow.

If I know the duration of TS file before hand, then I can regulate my reads. But how to calculate the TS file duration ?

Because, I need to verify the application with multiple TS files, cannot calculate the duration using some utility and keep changing the file reads - How can this be achieved in program?

Thanks,

Kranti

Upvotes: 0

Views: 4348

Answers (2)

NiRR
NiRR

Reputation: 5002

If you have sufficient knowledge in the encoding and PES layer inside the transport stream, then you can read the time-stamps within the TS and calculate it yourself. It requires seeking to the end of the file, searching for the last time-stamp, and subtracting the first time stamp of the same program in the beginning of the file.

EDIT: In addition to the above method you need to include the last frame duration.
((last_pts - first_pts) + frame_duration) / pts_resolution

Lets say you have a 30 fps segment with a duration of 6.006s
((1081080 - 543543) + 3003) / 90000 = 6.006

Upvotes: 2

thomasgeorgec
thomasgeorgec

Reputation: 13

in most cases, each PES header contains a PTS and/or DTS, which is measured in 90kHz frequency. so the steps may include:

  1. find the program you need to demux from MPEG TS.
  2. find the PID of stream.
  3. find the first TS packet with PID found, and payload_start_indicator set to 1; that will be the starting of a PES frame, which will contain a PES header.
  4. Parse the PES header to find the starting PTS of the stream.
  5. parse the file backwards from end, to find a packet with same PID and payload_start_indicator set, which will contain the last PTS. find thier difference, divide it by 90000 will give duration in Seconds

Upvotes: 1

Related Questions