Dump PTS

- - | Comments

利用FFmpeg可以dump出TS file裡的PTS, 以下是範例程式:

pts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
 AVFormatContext *pFormatCtx ;
 int             i ;
 int             videoStream ;
 AVPacket        packet ;
 AVCodecContext  *pCodecCtx;
 AVCodec         *pCodec;
 AVFrame         *pFrame;
 AVFrame         *pFrameRGB;
 int             numBytes;
 uint8_t         *buffer;

 if(argc < 2)
   {
    printf("Please provide a movie file\n");
    return -1;
   }

 // Register all formats and codecs
 av_register_all();

 // Open video file
 if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
   {
    return -1; // Couldn't open file
   }

 // Retrieve stream information
 if(av_find_stream_info(pFormatCtx)<0)
   {
    return -1; // Couldn't find stream information
   }

 // Dump information about file onto standard error
 dump_format(pFormatCtx, 0, argv[1], 0);

 // Find the first video stream
 videoStream = -1 ;
 for( i = 0 ; i < pFormatCtx->nb_streams ; i ++ )
    {
     if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
       {
        videoStream=i;
        break;
       }
    }

 if( videoStream == -1 )
   {
    printf("Didn't find a video stream.\n");
    return -1; // Didn't find a video stream
   }

 // Get a pointer to the codec context for the video stream
 pCodecCtx=pFormatCtx->streams[videoStream]->codec;

 // Find the decoder for the video stream
 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
 if(pCodec==NULL) {
   printf("Unsupported codec!\n");
   return -1; // Codec not found
 }
 // Open codec
 if(avcodec_open(pCodecCtx, pCodec)<0)
   return -1; // Could not open codec

 // Allocate video frame
 pFrame=avcodec_alloc_frame();

 // Allocate an AVFrame structure
 pFrameRGB=avcodec_alloc_frame();
 if(pFrameRGB==NULL)
   return -1;

 // Determine required buffer size and allocate buffer
 numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
           pCodecCtx->height);
 buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

 // Assign appropriate parts of buffer to image planes in pFrameRGB
 // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
 // of AVPicture
 avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
     pCodecCtx->width, pCodecCtx->height);

 // Read frames
 i = 0 ;
 while(av_read_packet(pFormatCtx, &packet)==0)
      {
       // Is this a packet from the video stream?
       if(packet.stream_index==videoStream)
         {             
          if( packet.pts != AV_NOPTS_VALUE )
            {
             printf("video pts: %lld\n",packet.pts);
            }
         }

       // Free the packet that was allocated by av_read_frame
       av_free_packet(&packet);
      }

 // Free the RGB image
 av_free(buffer);
 av_free(pFrameRGB);

 // Free the YUV frame
 av_free(pFrame);

 // Close the codec
 avcodec_close(pCodecCtx);

 // Close the video file
 av_close_input_file(pFormatCtx);

 return 0;
}

實際dump PTS的狀況如下:

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
bramante@matrix:~/pts$ ll
total 3556
drwxrwxr-x 2 bramante bramante    4096 Aug  7 20:51 ./
drwxr-xr-x 5 bramante bramante    4096 Aug  7 20:32 ../
-rwxrw-rw- 1 bramante bramante 3625204 Jul 21 20:04 demo.ts*
-rwxrw-rw- 1 bramante bramante    2975 Aug  7 20:47 pts.c*
bramante@matrix:~/pts$ gcc -o pts pts.c -lavformat
bramante@matrix:~/pts$ ./pts demo.ts
[mpegts @ 0x1815040] max_analyze_duration reached
Input #0, mpegts, from 'demo.ts':
  Duration: 00:04:15.18, start: 1.400000, bitrate: 113 kb/s
  Program 1
    Metadata:
      service_name    : Service01
      service_provider: Libav
    Stream #0.0[0x100]: Video: mpeg4 (Simple Profile), yuv420p, 640x480 [PAR 1:1 DAR 4:3], 1 fps, 1 tbr, 90k tbn, 1 tbc
    Stream #0.1[0x101]: Audio: aac, 44100 Hz, stereo, s16, 61 kb/s
video pts: 126000
video pts: 216000
video pts: 306000
video pts: 396000
video pts: 486000
video pts: 576000
video pts: 666000
video pts: 756000
video pts: 846000
video pts: 936000
video pts: 1026000
video pts: 1116000
video pts: 1206000
....
video pts: 22806000
video pts: 22896000
video pts: 22986000
video pts: 23076000
bramante@matrix:~/pts$

Comments