]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/captions-utils.ts
remove isAdministration
[github/Chocobozzz/PeerTube.git] / server / helpers / captions-utils.ts
CommitLineData
f4001cf4
C
1import { join } from 'path'
2import { CONFIG } from '../initializers'
3import { VideoCaptionModel } from '../models/video/video-caption'
4import * as srt2vtt from 'srt-to-vtt'
f481c4f9 5import { createReadStream, createWriteStream, remove, move } from 'fs-extra'
f4001cf4
C
6
7async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: VideoCaptionModel) {
8 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
9 const destination = join(videoCaptionsDir, videoCaption.getCaptionName())
10
11 // Convert this srt file to vtt
12 if (physicalFile.path.endsWith('.srt')) {
13 await convertSrtToVtt(physicalFile.path, destination)
62689b94 14 await remove(physicalFile.path)
f4001cf4 15 } else { // Just move the vtt file
44848a51 16 await move(physicalFile.path, destination, { overwrite: true })
f4001cf4
C
17 }
18
19 // This is important in case if there is another attempt in the retry process
20 physicalFile.filename = videoCaption.getCaptionName()
21 physicalFile.path = destination
22}
23
24// ---------------------------------------------------------------------------
25
26export {
27 moveAndProcessCaptionFile
28}
29
30// ---------------------------------------------------------------------------
31
32function convertSrtToVtt (source: string, destination: string) {
33 return new Promise((res, rej) => {
34 const file = createReadStream(source)
35 const converter = srt2vtt()
36 const writer = createWriteStream(destination)
37
38 for (const s of [ file, converter, writer ]) {
39 s.on('error', err => rej(err))
40 }
41
42 return file.pipe(converter)
43 .pipe(writer)
44 .on('finish', () => res())
45 })
46}