comparison TOOLS/subreader.c @ 223:7b7c7e26c3a7

added subreader by laaz
author arpi_esp
date Wed, 28 Mar 2001 02:27:29 +0000
parents
children
comparison
equal deleted inserted replaced
222:163bb3cee94d 223:7b7c7e26c3a7
1 /*
2 * Subtitle reader with format autodetection
3 * Mier nem muxik realloccal!?!?! - nekem muxik :)
4 *
5 * Written by laaz
6 * Some code cleanup & realloc() by A'rpi/ESP-team
7 */
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #define ERR (void *)-1
15 #define MAX_TEXT 5
16
17
18 int sub_uses_time=0;
19 int sub_errs=0;
20 int sub_num=0; // number of subtitle structs
21 int sub_format=-1; // 0 for microdvd, 1 for SubRip, 2 for the third format
22
23 struct subtitle {
24
25 int lines;
26
27 unsigned long start;
28 unsigned long end;
29
30 char *text[MAX_TEXT];
31 };
32
33 char *sub_readtext(char *source, char **dest) {
34 int len=0;
35 char *p;
36
37 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
38
39 *dest= (char *)malloc (len+1);
40 if (!dest) {return ERR;}
41
42 strncpy(*dest, source, len);
43 (*dest)[len]=0;
44
45 while (*p=='\r' || *p=='\n' || *p=='|') p++;
46
47 if (*p) return p; // not-last text field
48 else return NULL; // last text field
49 }
50
51
52
53 struct subtitle *sub_read_line_microdvd(FILE *fd,struct subtitle *current) {
54 char line[1001];
55 char line2[1001];
56 char *p, *next;
57 int i;
58
59 bzero (current, sizeof(current));
60
61 do {
62 if (!fgets (line, 1000, fd)) return NULL;
63 } while (*line=='\n' || *line == '\r' || !*line);
64
65 if (sscanf (line, "{%i}{%i}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
66
67 p=line;
68 while (*p++!='}');
69 while (*p++!='}');
70
71 next=p, i=0;
72 while ((next =sub_readtext (next, &(current->text[i])))) {
73 if (current->text[i]==ERR || current->text[i]==ERR) {return ERR;}
74 i++;
75 if (i>MAX_TEXT) { printf ("Too many lines in a subtitle\n");return ERR;}
76 }
77 current->lines=i+1;
78
79 return current;
80 }
81
82 struct subtitle *sub_read_line_subrip(FILE *fd, struct subtitle *current) {
83 char line[1001];
84 int a1,a2,a3,a4,b1,b2,b3,b4;
85 char *p=NULL, *q=NULL;
86 int len;
87
88 bzero (current, sizeof(current));
89
90 while (!current->text[0]) {
91 if (!fgets (line, 1000, fd)) return NULL;
92 if (sscanf (line, "%i:%i:%i.%i,%i:%i:%i.%i",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
93 current->start = a1*360000+a2*6000+a3*100+a4;
94 current->end = b1*360000+b2*6000+b3*100+b4;
95
96 if (!fgets (line, 1000, fd)) return NULL;
97
98 p=q=line;
99 for (current->lines=1; current->lines < MAX_TEXT; current->lines++) {
100 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++);
101 current->text[current->lines-1]=(char *)malloc (len+1);
102 if (!current->text[current->lines-1]) return ERR;
103 strncpy (current->text[current->lines-1], q, len);
104 if (!*p || *p=='\r' || *p=='\n') break;
105 while (*p++!=']');
106 }
107 }
108 return current;
109 }
110
111 struct subtitle *sub_read_line_third(FILE *fd,struct subtitle *current) {
112 char line[1001];
113 int a1,a2,a3,a4,b1,b2,b3,b4;
114 char *p=NULL;
115 int i,len;
116
117 bzero (current, sizeof(current));
118
119 while (!current->text[0]) {
120 if (!fgets (line, 1000, fd)) return NULL;
121 if ((len=sscanf (line, "%i:%i:%i,%i --> %i:%i:%i,%i",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
122 continue;
123 current->start = a1*360000+a2*6000+a3*100+a4/10;
124 current->end = b1*360000+b2*6000+b3*100+b4/10;
125 for (i=0; i<MAX_TEXT;) {
126 if (!fgets (line, 1000, fd)) return NULL;
127 len=0;
128 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
129 if (len) {
130 current->text[i]=(char *)malloc (len+1);
131 if (!current->text[i]) return ERR;
132 strncpy (current->text[i], line, len);
133 i++;
134 } else {
135 break;
136 }
137 }
138 current->lines=i;
139 }
140 return current;
141 }
142
143
144 int sub_autodetect (FILE *fd) {
145 char line[1001];
146 int i,j=0;
147 // char *p;
148
149 while (1) {
150 j++;
151 if (!fgets (line, 1000, fd))
152 return -1;
153
154 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
155 if (sscanf (line, "{%i}{%i}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
156 {sub_uses_time=0;return 0;}
157 if (sscanf (line, "%i:%i:%i.%i,%i:%i:%i.%i", &i, &i, &i, &i, &i, &i, &i, &i)==8)
158 {sub_uses_time=1;return 1;}
159 if (sscanf (line, "%i:%i:%i,%i --> %i:%i:%i,%i", &i, &i, &i, &i, &i, &i, &i, &i)==8)
160 {sub_uses_time=1;return 2;}
161 if (j>100) return -1; // too many bad lines or bad coder
162 }
163 }
164
165
166 struct subtitle * sub_get_subtitles (char *filename) {
167 FILE *fd;
168 int n_max;
169 struct subtitle *first;
170 struct subtitle * (*func[3])(FILE *fd,struct subtitle *dest)=
171 {
172 sub_read_line_microdvd,
173 sub_read_line_subrip,
174 sub_read_line_third
175 };
176
177 fd=fopen (filename, "r"); if (!fd) return NULL;
178
179 sub_format=sub_autodetect (fd);
180 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
181 printf ("SUB: Detected subtitle file format: %i\n",sub_format);
182
183 rewind (fd);
184
185 sub_num=0;n_max=32;
186 first=(struct subtitle *)malloc(n_max*sizeof(struct subtitle));
187 if(!first) return NULL;
188
189 while(1){
190 struct subtitle *sub;
191 if(sub_num>=n_max){
192 n_max+=16;
193 first=realloc(first,n_max*sizeof(struct subtitle));
194 }
195 sub=func[sub_format](fd,&first[sub_num]);
196 if(!sub) break; // EOF
197 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
198 }
199
200 fclose(fd);
201
202 return first;
203 }
204
205
206 int main(int argc, char **argv) { // for testing
207
208 int i,j;
209 struct subtitle *subs;
210 struct subtitle *egysub;
211
212 if(argc<2){
213 printf("\nUsage: subreader filename.sub\n\n");
214 exit(1);
215 }
216
217 subs=sub_get_subtitles(argv[1]);
218 if(!subs){
219 printf("Couldn't load file... let's write a bugreport :)\n");
220 exit(1);
221 }
222
223 for(j=0;j<sub_num;j++){
224 egysub=&subs[j];
225 printf ("%i line%c (%i-%i) ",
226 egysub->lines,
227 (1==egysub->lines)?' ':'s',
228 egysub->start,
229 egysub->end);
230 for (i=0; i<egysub->lines; i++) {
231 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
232 }
233 printf ("\n");
234 }
235
236 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
237 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
238 return 0;
239 }