-
Notifications
You must be signed in to change notification settings - Fork 1
/
edl2mkvchapters.c
164 lines (136 loc) · 4.01 KB
/
edl2mkvchapters.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
//
// Created by paul on 4/4/21.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "edl2mkvchapters.h"
const char * xmlHeader =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
"<!DOCTYPE Chapters SYSTEM \"matroskachapters.dtd\">\n"
"<Chapters>\n"
" <EditionEntry>\n";
const char * chapterEntry =
" <ChapterAtom>\n"
" <ChapterTimeStart>%02d:%02d:%2.3f</ChapterTimeStart>\n"
" <ChapterTimeEnd>%02d:%02d:%2.3f</ChapterTimeEnd>\n"
" <ChapterDisplay>\n"
" <ChapterString>%s</ChapterString>\n"
" </ChapterDisplay>\n"
" </ChapterAtom>\n";
const char * xmlTrailer =
" </EditionEntry>\n"
"</Chapters>\n";
typedef struct {
unsigned short hours;
unsigned short minutes;
float seconds;
} tTimestamp;
void usage(void)
{
fprintf( stderr, "usage: \n");
}
void setTimestamp(tTimestamp * timestamp, float seconds )
{
timestamp->hours = seconds / (60 * 60);
seconds -= timestamp->hours * (60 * 60);
timestamp->minutes = seconds / 60;
seconds -= timestamp->minutes * 60;
timestamp->seconds = seconds;
}
void processFile( const char * inputFilename )
{
FILE * input = NULL;
FILE * output = NULL;
input = fopen( inputFilename, "r" );
if ( input == NULL )
{
fprintf( stderr, "Error: unable to open \"%s\" to read.\n", inputFilename );
return;
}
char * outputFilename = strdup( inputFilename );
char * p = strrchr( outputFilename, '.');
if ( p == NULL || strcasecmp( p, ".edl" ) != 0 )
{
/* Didn't find '.edl' extension at the end. Make
a little extra room to append an extension */
int len = strlen( outputFilename );
outputFilename = realloc( outputFilename, len + 5 );
p = outputFilename + len;
}
strncpy( p, ".xml", 5 );
output = fopen( outputFilename, "w" );
if ( input != NULL && output != NULL )
{
float startSec, endSec, prevSec;
tTimestamp start, end;
int type;
const char * typeAsStr;
fprintf( output, "%s", xmlHeader );
prevSec = 0;
while (!feof(input))
{
fscanf( input, "%f %f %d\n", &startSec, &endSec, &type );
if ( startSec > prevSec)
{
setTimestamp( &start, prevSec );
setTimestamp( &end, startSec );
typeAsStr = "Show";
fprintf( output, chapterEntry,
start.hours, start.minutes, start.seconds,
end.hours, end.minutes, end.seconds,
typeAsStr
);
}
setTimestamp( &start, startSec );
setTimestamp( &end, endSec );
switch ( type )
{
case 0:
typeAsStr = "Cut";
break;
case 1:
typeAsStr = "Mute";
break;
case 2:
typeAsStr = "Scene";
break;
case 3:
typeAsStr = "Commercials";
break;
default:
typeAsStr = "(unknown)";
break;
}
fprintf( output, chapterEntry,
start.hours, start.minutes, start.seconds,
end.hours, end.minutes, end.seconds,
typeAsStr
);
prevSec = endSec;
}
fprintf( output, "%s", xmlTrailer );
fclose( input );
fclose( output );
}
}
int main( int argc, const char * argv[] )
{
if ( argc < 2 )
{
fprintf( stderr, "Error: needs at least one file to process.\n" );
usage();
}
else
{
for ( int i = 1; i < argc; i++ )
{
if ( argv[i][0] != '-' )
{
processFile( argv[i] );
}
}
}
return 0;
}