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