9 #define PKTFILESUFF "_%08Ld_%02d_%010Ld_%06d_%c.bin"
11 static int usage(int ret
)
13 fprintf(stderr
, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
14 fprintf(stderr
, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
15 fprintf(stderr
, "pktdumper [-nw] file [maxpkts]\n");
16 fprintf(stderr
, "-n\twrite No file at all, only demux.\n");
17 fprintf(stderr
, "-w\tWait at end of processing instead of quitting.\n");
21 int main(int argc
, char **argv
)
23 char fntemplate
[PATH_MAX
];
24 char pktfilename
[PATH_MAX
];
25 AVFormatContext
*fctx
;
33 if ((argc
> 1) && !strncmp(argv
[1], "-", 1)) {
34 if (strchr(argv
[1], 'w'))
36 if (strchr(argv
[1], 'n'))
44 maxpkts
= atoi(argv
[2]);
45 strncpy(fntemplate
, argv
[1], PATH_MAX
-1);
46 if (strrchr(argv
[1], '/'))
47 strncpy(fntemplate
, strrchr(argv
[1], '/')+1, PATH_MAX
-1);
48 if (strrchr(fntemplate
, '.'))
49 *strrchr(fntemplate
, '.') = '\0';
50 if (strchr(fntemplate
, '%')) {
51 fprintf(stderr
, "can't use filenames containing '%%'\n");
54 if (strlen(fntemplate
) + sizeof(PKTFILESUFF
) >= PATH_MAX
-1) {
55 fprintf(stderr
, "filename too long\n");
58 strcat(fntemplate
, PKTFILESUFF
);
59 printf("FNTEMPLATE: '%s'\n", fntemplate
);
61 // register all file formats
64 err
= av_open_input_file(&fctx
, argv
[1], NULL
, 0, NULL
);
66 fprintf(stderr
, "av_open_input_file: error %d\n", err
);
70 err
= av_find_stream_info(fctx
);
72 fprintf(stderr
, "av_find_stream_info: error %d\n", err
);
78 while ((err
= av_read_frame(fctx
, &pkt
)) >= 0) {
80 snprintf(pktfilename
, PATH_MAX
-1, fntemplate
, pktnum
, pkt
.stream_index
, pkt
.pts
, pkt
.size
, (pkt
.flags
& PKT_FLAG_KEY
)?'K':'_');
81 printf(PKTFILESUFF
"\n", pktnum
, pkt
.stream_index
, pkt
.pts
, pkt
.size
, (pkt
.flags
& PKT_FLAG_KEY
)?'K':'_');
82 //printf("open(\"%s\")\n", pktfilename);
84 fd
= open(pktfilename
, O_WRONLY
|O_CREAT
, 0644);
85 write(fd
, pkt
.data
, pkt
.size
);
89 if (maxpkts
&& (pktnum
>= maxpkts
))