seek_print.c 3.2 KB

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