-
Notifications
You must be signed in to change notification settings - Fork 9
/
epgdump.c
214 lines (189 loc) · 5.7 KB
/
epgdump.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <iconv.h>
#include <time.h>
#include "ts.h"
#include "sdt.h"
#include "eit.h"
#include "ts_ctl.h"
#include "xmldata.c"
SVT_CONTROL *svttop = NULL;
#define SECCOUNT 64
char title[1024];
char subtitle[1024];
char Category[1024];
char ServiceName[1024];
/* prototype */
extern int strrep(char *buf, char *mae, char *ato);
void xmlspecialchars(char *str)
{
strrep(str, "&", "&");
strrep(str, "'", "'");
strrep(str, "\"", """);
strrep(str, "<", "<");
strrep(str, ">", ">");
}
void GetEITSDTInfo(FILE *infile, SECcache *secs, int count)
{
SECcache *bsecs;
SVT_CONTROL *svtcur;
int pid;
while((bsecs = readTS(infile, secs, count)) != NULL) {
pid = bsecs->pid & 0xFF;
switch (pid) {
case 0x11: // SDT
dumpSDT(bsecs->buf, svttop);
svtcur = svttop->next;
while(svtcur) {
if (svtcur->eit == NULL) {
svtcur->eit = calloc(1, sizeof(EIT_CONTROL));
}
svtcur = svtcur->next;
}
break;
case 0x12: // EIT
case 0x26: // EIT(地デジ)
case 0x27: // EIT(地デジ)
dumpEIT(bsecs->buf, svttop);
break;
}
}
}
void dumpXML(FILE *outfile, char *header)
{
SVT_CONTROL *svtcur ;
EIT_CONTROL *eitcur ;
char cendtime[32];
char cstarttime[32];
fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
svtcur = svttop->next;
while(svtcur != NULL) {
if (!svtcur->haveeitschedule) {
svtcur = svtcur->next;
continue;
}
memset(ServiceName, '\0', sizeof(ServiceName));
strcpy(ServiceName, svtcur->service_name);
xmlspecialchars(ServiceName);
fprintf(outfile, " <channel id=\"%s_%d\" tp=\"%s\">\n", (svtcur->original_network_id < 0x10) ? header : "GR", svtcur->service_id, (svtcur->original_network_id < 0x10) ? getTP(svtcur->transport_stream_id) : header);
fprintf(outfile, " <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
fprintf(outfile, " <transport_stream_id>%d</transport_stream_id>\n", svtcur->transport_stream_id);
fprintf(outfile, " <original_network_id>%d</original_network_id>\n", svtcur->original_network_id);
fprintf(outfile, " <service_id>%d</service_id>\n", svtcur->service_id);
fprintf(outfile, " </channel>\n");
svtcur = svtcur->next;
}
svtcur = svttop->next;
while(svtcur != NULL) {
if (!svtcur->haveeitschedule) {
svtcur = svtcur->next;
continue;
}
eitcur = svtcur->eit;
while(eitcur != NULL){
if(!eitcur->service_id){
eitcur = eitcur->next ;
continue ;
}
if(eitcur->content_type > CAT_COUNT){
eitcur->content_type = CAT_COUNT -1 ;
}
memset(title, '\0', sizeof(title));
strcpy(title, eitcur->title);
xmlspecialchars(title);
memset(subtitle, '\0', sizeof(subtitle));
strcpy(subtitle, eitcur->subtitle);
xmlspecialchars(subtitle);
memset(Category, '\0', sizeof(Category));
strcpy(Category, ContentCatList[eitcur->content_type].japanese);
xmlspecialchars(Category);
memset(cendtime, '\0', sizeof(cendtime));
memset(cstarttime, '\0', sizeof(cstarttime));
strcpy(cendtime, strTime(eitcur->start_time + eitcur->duration , "%Y%m%d%H%M%S"));
strcpy(cstarttime, strTime(eitcur->start_time, "%Y%m%d%H%M%S"));
fprintf(outfile, " <programme start=\"%s +0900\" stop=\"%s +0900\" channel=\"%s_%d\" event_id=\"%d\">\n",
cstarttime, cendtime, (svtcur->original_network_id < 0x10) ? header : "GR", svtcur->service_id, eitcur->event_id);
fprintf(outfile, " <title lang=\"ja_JP\">%s</title>\n", title);
fprintf(outfile, " <desc lang=\"ja_JP\">%s</desc>\n", subtitle);
fprintf(outfile, " <category lang=\"ja_JP\">%s</category>\n", Category);
fprintf(outfile, " <category lang=\"en\">%s</category>\n", ContentCatList[eitcur->content_type].english);
fprintf(outfile, " </programme>\n");
eitcur = eitcur->next;
}
svtcur = svtcur->next;
}
fprintf(outfile, "</tv>\n");
}
int main(int argc, char *argv[])
{
FILE *infile = stdin;
FILE *outfile = stdout;
char *arg_onTV ;
int staCount ;
int inclose = 0;
int outclose = 0;
SECcache secs[SECCOUNT];
char *ontvheader ;
if(argc == 4){
arg_onTV = argv[1];
if(strcmp(argv[2], "-")) {
infile = fopen(argv[2], "r");
if ( !infile) {
printf( "tsFile not found (Can't open file: %s)\n", argv[2] );
exit( -1 );
}
inclose = 1;
}
else {
infile = stdin;
}
if(strcmp(argv[3], "-")) {
outfile = fopen(argv[3], "w+");
if ( !outfile) {
printf( "xmlFile not found (Can't open file: %s)\n", argv[3] );
exit( -1 );
}
outclose = 1;
}
else {
outfile = stdout;
}
}else{
fprintf(stdout, "Usage : %s {/BS|/CS|<id>} <tsFile> <outfile>\n", argv[0]);
fprintf(stdout, "\n");
fprintf(stdout, "id チャンネル識別子。地上波の物理チャンネルを与えます。\n");
fprintf(stdout, "/BS BSモード。一つのTSからBS全局のデータを読み込みます。\n");
fprintf(stdout, "/CS CSモード。一つのTSから複数局のデータを読み込みます。\n");
return 0;
}
staCount = 0;
svttop = calloc(1, sizeof(SVT_CONTROL));
/* 興味のあるpidを指定 */
memset(secs, 0, sizeof(SECcache) * SECCOUNT);
secs[0].pid = 0x10;
secs[1].pid = 0x11;
secs[2].pid = 0x12;
secs[3].pid = 0x26;
secs[4].pid = 0x27;
GetEITSDTInfo(infile, secs, SECCOUNT);
if(strcmp(arg_onTV, "/BS") == 0){
ontvheader = "BS";
}else if(strcmp(arg_onTV, "/CS") == 0){
ontvheader = "CS";
}else{
ontvheader = arg_onTV;
}
dumpXML(outfile, ontvheader);
if(inclose) {
fclose(infile);
}
if(outclose) {
fclose(outfile);
}
return 0;
}