2007-02-24 12:53:10 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-08-28 22:54:43 +03:00
|
|
|
#include "config.h"
|
2005-03-16 01:52:25 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-28 22:54:43 +03:00
|
|
|
#if HAVE_UNISTD_H
|
2005-03-16 01:52:25 +02:00
|
|
|
#include <unistd.h>
|
2012-08-28 22:54:43 +03:00
|
|
|
#endif
|
|
|
|
#if HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2005-03-16 01:52:25 +02:00
|
|
|
|
2012-08-30 23:10:07 +03:00
|
|
|
#include "libavutil/avstring.h"
|
2012-06-21 22:31:44 +03:00
|
|
|
#include "libavutil/time.h"
|
2008-08-21 00:58:05 +03:00
|
|
|
#include "libavformat/avformat.h"
|
2005-03-16 01:52:25 +02:00
|
|
|
|
2013-07-05 18:42:37 +03:00
|
|
|
#define FILENAME_BUF_SIZE 4096
|
2012-01-25 17:03:18 +03:00
|
|
|
#define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
|
2008-01-15 00:22:19 +02:00
|
|
|
|
2005-03-16 01:52:25 +02:00
|
|
|
static int usage(int ret)
|
|
|
|
{
|
2013-07-05 18:42:37 +03:00
|
|
|
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\n");
|
|
|
|
fprintf(stderr, "$(basename file.ext)_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
|
2005-04-07 19:52:28 +03:00
|
|
|
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");
|
2005-03-16 15:49:20 +02:00
|
|
|
return ret;
|
2005-03-16 01:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-08-28 22:56:03 +03:00
|
|
|
char fntemplate[FILENAME_BUF_SIZE];
|
|
|
|
char pktfilename[FILENAME_BUF_SIZE];
|
2011-10-28 19:36:54 +03:00
|
|
|
AVFormatContext *fctx = NULL;
|
2005-03-16 15:49:20 +02:00
|
|
|
AVPacket pkt;
|
2012-01-25 17:03:18 +03:00
|
|
|
int64_t pktnum = 0;
|
2005-03-16 15:49:20 +02:00
|
|
|
int64_t maxpkts = 0;
|
2012-01-25 17:03:18 +03:00
|
|
|
int donotquit = 0;
|
|
|
|
int nowrite = 0;
|
2005-03-16 15:49:20 +02:00
|
|
|
int err;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2005-04-07 19:52:28 +03:00
|
|
|
if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
|
|
|
|
if (strchr(argv[1], 'w'))
|
2007-06-12 12:29:25 +03:00
|
|
|
donotquit = 1;
|
2005-04-07 19:52:28 +03:00
|
|
|
if (strchr(argv[1], 'n'))
|
|
|
|
nowrite = 1;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
2005-03-16 15:49:20 +02:00
|
|
|
if (argc < 2)
|
|
|
|
return usage(1);
|
|
|
|
if (argc > 2)
|
|
|
|
maxpkts = atoi(argv[2]);
|
2012-08-30 23:10:07 +03:00
|
|
|
av_strlcpy(fntemplate, argv[1], sizeof(fntemplate));
|
2005-03-16 15:49:20 +02:00
|
|
|
if (strrchr(argv[1], '/'))
|
2012-08-30 23:10:07 +03:00
|
|
|
av_strlcpy(fntemplate, strrchr(argv[1], '/') + 1, sizeof(fntemplate));
|
2005-03-16 15:49:20 +02:00
|
|
|
if (strrchr(fntemplate, '.'))
|
|
|
|
*strrchr(fntemplate, '.') = '\0';
|
|
|
|
if (strchr(fntemplate, '%')) {
|
2013-07-05 18:42:37 +03:00
|
|
|
fprintf(stderr, "cannot use filenames containing '%%'\n");
|
2005-03-16 15:49:20 +02:00
|
|
|
return usage(1);
|
|
|
|
}
|
2012-08-30 23:08:06 +03:00
|
|
|
if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= sizeof(fntemplate) - 1) {
|
2005-03-16 15:49:20 +02:00
|
|
|
fprintf(stderr, "filename too long\n");
|
|
|
|
return usage(1);
|
|
|
|
}
|
|
|
|
strcat(fntemplate, PKTFILESUFF);
|
|
|
|
printf("FNTEMPLATE: '%s'\n", fntemplate);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-10-28 19:36:54 +03:00
|
|
|
err = avformat_open_input(&fctx, argv[1], NULL, NULL);
|
2005-03-16 15:49:20 +02:00
|
|
|
if (err < 0) {
|
2011-10-28 19:36:54 +03:00
|
|
|
fprintf(stderr, "cannot open input: error %d\n", err);
|
2005-03-16 15:49:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-08-26 18:40:07 +03:00
|
|
|
err = avformat_find_stream_info(fctx, NULL);
|
2005-03-16 15:49:20 +02:00
|
|
|
if (err < 0) {
|
2011-08-26 18:40:07 +03:00
|
|
|
fprintf(stderr, "avformat_find_stream_info: error %d\n", err);
|
2005-03-16 15:49:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2005-03-16 15:49:20 +02:00
|
|
|
av_init_packet(&pkt);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2005-03-16 15:49:20 +02:00
|
|
|
while ((err = av_read_frame(fctx, &pkt)) >= 0) {
|
|
|
|
int fd;
|
2012-08-30 23:08:06 +03:00
|
|
|
snprintf(pktfilename, sizeof(pktfilename), fntemplate, pktnum,
|
2012-01-25 17:03:18 +03:00
|
|
|
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' : '_');
|
2005-04-07 19:52:28 +03:00
|
|
|
if (!nowrite) {
|
2012-01-25 17:03:18 +03:00
|
|
|
fd = open(pktfilename, O_WRONLY | O_CREAT, 0644);
|
2011-05-15 19:34:11 +03:00
|
|
|
err = write(fd, pkt.data, pkt.size);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "write: error %d\n", err);
|
|
|
|
return 1;
|
|
|
|
}
|
2005-04-07 19:52:28 +03:00
|
|
|
close(fd);
|
|
|
|
}
|
2015-10-23 11:11:31 +02:00
|
|
|
av_packet_unref(&pkt);
|
2005-03-16 15:49:20 +02:00
|
|
|
pktnum++;
|
|
|
|
if (maxpkts && (pktnum >= maxpkts))
|
|
|
|
break;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-12-11 12:38:28 +03:00
|
|
|
avformat_close_input(&fctx);
|
2009-05-02 00:39:53 +03:00
|
|
|
|
2007-06-12 12:29:25 +03:00
|
|
|
while (donotquit)
|
2012-06-21 22:31:44 +03:00
|
|
|
av_usleep(60 * 1000000);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2005-03-16 15:49:20 +02:00
|
|
|
return 0;
|
2005-03-16 01:52:25 +02:00
|
|
|
}
|