-
Notifications
You must be signed in to change notification settings - Fork 0
/
mark-commercial.c
178 lines (164 loc) · 5.18 KB
/
mark-commercial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __unix__
# include <unistd.h>
#endif
#ifdef WIN32
# include "gnu_getopt\getopt.h"
#endif
#include "rtv.h"
#include "ndx.h"
static int handle(FILE * in, FILE * out, u32 start, u32 end, int add, int flags)
{
struct ndx_record this;
u64 basetime = 0;
double seconds;
u32 recno = 0;
while (read_ndx_record(in, &this) > 0) {
switch (recno) {
case 0: /* header, passthrough */
break;
case 1: /* need to grab the base of the timestamp */
basetime = this.timestamp;
/* fallthrough */
default:
seconds = ((s64)(this.timestamp - basetime))/1000000000.0;
if (seconds >= start && seconds <= end) {
if (add)
this.commercial_flag |= flags;
else
this.commercial_flag &= ~flags;
}
break;
}
write_ndx_record(out, &this);
recno++;
}
return 0;
}
static void usage(const char * str)
{
if (str)
fprintf(stderr, "*** ERROR: %s\n\n", str);
fprintf(stderr,
"Usage: commercial [-i <i>] [-o <o>] [-s <s>] [-e <e>] [-r] [filename]\n"
" -i -- input filename\n"
" -o -- output filename\n"
" -s -- start time, in seconds\n"
" -e -- end time, in seconds\n"
" -r -- remove rather than add\n"
"\n"
"If no files are given, reads from stdin and writes to stdout\n"
"If [filename] is given, it's used for both input and output\n"
"If -r is given with no start or end time, all commercial breaks are removed\n"
"If -r is not given, start and end time must be given\n"
);
exit(str ? 1 : 0);
}
static void error(const char * str)
{
fprintf(stderr, "*** ERROR: %s\n", str);
exit(1);
}
int main(int argc, char ** argv)
{
FILE * in = NULL,
* out = NULL;
u32 start = 0,
end = 0;
int start_set = 0,
end_set = 0,
add = 1,
flags = 0x01,
ch;
char * p;
while ((ch = getopt(argc, argv, "i:o:s:e:rhf:")) != EOF) {
switch(ch) {
case 'i':
if (in)
usage("Can only specify input once");
in = fopen(optarg, "r");
if (!in)
error("Couldn't open input file");
break;
case 'o':
if (out)
usage("Can only specify output once");
out = fopen(optarg, "w");
if (!out)
error("Couldn't open output file");
break;
case 's':
if (start_set)
usage("Can only specify one start time");
start = strtoul(optarg, &p, 0);
if (*p != 0)
usage("Start time must be numeric");
start_set = 1;
break;
case 'e':
if (end_set)
usage("Can only specify one end time");
end = strtoul(optarg, &p, 0);
if (*p != 0)
usage("End time must be numeric");
end_set = 1;
break;
case 'r':
if (!add)
usage("Only use -r once");
add = 0;
break;
case 'h':
usage(NULL);
break;
case 'f':
flags = strtoul(optarg, NULL, 0);
break;
default:
usage("");
}
}
if (argc - optind > 1)
usage("If input and output are separate files, must use -i and -o");
if (argc - optind == 0) {
if (!in)
in = stdin;
if (!out)
out = stdout;
}
if (argc - optind == 1) {
if (in || out)
usage("If you use -i or -o, use both or std{in,out}");
in = fopen(argv[optind], "r");
if (!in)
error("Couldn't open file to read");
out = fopen(argv[optind], "r+");
if (!out)
error("Couldn't open file to write");
}
if ((start_set && !end_set) || (end_set && !start_set))
usage("If you specify start or end, specify both");
if (flags == 0x01 && add && !start_set && !end_set)
usage("It doesn't make much sense to set the whole stream to commercial");
if (!start_set)
start = 0;
if (!end_set)
end = 0xffffffff;
return handle(in, out, start, end, add, flags);
}