00001 #include <avformat.h>
00002 #include <limits.h>
00003 #include <fcntl.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008
00009 #define PKTFILESUFF "_%08Ld_%02d_%010Ld_%06d_%c.bin"
00010
00011 static int usage(int ret)
00012 {
00013 fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
00014 fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
00015 fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
00016 fprintf(stderr, "-n\twrite No file at all, only demux.\n");
00017 fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
00018 return ret;
00019 }
00020
00021 int main(int argc, char **argv)
00022 {
00023 char fntemplate[PATH_MAX];
00024 char pktfilename[PATH_MAX];
00025 AVFormatContext *fctx;
00026 AVPacket pkt;
00027 int64_t pktnum = 0;
00028 int64_t maxpkts = 0;
00029 int dontquit = 0;
00030 int nowrite = 0;
00031 int err;
00032
00033 if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
00034 if (strchr(argv[1], 'w'))
00035 dontquit = 1;
00036 if (strchr(argv[1], 'n'))
00037 nowrite = 1;
00038 argv++;
00039 argc--;
00040 }
00041 if (argc < 2)
00042 return usage(1);
00043 if (argc > 2)
00044 maxpkts = atoi(argv[2]);
00045 strncpy(fntemplate, argv[1], PATH_MAX-1);
00046 if (strrchr(argv[1], '/'))
00047 strncpy(fntemplate, strrchr(argv[1], '/')+1, PATH_MAX-1);
00048 if (strrchr(fntemplate, '.'))
00049 *strrchr(fntemplate, '.') = '\0';
00050 if (strchr(fntemplate, '%')) {
00051 fprintf(stderr, "can't use filenames containing '%%'\n");
00052 return usage(1);
00053 }
00054 if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX-1) {
00055 fprintf(stderr, "filename too long\n");
00056 return usage(1);
00057 }
00058 strcat(fntemplate, PKTFILESUFF);
00059 printf("FNTEMPLATE: '%s'\n", fntemplate);
00060
00061
00062 av_register_all();
00063
00064 err = av_open_input_file(&fctx, argv[1], NULL, 0, NULL);
00065 if (err < 0) {
00066 fprintf(stderr, "av_open_input_file: error %d\n", err);
00067 return 1;
00068 }
00069
00070 err = av_find_stream_info(fctx);
00071 if (err < 0) {
00072 fprintf(stderr, "av_find_stream_info: error %d\n", err);
00073 return 1;
00074 }
00075
00076 av_init_packet(&pkt);
00077
00078 while ((err = av_read_frame(fctx, &pkt)) >= 0) {
00079 int fd;
00080 snprintf(pktfilename, PATH_MAX-1, fntemplate, pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
00081 printf(PKTFILESUFF"\n", pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
00082
00083 if (!nowrite) {
00084 fd = open(pktfilename, O_WRONLY|O_CREAT, 0644);
00085 write(fd, pkt.data, pkt.size);
00086 close(fd);
00087 }
00088 pktnum++;
00089 if (maxpkts && (pktnum >= maxpkts))
00090 break;
00091 }
00092
00093 while (dontquit)
00094 sleep(60);
00095
00096 return 0;
00097 }