You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4KB

  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 file [maxpkts]\n");
  14. return ret;
  15. }
  16. int main(int argc, char **argv)
  17. {
  18. char fntemplate[PATH_MAX];
  19. char pktfilename[PATH_MAX];
  20. AVFormatContext *fctx;
  21. AVPacket pkt;
  22. int64_t pktnum = 0;
  23. int64_t maxpkts = 0;
  24. int err;
  25. if (argc < 2)
  26. return usage(1);
  27. if (argc > 2)
  28. maxpkts = atoi(argv[2]);
  29. strncpy(fntemplate, argv[1], PATH_MAX-1);
  30. if (strrchr(argv[1], '/'))
  31. strncpy(fntemplate, strrchr(argv[1], '/')+1, PATH_MAX-1);
  32. if (strrchr(fntemplate, '.'))
  33. *strrchr(fntemplate, '.') = '\0';
  34. if (strchr(fntemplate, '%')) {
  35. fprintf(stderr, "can't use filenames containing '%%'\n");
  36. return usage(1);
  37. }
  38. if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX-1) {
  39. fprintf(stderr, "filename too long\n");
  40. return usage(1);
  41. }
  42. strcat(fntemplate, PKTFILESUFF);
  43. printf("FNTEMPLATE: '%s'\n", fntemplate);
  44. // register all file formats
  45. av_register_all();
  46. err = av_open_input_file(&fctx, argv[1], NULL, 0, NULL);
  47. if (err < 0) {
  48. fprintf(stderr, "av_open_input_file: error %d\n", err);
  49. return 1;
  50. }
  51. err = av_find_stream_info(fctx);
  52. if (err < 0) {
  53. fprintf(stderr, "av_find_stream_info: error %d\n", err);
  54. return 1;
  55. }
  56. av_init_packet(&pkt);
  57. while ((err = av_read_frame(fctx, &pkt)) >= 0) {
  58. int fd;
  59. snprintf(pktfilename, PATH_MAX-1, fntemplate, pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
  60. printf(PKTFILESUFF"\n", pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
  61. //printf("open(\"%s\")\n", pktfilename);
  62. fd = open(pktfilename, O_WRONLY|O_CREAT, 0644);
  63. write(fd, pkt.data, pkt.size);
  64. close(fd);
  65. pktnum++;
  66. if (maxpkts && (pktnum >= maxpkts))
  67. break;
  68. }
  69. return 0;
  70. }