tools/pktdumper.c
15ea54d5
 /*
  * Copyright (c) 2005 Francois Revol
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
212ec5fa
 #include "config.h"
0fa04b7f
 #include <limits.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
212ec5fa
 #if HAVE_UNISTD_H
0fa04b7f
 #include <unistd.h>
212ec5fa
 #endif
 #if HAVE_IO_H
 #include <io.h>
 #endif
0fa04b7f
 
21411a41
 #define FILENAME_BUF_SIZE 4096
 
bcc44873
 #include "libavutil/avstring.h"
896bb0d7
 #include "libavutil/time.h"
3ef61825
 #include "libavformat/avformat.h"
0fa04b7f
 
4e81b5f5
 #define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
6878026a
 
0fa04b7f
 static int usage(int ret)
 {
15b34f63
     fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
     fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
2d216336
     fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
     fprintf(stderr, "-n\twrite No file at all, only demux.\n");
     fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
15b34f63
     return ret;
0fa04b7f
 }
 
 int main(int argc, char **argv)
 {
21411a41
     char fntemplate[FILENAME_BUF_SIZE];
     char pktfilename[FILENAME_BUF_SIZE];
d830264a
     AVFormatContext *fctx = NULL;
15b34f63
     AVPacket pkt;
4e81b5f5
     int64_t pktnum  = 0;
15b34f63
     int64_t maxpkts = 0;
4e81b5f5
     int donotquit   = 0;
     int nowrite     = 0;
15b34f63
     int err;
115329f1
 
2d216336
     if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
         if (strchr(argv[1], 'w'))
755bfeab
             donotquit = 1;
2d216336
         if (strchr(argv[1], 'n'))
             nowrite = 1;
         argv++;
         argc--;
     }
15b34f63
     if (argc < 2)
         return usage(1);
     if (argc > 2)
         maxpkts = atoi(argv[2]);
bcc44873
     av_strlcpy(fntemplate, argv[1], sizeof(fntemplate));
15b34f63
     if (strrchr(argv[1], '/'))
bcc44873
         av_strlcpy(fntemplate, strrchr(argv[1], '/') + 1, sizeof(fntemplate));
15b34f63
     if (strrchr(fntemplate, '.'))
         *strrchr(fntemplate, '.') = '\0';
     if (strchr(fntemplate, '%')) {
         fprintf(stderr, "can't use filenames containing '%%'\n");
         return usage(1);
     }
372de27d
     if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= sizeof(fntemplate) - 1) {
15b34f63
         fprintf(stderr, "filename too long\n");
         return usage(1);
     }
     strcat(fntemplate, PKTFILESUFF);
     printf("FNTEMPLATE: '%s'\n", fntemplate);
115329f1
 
15b34f63
     // register all file formats
     av_register_all();
115329f1
 
d830264a
     err = avformat_open_input(&fctx, argv[1], NULL, NULL);
15b34f63
     if (err < 0) {
d830264a
         fprintf(stderr, "cannot open input: error %d\n", err);
15b34f63
         return 1;
     }
115329f1
 
c960e67a
     err = avformat_find_stream_info(fctx, NULL);
15b34f63
     if (err < 0) {
c960e67a
         fprintf(stderr, "avformat_find_stream_info: error %d\n", err);
15b34f63
         return 1;
     }
115329f1
 
15b34f63
     av_init_packet(&pkt);
115329f1
 
15b34f63
     while ((err = av_read_frame(fctx, &pkt)) >= 0) {
         int fd;
372de27d
         snprintf(pktfilename, sizeof(pktfilename), fntemplate, pktnum,
4e81b5f5
                  pkt.stream_index, pkt.pts, pkt.size,
                  (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
         printf(PKTFILESUFF "\n", pktnum, pkt.stream_index, pkt.pts, pkt.size,
                (pkt.flags & AV_PKT_FLAG_KEY) ? 'K' : '_');
2d216336
         if (!nowrite) {
4e81b5f5
             fd  = open(pktfilename, O_WRONLY | O_CREAT, 0644);
d39facc7
             err = write(fd, pkt.data, pkt.size);
             if (err < 0) {
                 fprintf(stderr, "write: error %d\n", err);
                 return 1;
             }
2d216336
             close(fd);
         }
92147b6b
         av_free_packet(&pkt);
15b34f63
         pktnum++;
         if (maxpkts && (pktnum >= maxpkts))
             break;
     }
115329f1
 
cd3716b9
     avformat_close_input(&fctx);
92147b6b
 
755bfeab
     while (donotquit)
896bb0d7
         av_usleep(60 * 1000000);
115329f1
 
15b34f63
     return 0;
0fa04b7f
 }