-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (148 loc) · 4.51 KB
/
index.js
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
#!/usr/bin/env node
var program = require('commander');
var currentFile = "";
var debug = true;
var lrc_insertdummyforempty = false;
var ConvertOptionsJS = require('./ConvertOptions');
var convertOptions = null;
var noOutput = false;
var version = "0.0.1";
program
.version(version)
.arguments('<file>')
.option('-f, --format <format>', 'Mandatory. Output format.')
.option('-c, --convertoptions <convertoptions>', 'Convert options.')
.option('-p, --profile <profile>', 'Profile of the output format (version and supported features). If the profile is not specified, the version with the least features is selected.')
.option('-d, --direct', 'Direct saving.')
.action(function(file) {
main(file);
});
program.on('--help', function(){
console.log("Supported file formats are:");
console.log("");
console.log("File format\t\tFile extension\tProfiles");
console.log("lrc / LyRiCs\t\t.lrc\t\tlrc, lrc-id, lrc-sfe (Walaoke), lrc-ef (A2 Media Player)");
console.log("SubRip \t\t\t.srt\t\tsrt, subrip-markup");
console.log("WebVTT \t\t\t.vtt / .srt\twebvtt");
console.log("");
console.log("Available convert options:");
console.log("");
console.log("lrc_dummyforempty\tInsert empty line between subtitles if there is a pause");
console.log("webvtt_linenumbers\tPass this option to include line numbers in WebVTT files");
});
program.parse(process.argv);
// start point of our application
function main(file){
console.warn("subconv " + version);
console.warn("");
var format = program.format;
convertOptions = new ConvertOptionsJS(program.convertoptions);
currentFile = file;
readFile(file);
}
function switchConvert(err, fileContents){
if(err){
if(debug)
return console.log(err);
return console.log("WARNING: Could not read file %s", currentFile);
}
var inputFormat = getInputFormat();
var outputFormat = getOutputFormat();
// TODO: internal representation of the data that is contained in the file format
showDebug("Converting " + currentFile);
showDebug("Input format: " + inputFormat);
if(inputFormat == "srt"){
console.warn("Using the default SubRip parser without profiles.");
}
switch(inputFormat.toLowerCase()){
case "lrc":
case "lyrics":
var format_lrc = require('./format/lrc');
fileContents = format_lrc.c_lrc_generic(fileContents,convertOptions);
break;
case "vtt":
case "webvtt":
var format_webvtt = require('./format/webvtt');
fileContents = format_webvtt.c_webvtt_generic(fileContents, convertOptions);
default:
break;
}
showDebug("Output format: " + outputFormat);
switch(outputFormat.toLowerCase()){
case "lrc":
case "lyrics":
var format_lrc = require('./format/lrc');
handleResult(format_lrc.c_srt_lrc(fileContents,convertOptions));
break;
case "vtt":
case "webvtt":
var format_webvtt = require('./format/webvtt');
handleResult(format_webvtt.c_srt_webvtt(fileContents,convertOptions));
break;
case "srt":
case "subrip":
handleResult(fileContents);
break;
default:
break;
}
}
///////////////////////////////////////////////////
// File System
///////////////////////////////////////////////////
function readFile(file){
var fs = require('fs')
fs.readFile(file, 'utf8', switchConvert);
}
function writeFile(file, fileContents){
var fs = require('fs');
fs.writeFile(file, fileContents, function(err) {
if(err) {
return console.log(err);
}
//console.log("The file was saved!");
});
}
///////////////////////////////////////////////////
// Handle output
///////////////////////////////////////////////////
function handleResult(result){
var outputFileName = getOutputFileName();
if(outputFileName != ""){
writeFile(outputFileName, result);
}else{
if(!noOutput){
console.log(result);
}
}
}
function showDebug(debugInformation){
if(debug){
console.warn(debugInformation);
}
}
///////////////////////////////////////////////////
// "Simple" getters
///////////////////////////////////////////////////
function getInputFormat(){
var lastDot = currentFile.lastIndexOf(".");
var extension = currentFile.substring(lastDot + 1);
return extension;
}
function getOutputFormat(){
return program.format;
}
function isDirect(){
return program.direct == true;
}
function getOutputFileName(){
if(isDirect()){
var lastDot = currentFile.lastIndexOf(".");
var outputFileName = currentFile.substring(0, lastDot + 1) + getOutputFormat(); // include the dot
outputFileName = outputFileName.replace(".en.", ".");
outputFileName = outputFileName.replace(".en-part.", ".");
return outputFileName;
}else{
return "";
}
}