Statistics
| Branch: | Revision:

ffmpeg / pktdumper.c @ c04c3282

History | View | Annotate | Download (2.81 KB)

1 0fa04b7f François Revol
#include <avformat.h>
2
#include <limits.h>
3
#include <fcntl.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <unistd.h>
8
9
#define PKTFILESUFF "_%08Ld_%02d_%010Ld_%06d_%c.bin"
10
11
static int usage(int ret)
12
{
13 15b34f63 François Revol
    fprintf(stderr, "dump (up to maxpkts) AVPackets as they are demuxed by libavformat.\n");
14
    fprintf(stderr, "each packet is dumped in its own file named like `basename file.ext`_$PKTNUM_$STREAMINDEX_$STAMP_$SIZE_$FLAGS.bin\n");
15 2d216336 François Revol
    fprintf(stderr, "pktdumper [-nw] file [maxpkts]\n");
16
    fprintf(stderr, "-n\twrite No file at all, only demux.\n");
17
    fprintf(stderr, "-w\tWait at end of processing instead of quitting.\n");
18 15b34f63 François Revol
    return ret;
19 0fa04b7f François Revol
}
20
21
int main(int argc, char **argv)
22
{
23 15b34f63 François Revol
    char fntemplate[PATH_MAX];
24
    char pktfilename[PATH_MAX];
25
    AVFormatContext *fctx;
26
    AVPacket pkt;
27
    int64_t pktnum = 0;
28
    int64_t maxpkts = 0;
29 2d216336 François Revol
    int dontquit = 0;
30
    int nowrite = 0;
31 15b34f63 François Revol
    int err;
32 115329f1 Diego Biurrun
33 2d216336 François Revol
    if ((argc > 1) && !strncmp(argv[1], "-", 1)) {
34
        if (strchr(argv[1], 'w'))
35
            dontquit = 1;
36
        if (strchr(argv[1], 'n'))
37
            nowrite = 1;
38
        argv++;
39
        argc--;
40
    }
41 15b34f63 François Revol
    if (argc < 2)
42
        return usage(1);
43
    if (argc > 2)
44
        maxpkts = atoi(argv[2]);
45
    strncpy(fntemplate, argv[1], PATH_MAX-1);
46
    if (strrchr(argv[1], '/'))
47
        strncpy(fntemplate, strrchr(argv[1], '/')+1, PATH_MAX-1);
48
    if (strrchr(fntemplate, '.'))
49
        *strrchr(fntemplate, '.') = '\0';
50
    if (strchr(fntemplate, '%')) {
51
        fprintf(stderr, "can't use filenames containing '%%'\n");
52
        return usage(1);
53
    }
54
    if (strlen(fntemplate) + sizeof(PKTFILESUFF) >= PATH_MAX-1) {
55
        fprintf(stderr, "filename too long\n");
56
        return usage(1);
57
    }
58
    strcat(fntemplate, PKTFILESUFF);
59
    printf("FNTEMPLATE: '%s'\n", fntemplate);
60 115329f1 Diego Biurrun
61 15b34f63 François Revol
    // register all file formats
62
    av_register_all();
63 115329f1 Diego Biurrun
64 15b34f63 François Revol
    err = av_open_input_file(&fctx, argv[1], NULL, 0, NULL);
65
    if (err < 0) {
66
        fprintf(stderr, "av_open_input_file: error %d\n", err);
67
        return 1;
68
    }
69 115329f1 Diego Biurrun
70 15b34f63 François Revol
    err = av_find_stream_info(fctx);
71
    if (err < 0) {
72
        fprintf(stderr, "av_find_stream_info: error %d\n", err);
73
        return 1;
74
    }
75 115329f1 Diego Biurrun
76 15b34f63 François Revol
    av_init_packet(&pkt);
77 115329f1 Diego Biurrun
78 15b34f63 François Revol
    while ((err = av_read_frame(fctx, &pkt)) >= 0) {
79
        int fd;
80
        snprintf(pktfilename, PATH_MAX-1, fntemplate, pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
81
        printf(PKTFILESUFF"\n", pktnum, pkt.stream_index, pkt.pts, pkt.size, (pkt.flags & PKT_FLAG_KEY)?'K':'_');
82
        //printf("open(\"%s\")\n", pktfilename);
83 2d216336 François Revol
        if (!nowrite) {
84
            fd = open(pktfilename, O_WRONLY|O_CREAT, 0644);
85
            write(fd, pkt.data, pkt.size);
86
            close(fd);
87
        }
88 15b34f63 François Revol
        pktnum++;
89
        if (maxpkts && (pktnum >= maxpkts))
90
            break;
91
    }
92 115329f1 Diego Biurrun
93 2d216336 François Revol
    while (dontquit)
94
        sleep(60);
95 115329f1 Diego Biurrun
96 15b34f63 François Revol
    return 0;
97 0fa04b7f François Revol
}