seek_print.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if ((ret = avformat_open_input(&avf, filename, NULL, NULL)) < 0) {
  62. fprintf(stderr, "%s: %s\n", filename, av_err2str(ret));
  63. return 1;
  64. }
  65. if ((ret = avformat_find_stream_info(avf, NULL)) < 0) {
  66. fprintf(stderr, "%s: could not find codec parameters: %s\n", filename,
  67. av_err2str(ret));
  68. return 1;
  69. }
  70. for (; argc; argc--, argv++) {
  71. if (!strcmp(*argv, "read")) {
  72. ret = av_read_frame(avf, &packet);
  73. if (ret < 0) {
  74. printf("read: %d (%s)\n", ret, av_err2str(ret));
  75. } else {
  76. AVRational *tb = &avf->streams[packet.stream_index]->time_base;
  77. printf("read: %d size=%d stream=%d dts=%s (%s) pts=%s (%s)\n",
  78. ret, packet.size, packet.stream_index,
  79. av_ts2str(packet.dts), av_ts2timestr(packet.dts, tb),
  80. av_ts2str(packet.pts), av_ts2timestr(packet.pts, tb));
  81. av_packet_unref(&packet);
  82. }
  83. } else if (sscanf(*argv, "seek:%i:%"SCNi64":%"SCNi64":%"SCNi64":%i",
  84. &stream, &min_ts, &ts, &max_ts, &flags) == 5) {
  85. ret = avformat_seek_file(avf, stream, min_ts, ts, max_ts, flags);
  86. printf("seek: %d (%s)\n", ret, av_err2str(ret));
  87. } else {
  88. fprintf(stderr, "'%s': unknown command\n", *argv);
  89. return 1;
  90. }
  91. }
  92. avformat_close_input(&avf);
  93. return 0;
  94. }