• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

tools/pktdumper.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Francois Revol
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav 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  * Libav 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 Libav; 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 #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 = NULL;
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     // register all file formats
00084     av_register_all();
00085 
00086     err = avformat_open_input(&fctx, argv[1], NULL, NULL);
00087     if (err < 0) {
00088         fprintf(stderr, "cannot open input: error %d\n", err);
00089         return 1;
00090     }
00091 
00092     err = avformat_find_stream_info(fctx, NULL);
00093     if (err < 0) {
00094         fprintf(stderr, "avformat_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         //printf("open(\"%s\")\n", pktfilename);
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     avformat_close_input(&fctx);
00121 
00122     while (donotquit)
00123         sleep(60);
00124 
00125     return 0;
00126 }
Generated on Thu Jul 11 2013 15:38:25 for Libav by doxygen 1.7.1