[Libav-user] Confusion about PTS in files and streams

Bradley O'Hearne brado at bighillsoftware.com
Fri Nov 21 17:19:55 CET 2014

> On Nov 6, 2014, at 4:07 AM, Maziar Mehrabi <maziar.mehrabi at gmail.com> wrote:
> Hi,
> Is it possible that the PTS of a particular frame in a file is different with the PTS of the same frame in the same file while it is being streamed?
> When I read a frame using av_read_frame I store the video stream in a AVStream. After I decode the frame with avcodec_decode_video2, I store the time stamp of that frame in a int64_t using av_frame_get_best_effort_timestamp. Now if the program is getting its input from a file I get a different timestamp than if I stream the input (from the same file) to the program.
> To change the input type I simply change the argv argument from "/path/to/file.mp4" to something like "udp://localhost:1234", then I stream the file with ffmpeg in command line: "ffmpeg -re -i /path/to/file.mp4 -f mpegts udp://localhost:1234".
> Below is my code (simplified). By reading the previous mailing lists I realized that the time_base that I'm looking for is in the AVStream and not the AVCodecContext. Instead of using av_frame_get_best_effort_timestamp I have also tried using the packet.pts but the results don't change.
> I need the time stamps to have a notion of frame number in a streaming video.
> I would really appreciate any sort of help.
> //..
> //argv[1]="/file.mp4";
> argv[1]="udp://localhost:7777";
> // define AVFormatContext, AVFrame...
> // register av, avcodec, avformat_network_init()...
> avformat_open_input(&pFormatCtx, argv, NULL, NULL);
> avformat_find_stream_info(pFormatCtx, NULL);
> // find the video stream...
> // pointer to the codec context...
> // open codec...
> pFrame=av_frame_alloc();
> while(av_read_frame(pFormatCtx, &packet)>=0) {
>         AVStream *strem = pFormatCtx->streams[videoStream];
>         if(packet.stream_index==videoStream) {
>             avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
>             if(frameFinished) {
>                 int64_t perts = av_frame_get_best_effort_timestamp(pFrame);
>                 if (isMyFrame(pFrame)){
>                      cout << perts*av_q2d(strem->time_base) << "\n";
>                 }
>              }
> }
> //free allocated space
> }
> //..
> Thanks a lot,
> Maziar


I’ve been watching this issue, and I never saw any responses on it. Did you ever get it worked out, and if so, could you share the resolution? 



More information about the Libav-user mailing list