seek_print.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <unistd.h>
22 
23 #include "config.h"
24 #if HAVE_UNISTD_H
25 #include <unistd.h> /* getopt */
26 #endif
27 
28 #include "libavformat/avformat.h"
29 #include "libavutil/timestamp.h"
30 
31 #if !HAVE_GETOPT
32 #include "compat/getopt.c"
33 #endif
34 
35 static void usage(int ret)
36 {
37  fprintf(ret ? stderr : stdout,
38  "Usage: seek_print file [command ...]\n"
39  "Commands:\n"
40  " read\n"
41  " seek:stream:min_ts:ts:max_ts:flags\n"
42  );
43  exit(ret);
44 }
45 
46 int main(int argc, char **argv)
47 {
48  int opt, ret, stream, flags;
49  const char *filename;
50  AVFormatContext *avf = NULL;
51  int64_t min_ts, max_ts, ts;
52  AVPacket packet;
53 
54  while ((opt = getopt(argc, argv, "h")) != -1) {
55  switch (opt) {
56  case 'h':
57  usage(0);
58  default:
59  usage(1);
60  }
61  }
62  argc -= optind;
63  argv += optind;
64  if (!argc)
65  usage(1);
66  filename = *argv;
67  argv++;
68  argc--;
69 
71  if ((ret = avformat_open_input(&avf, filename, NULL, NULL)) < 0) {
72  fprintf(stderr, "%s: %s\n", filename, av_err2str(ret));
73  return 1;
74  }
75  if ((ret = avformat_find_stream_info(avf, NULL)) < 0) {
76  fprintf(stderr, "%s: could not find codec parameters: %s\n", filename,
77  av_err2str(ret));
78  return 1;
79  }
80 
81  for (; argc; argc--, argv++) {
82  if (!strcmp(*argv, "read")) {
83  ret = av_read_frame(avf, &packet);
84  if (ret < 0) {
85  printf("read: %d (%s)\n", ret, av_err2str(ret));
86  } else {
87  AVRational *tb = &avf->streams[packet.stream_index]->time_base;
88  printf("read: %d size=%d stream=%d dts=%s (%s) pts=%s (%s)\n",
89  ret, packet.size, packet.stream_index,
90  av_ts2str(packet.dts), av_ts2timestr(packet.dts, tb),
91  av_ts2str(packet.pts), av_ts2timestr(packet.pts, tb));
92  av_free_packet(&packet);
93  }
94  } else if (sscanf(*argv, "seek:%i:%"SCNi64":%"SCNi64":%"SCNi64":%i",
95  &stream, &min_ts, &ts, &max_ts, &flags) == 5) {
96  ret = avformat_seek_file(avf, stream, min_ts, ts, max_ts, flags);
97  printf("seek: %d (%s)\n", ret, av_err2str(ret));
98  } else {
99  fprintf(stderr, "'%s': unknown command\n", *argv);
100  return 1;
101  }
102  }
103 
104  avformat_close_input(&avf);
105 
106  return 0;
107 }
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
static int optind
Definition: getopt.c:37
Format I/O context.
Definition: avformat.h:944
timestamp utils, mostly useful for debugging/logging purposes
AVStream ** streams
Definition: avformat.h:992
static void usage(int ret)
Definition: seek_print.c:35
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:72
ret
Definition: avfilter.c:821
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:110
NULL
Definition: eval.c:55
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:43
rational number numerator/denominator
Definition: rational.h:43
int main(int argc, char **argv)
Definition: seek_print.c:46
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
static int flags
Definition: cpu.c:23
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:50
printf("static const uint8_t my_array[100] = {\n")
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
This structure stores compressed data.
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:52
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define tb
Definition: regdef.h:68