FFmpeg
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 "config.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h> /* getopt */
24 #endif
25 
26 #include "libavformat/avformat.h"
27 #include "libavutil/timestamp.h"
28 
29 #if !HAVE_GETOPT
30 #include "compat/getopt.c"
31 #endif
32 
33 static void usage(int ret)
34 {
35  fprintf(ret ? stderr : stdout,
36  "Usage: seek_print file [command ...]\n"
37  "Commands:\n"
38  " read\n"
39  " seek:stream:min_ts:ts:max_ts:flags\n"
40  );
41  exit(ret);
42 }
43 
44 int main(int argc, char **argv)
45 {
46  int opt, ret, stream, flags;
47  const char *filename;
48  AVFormatContext *avf = NULL;
49  int64_t min_ts, max_ts, ts;
51 
52  while ((opt = getopt(argc, argv, "h")) != -1) {
53  switch (opt) {
54  case 'h':
55  usage(0);
56  default:
57  usage(1);
58  }
59  }
60  argc -= optind;
61  argv += optind;
62  if (!argc)
63  usage(1);
64  filename = *argv;
65  argv++;
66  argc--;
67 
68  if ((ret = avformat_open_input(&avf, filename, NULL, NULL)) < 0) {
69  fprintf(stderr, "%s: %s\n", filename, av_err2str(ret));
70  return 1;
71  }
72  if ((ret = avformat_find_stream_info(avf, NULL)) < 0) {
73  fprintf(stderr, "%s: could not find codec parameters: %s\n", filename,
74  av_err2str(ret));
75  return 1;
76  }
77 
78  for (; argc; argc--, argv++) {
79  if (!strcmp(*argv, "read")) {
80  ret = av_read_frame(avf, &packet);
81  if (ret < 0) {
82  printf("read: %d (%s)\n", ret, av_err2str(ret));
83  } else {
84  AVRational *tb = &avf->streams[packet.stream_index]->time_base;
85  printf("read: %d size=%d stream=%d dts=%s (%s) pts=%s (%s)\n",
86  ret, packet.size, packet.stream_index,
88  av_ts2str(packet.pts), av_ts2timestr(packet.pts, tb));
90  }
91  } else if (sscanf(*argv, "seek:%i:%"SCNi64":%"SCNi64":%"SCNi64":%i",
92  &stream, &min_ts, &ts, &max_ts, &flags) == 5) {
93  ret = avformat_seek_file(avf, stream, min_ts, ts, max_ts, flags);
94  printf("seek: %d (%s)\n", ret, av_err2str(ret));
95  } else {
96  fprintf(stderr, "'%s': unknown command\n", *argv);
97  return 1;
98  }
99  }
100 
101  avformat_close_input(&avf);
102 
103  return 0;
104 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
int64_t
long long int64_t
Definition: coverity.c:34
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1525
usage
static void usage(int ret)
Definition: seek_print.c:33
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:362
getopt
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:41
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:214
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
main
int main(int argc, char **argv)
Definition: seek_print.c:44
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:782
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2499
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:83
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
avformat_seek_file
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.
Definition: seek.c:662
printf
printf("static const uint8_t my_array[100] = {\n")
packet
enum AVPacketSideDataType packet
Definition: decode.c:1381
tb
#define tb
Definition: regdef.h:68
optind
static int optind
Definition: getopt.c:37
ret
ret
Definition: filter_design.txt:187
avformat.h
getopt.c
AVPacket
This structure stores compressed data.
Definition: packet.h:499
timestamp.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54