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