seek_print.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <unistd.h>
  21. #include "config.h"
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h> /* getopt */
  24. #endif
  25. #include "libavformat/avformat.h"
  26. #include "libavutil/timestamp.h"
  27. #if !HAVE_GETOPT
  28. #include "compat/getopt.c"
  29. #endif
  30. static void usage(int ret)
  31. {
  32. fprintf(ret ? stderr : stdout,
  33. "Usage: seek_print file [command ...]\n"
  34. "Commands:\n"
  35. " read\n"
  36. " seek:stream:min_ts:ts:max_ts:flags\n"
  37. );
  38. exit(ret);
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. int opt, ret, stream, flags;
  43. const char *filename;
  44. AVFormatContext *avf = NULL;
  45. int64_t min_ts, max_ts, ts;
  46. AVPacket packet;
  47. while ((opt = getopt(argc, argv, "h")) != -1) {
  48. switch (opt) {
  49. case 'h':
  50. usage(0);
  51. default:
  52. usage(1);
  53. }
  54. }
  55. argc -= optind;
  56. argv += optind;
  57. if (!argc)
  58. usage(1);
  59. filename = *argv;
  60. argv++;
  61. argc--;
  62. av_register_all();
  63. if ((ret = avformat_open_input(&avf, filename, NULL, NULL)) < 0) {
  64. fprintf(stderr, "%s: %s\n", filename, av_err2str(ret));
  65. return 1;
  66. }
  67. if ((ret = avformat_find_stream_info(avf, NULL)) < 0) {
  68. fprintf(stderr, "%s: could not find codec parameters: %s\n", filename,
  69. av_err2str(ret));
  70. return 1;
  71. }
  72. for (; argc; argc--, argv++) {
  73. if (!strcmp(*argv, "read")) {
  74. ret = av_read_frame(avf, &packet);
  75. if (ret < 0) {
  76. printf("read: %d (%s)\n", ret, av_err2str(ret));
  77. } else {
  78. AVRational *tb = &avf->streams[packet.stream_index]->time_base;
  79. printf("read: %d size=%d stream=%d dts=%s (%s) pts=%s (%s)\n",
  80. ret, packet.size, packet.stream_index,
  81. av_ts2str(packet.dts), av_ts2timestr(packet.dts, tb),
  82. av_ts2str(packet.pts), av_ts2timestr(packet.pts, tb));
  83. av_free_packet(&packet);
  84. }
  85. } else if (sscanf(*argv, "seek:%i:%"PRIi64":%"PRIi64":%"PRIi64":%i",
  86. &stream, &min_ts, &ts, &max_ts, &flags) == 5) {
  87. ret = avformat_seek_file(avf, stream, min_ts, ts, max_ts, flags);
  88. printf("seek: %d (%s)\n", ret, av_err2str(ret));
  89. } else {
  90. fprintf(stderr, "'%s': unknown command\n", *argv);
  91. return 1;
  92. }
  93. }
  94. avformat_close_input(&avf);
  95. return 0;
  96. }