diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-16 14:22:16 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 14:31:40 +0200 |
commit | f4001cf408a99049d01a356bfb20a62342de06ea (patch) | |
tree | 421776dfe64335dca2725ac3ac5f3b3e6b7564c6 /server/helpers/captions-utils.ts | |
parent | 16f7022b06fb76c0b00c23c970bc8df605b0ec63 (diff) | |
download | PeerTube-f4001cf408a99049d01a356bfb20a62342de06ea.tar.gz PeerTube-f4001cf408a99049d01a356bfb20a62342de06ea.tar.zst PeerTube-f4001cf408a99049d01a356bfb20a62342de06ea.zip |
Handle .srt subtitles
Diffstat (limited to 'server/helpers/captions-utils.ts')
-rw-r--r-- | server/helpers/captions-utils.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/server/helpers/captions-utils.ts b/server/helpers/captions-utils.ts new file mode 100644 index 000000000..8b04f878d --- /dev/null +++ b/server/helpers/captions-utils.ts | |||
@@ -0,0 +1,47 @@ | |||
1 | import { renamePromise, unlinkPromise } from './core-utils' | ||
2 | import { join } from 'path' | ||
3 | import { CONFIG } from '../initializers' | ||
4 | import { VideoCaptionModel } from '../models/video/video-caption' | ||
5 | import * as srt2vtt from 'srt-to-vtt' | ||
6 | import { createReadStream, createWriteStream } from 'fs' | ||
7 | |||
8 | async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: VideoCaptionModel) { | ||
9 | const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR | ||
10 | const destination = join(videoCaptionsDir, videoCaption.getCaptionName()) | ||
11 | |||
12 | // Convert this srt file to vtt | ||
13 | if (physicalFile.path.endsWith('.srt')) { | ||
14 | await convertSrtToVtt(physicalFile.path, destination) | ||
15 | await unlinkPromise(physicalFile.path) | ||
16 | } else { // Just move the vtt file | ||
17 | await renamePromise(physicalFile.path, destination) | ||
18 | } | ||
19 | |||
20 | // This is important in case if there is another attempt in the retry process | ||
21 | physicalFile.filename = videoCaption.getCaptionName() | ||
22 | physicalFile.path = destination | ||
23 | } | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | export { | ||
28 | moveAndProcessCaptionFile | ||
29 | } | ||
30 | |||
31 | // --------------------------------------------------------------------------- | ||
32 | |||
33 | function convertSrtToVtt (source: string, destination: string) { | ||
34 | return new Promise((res, rej) => { | ||
35 | const file = createReadStream(source) | ||
36 | const converter = srt2vtt() | ||
37 | const writer = createWriteStream(destination) | ||
38 | |||
39 | for (const s of [ file, converter, writer ]) { | ||
40 | s.on('error', err => rej(err)) | ||
41 | } | ||
42 | |||
43 | return file.pipe(converter) | ||
44 | .pipe(writer) | ||
45 | .on('finish', () => res()) | ||
46 | }) | ||
47 | } | ||