tools/pktdumper.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Francois Revol
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 
00028 #include "libavformat/avformat.h"
00029 
00030 #define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
00031 
00032 static int usage(int ret)
00033 {
00034     fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
00035     fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
00036     fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
00037     fprintf(stderr, "-n\twrite No file at all, only demux.\n");
00038     fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
00039     return ret;
00040 }
00041 
00042 int main(int argc, char **argv)
00043 {
00044     char fntemplate[PATH_MAX];
00045     char pktfilename[PATH_MAX];
00046     AVFormatContext *fctx = NULL;
00047     AVPacket pkt;
00048     int64_t pktnum  = 0;
00049     int64_t maxpkts = 0;
00050     int donotquit   = 0;
00051     int nowrite     = 0;
00052     int err;
00053 
00054     if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
00055         if (strchr(argv[1], 'w'))
00056             donotquit = 1;
00057         if (strchr(argv[1], 'n'))
00058             nowrite = 1;
00059         argv++;
00060         argc--;
00061     }
00062     if (argc < 2)
00063         return usage(1);
00064     if (argc > 2)
00065         maxpkts = atoi(argv[2]);
00066     strncpy(fntemplate, argv[1], PATH_MAX - 1);
00067     if (strrchr(argv[1], '/'))
00068         strncpy(fntemplate, strrchr(argv[1], '/') + 1, PATH_MAX - 1);
00069     if (strrchr(fntemplate, '.'))
00070         *strrchr(fntemplate, '.') = '\0';
00071     if (strchr(fntemplate, '%')) {
00072         fprintf(stderr, "can't use filenames containing '%%'\n");
00073         return usage(1);
00074     }
00075     if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX - 1) {
00076         fprintf(stderr, "filename too long\n");
00077         return usage(1);
00078     }
00079     strcat(fntemplate, PKTFILESUFF);
00080     printf("FNTEMPLATE: '%s'\n", fntemplate);
00081 
00082     // register all file formats
00083     av_register_all();
00084 
00085     err = avformat_open_input(&fctx, argv[1], NULL, NULL);
00086     if (err < 0) {
00087         fprintf(stderr, "cannot open input: error %d\n", err);
00088         return 1;
00089     }
00090 
00091     err = avformat_find_stream_info(fctx, NULL);
00092     if (err < 0) {
00093         fprintf(stderr, "avformat_find_stream_info: error %d\n", err);
00094         return 1;
00095     }
00096 
00097     av_init_packet(&pkt);
00098 
00099     while ((err = av_read_frame(fctx, &pkt)) >= 0) {
00100         int fd;
00101         snprintf(pktfilename, PATH_MAX - 1, fntemplate, pktnum,
00102                  pkt.stream_index, pkt.pts, pkt.size,
00103                  (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
00104         printf(PKTFILESUFF "\n", pktnum, pkt.stream_index, pkt.pts, pkt.size,
00105                (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
00106         //printf("open(\"%s\")\n", pktfilename);
00107         if (!nowrite) {
00108             fd  = open(pktfilename, O_WRONLY | O_CREAT, 0644);
00109             err = write(fd, pkt.data, pkt.size);
00110             if (err < 0) {
00111                 fprintf(stderr, "write: error %d\n", err);
00112                 return 1;
00113             }
00114             close(fd);
00115         }
00116         av_free_packet(&pkt);
00117         pktnum++;
00118         if (maxpkts && (pktnum >= maxpkts))
00119             break;
00120     }
00121 
00122     avformat_close_input(&fctx);
00123 
00124     while (donotquit)
00125         usleep(60 * 1000000);
00126 
00127     return 0;
00128 }