Statistics
| Branch: | Revision:

ffmpeg / pktdumper.c @ 307eb248

History | View | Annotate | Download (2.81 KB)

1
#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
    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
    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
    return ret;
19
}
20

    
21
int main(int argc, char **argv)
22
{
23
    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
    int dontquit = 0;
30
    int nowrite = 0;
31
    int err;
32

    
33
    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
    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

    
61
    // register all file formats
62
    av_register_all();
63

    
64
    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

    
70
    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

    
76
    av_init_packet(&pkt);
77

    
78
    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
        if (!nowrite) {
84
            fd = open(pktfilename, O_WRONLY|O_CREAT, 0644);
85
            write(fd, pkt.data, pkt.size);
86
            close(fd);
87
        }
88
        pktnum++;
89
        if (maxpkts && (pktnum >= maxpkts))
90
            break;
91
    }
92

    
93
    while (dontquit)
94
        sleep(60);
95

    
96
    return 0;
97
}