Debian Codegraph @

lib/subtitle/subwrite.c

raw
1 #include "subtitle.h"
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include <pthread.h>
8
9 int subtitle_write(const subtitles_t* st, const char* filename, subtitle_t type)
10 {
11 const char* nl = "\r\n";
12 FILE* file = fopen(filename, "wt");
13
14 if (!file)
15 return -1;
16
17 if (st->subtitle)
18 {
19 unsigned int i;
20 for (i = 0; i < st->count; ++i)
21 {
22 unsigned int t1 = st->subtitle[i].start;
23 unsigned int t2 = st->subtitle[i].end;
24
25 unsigned int hh1 = (t1/60/60/1000);
26 unsigned int mm1 = (t1/60/1000)%60;
27 unsigned int ss1 = (t1/1000)%60;
28 unsigned int ms1 = (t1)%1000;
29 unsigned int hh2 = (t2/60/60/1000);
30 unsigned int mm2 = (t2/60/1000)%60;
31 unsigned int ss2 = (t2/1000)%60;
32 unsigned int ms2 = (t2)%1000;
33 int tmp = 0;
34 int j;
35
36 switch (type)
37 {
38 case SUBTITLE_SUBRIP:
39 fprintf(file, "%d%s%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d%s",
40 i + 1, nl, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, nl);
41 for (j = 0; j < SUBTITLE_MAX_LINES; j++)
42 if (st->subtitle[i].line[j])
43 fprintf(file, "%s%s", st->subtitle[i].line[j], nl);
44 fputs(nl, file); /* extra new line */
45 break;
46 case SUBTITLE_MICRODVD:
47 fprintf(file, "{%d}{%d}",
48 (int)(t1 * st->fps / 1000. + 0.5),
49 (int)(t2 * st->fps / 1000. + 0.5));
50 for (j = 0; j < SUBTITLE_MAX_LINES; j++)
51 {
52 if (st->subtitle[i].line[j])
53 {
54 if (tmp)
55 fputc('|', file);
56 fprintf(file, "%s", st->subtitle[i].line[j]);
57 tmp++;
58 }
59 }
60 fputs(nl, file);
61 break;
62 default:
63 return -1;
64 }
65 }
66 }
67
68 return 0;
69 }
70