pktdumper.c 3.7 KB

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