pktdumper.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2005 Francois Revol
  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
  8. * License 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <avformat.h>
  21. #include <limits.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #define PKTFILESUFF "_%08"PRId64"_%02d_%010"PRId64"_%06d_%c.bin"
  28. static int usage(int ret)
  29. {
  30. fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
  31. fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
  32. fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
  33. fprintf(stderr, "-n\twrite No file at all, only demux.\n");
  34. fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
  35. return ret;
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. char fntemplate[PATH_MAX];
  40. char pktfilename[PATH_MAX];
  41. AVFormatContext *fctx;
  42. AVPacket pkt;
  43. int64_t pktnum = 0;
  44. int64_t maxpkts = 0;
  45. int dontquit = 0;
  46. int nowrite = 0;
  47. int err;
  48. if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
  49. if (strchr(argv[1], 'w'))
  50. dontquit = 1;
  51. if (strchr(argv[1], 'n'))
  52. nowrite = 1;
  53. argv++;
  54. argc--;
  55. }
  56. if (argc < 2)
  57. return usage(1);
  58. if (argc > 2)
  59. maxpkts = atoi(argv[2]);
  60. strncpy(fntemplate, argv[1], PATH_MAX-1);
  61. if (strrchr(argv[1], '/'))
  62. strncpy(fntemplate, strrchr(argv[1], '/')+1, PATH_MAX-1);
  63. if (strrchr(fntemplate, '.'))
  64. *strrchr(fntemplate, '.') = '\0';
  65. if (strchr(fntemplate, '%')) {
  66. fprintf(stderr, "can't use filenames containing '%%'\n");
  67. return usage(1);
  68. }
  69. if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX-1) {
  70. fprintf(stderr, "filename too long\n");
  71. return usage(1);
  72. }
  73. strcat(fntemplate, PKTFILESUFF);
  74. printf("FNTEMPLATE: '%s'\n", fntemplate);
  75. // register all file formats
  76. av_register_all();
  77. err = av_open_input_file(&fctx, argv[1], NULL, 0, NULL);
  78. if (err < 0) {
  79. fprintf(stderr, "av_open_input_file: error %d\n", err);
  80. return 1;
  81. }
  82. err = av_find_stream_info(fctx);
  83. if (err < 0) {
  84. fprintf(stderr, "av_find_stream_info: error %d\n", err);
  85. return 1;
  86. }
  87. av_init_packet(&pkt);
  88. while ((err = av_read_frame(fctx, &pkt)) >= 0) {
  89. int fd;
  90. snprintf(pktfilename, PATH_MAX-1, fntemplate, pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
  91. printf(PKTFILESUFF"\n", pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
  92. //printf("open(\"%s\")\n", pktfilename);
  93. if (!nowrite) {
  94. fd = open(pktfilename, O_WRONLY|O_CREAT, 0644);
  95. write(fd, pkt.data, pkt.size);
  96. close(fd);
  97. }
  98. pktnum++;
  99. if (maxpkts && (pktnum >= maxpkts))
  100. break;
  101. }
  102. while (dontquit)
  103. sleep(60);
  104. return 0;
  105. }