Pts Time Conversion method for C + + programming Audio-Video library FFmpeg

Source: Internet
Author: User

FFmpeg in the pts,dts,duration time records are based on timebase conversion, we mainly analyze the time of the PTS How to convert, the other is the same conversion. FFmpeg time conversion to many new contact students is a big pit, many just contact FFmpeg students are not easy to figure out its time calculation method.

Let's take a look at how the FFmpeg time is calculated:

Timestamp for one frame of image (audio) (typically starting at 0 for the first frame) = pts * (avrational.num/avrational.den)

Why do we have to do this (dig a hole) we look at the FFmpeg note:

"When rational numbers are converted by floating-point numbers, the ffmpeg requires a high-precision time stamp, so use fractions to convert".

We're looking at the structure of the conversion, and it's easy to see that he's using fractions.

typedef struct avrational{

int num; < molecule

int den; < denominator

} avrational;

In fact, when num=1,den=1000, pts ' units of time are equivalent to milliseconds of 1/1000 seconds.

In fact, when num=1,den=1000000, the time unit of PTS is equivalent to microseconds 1/1000000 seconds

Time Conversion

For example, we want to achieve live streaming through FFmpeg, push a file, the time base in the file is generally {num=1,den=1000000}, the normal use of push flow is timebase {num=1,den=1000}. Then you can do the following calculations:

pts for Push stream = File pts * File timebase/push stream timebase

If you manually calculate whether the denominator is 0, it will cause the program to go down. Of course FFmpeg also provides a function of conversion

int64_t Av_rescale_q (int64_t A, avrational BQ, avrational cq) Av_const;

can be called directly

Our code example

Avcodeccontext *VC = NULL; Video Encoder Context

Avstream *vs = NULL; Output video stream from rtmp wrapper

Coding, pushing stream processing code .....

。。。

。。。

Encoded video packet, the time base used by the video encoder

Avpacket PKT;

。。。

PTS for Push Flow

Using the FFmpeg function conversion

pkt.pts = Av_rescale_q (pkt.pts,vc->timebase,vs->timebase);

If you manually convert

if (Vc->timebase.den > 0 && vs->timebase.den > 0)

pkt.pts = pkt.pts * (vc->timebase.num/vc->timebase.den)/(Vs->timebase.num/vs->timebase.den)


More information can also follow my csdn on the video course

Summer Teacher Course Column http://edu.csdn.net/lecturer/961

C + + programming FFmpeg real-time live beauty streaming-based on FFMPEG,QT5,OPENCV video courses



Pts Time Conversion method for C + + programming Audio-Video library FFmpeg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.