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