pktdumper.c 2.8 KB

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