-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSRT to Text Layer Sequence.jsx
162 lines (131 loc) · 3.94 KB
/
SRT to Text Layer Sequence.jsx
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
/**
* SRT to Text Layers
*
* @author Zack Lovatt <[email protected]>
* @version 0.1.2
*/
(function subtitleFileToTextLayerSequenceComp() {
var compOptions = {
width: 4096,
height: 2304,
fps: 23.976
};
app.beginUndoGroup("Set to Average Position");
try {
var subtitleFiles = getSubtitleFiles();
var subtitleData = getFilesData(subtitleFiles);
generateSubtitleComps(subtitleData, compOptions);
} catch (e) {
alert(e);
} finally {
app.endUndoGroup();
}
/**
* Gets text data from received files
*
* @param {File[]} subtitleFiles Subtitle files to parse
* @return {{name: string, text: string[]}[]} Subtitle data
*/
function getFilesData(subtitleFiles) {
var subtitleData = [];
for (var ii = 0, il = subtitleFiles.length; ii < il; ii++) {
var subtitleFile = subtitleFiles[ii];
var fileData = parseSubtitleFile(subtitleFile);
subtitleData.push(fileData);
}
return subtitleData;
}
/**
* Parses text from a given file into data
*
* @param {File} subtitleFile Subtitle file to parse
* @return {{name: string, text: string[]}} Subtitle data for this file
*/
function parseSubtitleFile(subtitleFile) {
var filenameNoExtension = subtitleFile.name.replace(/\.[^\/.]+$/, "")
var subtitleData = {
name: filenameNoExtension,
text: []
};
subtitleFile.open("r");
var contents = subtitleFile.read();
subtitleFile.close();
var subtitleDataStart = "<begin subtitles>";
var subtitleDataEnd = "<end subtitles>";
var contentsCropped = contents.substring(
contents.indexOf(subtitleDataStart) + subtitleDataStart.length,
contents.indexOf(subtitleDataEnd)
);
var contentsSplit = contentsCropped.split(/\s+^\d.+\s+/gm);
var textRegexp = /^(\D+)/g;
for (var ii = 0, il = contentsSplit.length; ii < il; ii++) {
var line = contentsSplit[ii];
// Trim the line of whitespace
var trimmed = line.replace(/^\s+|\s+$/g, "");
if (trimmed == "") {
continue;
}
if (!trimmed.match(textRegexp)) {
continue;
}
subtitleData.text.push(trimmed);
}
return subtitleData;
}
/**
* Generates subtitle comps from all the data, given comp options
*
* @param {{name: string, text: string[]}[]} subtitlesData Subtitle data to populate comps with
* @param {object} compOptions Options to generate comps from
* @returns {CompItem[]} Created comps
*/
function generateSubtitleComps(subtitlesData, compOptions) {
var comps = [];
for (var ii = 0, il = subtitlesData.length; ii < il; ii++) {
var subtitleData = subtitlesData[ii];
var duration = subtitleData.text.length / compOptions.fps;
var comp = app.project.items.addComp(
subtitleData.name,
compOptions.width,
compOptions.height,
1,
duration,
compOptions.fps
);
createTextLayers(comp, subtitleData.text);
comp.openInViewer();
comps.push(comp);
}
return comps;
}
/**
* Creates 1-frame text layers from given text data
*
* @param {CompItem} comp Comp to make layers in
* @param {string[]} textData Text to make layers from
*/
function createTextLayers(comp, textData) {
for (var ii = 0, il = textData.length; ii < il; ii++) {
var textLine = textData[ii];
var textLayer = comp.layers.addText(textLine);
textLayer.startTime = ii / comp.frameRate;
textLayer.outPoint = (ii + 1) / comp.frameRate;
}
}
/**
* Prompts user to select srt or txt subtitle files
*
* @returns {File[]} SRT/TXT files
*/
function getSubtitleFiles() {
var subtitleFiles = File.openDialog(
"Select SRT/TXT File",
"*.srt;*.txt",
true
);
if (subtitleFiles.length === 0) {
throw "Select some srt/txt files!";
}
return subtitleFiles;
}
})();