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