]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/captions-utils.ts
Translated using Weblate (Arabic)
[github/Chocobozzz/PeerTube.git] / server / helpers / captions-utils.ts
CommitLineData
6302d599 1import { createReadStream, createWriteStream, move, remove } from 'fs-extra'
f4001cf4 2import { join } from 'path'
f4001cf4 3import * as srt2vtt from 'srt-to-vtt'
6302d599
C
4import { MVideoCaption } from '@server/types/models'
5import { CONFIG } from '../initializers/config'
f4001cf4 6
6302d599 7async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) {
f4001cf4 8 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
6302d599 9 const destination = join(videoCaptionsDir, videoCaption.filename)
f4001cf4
C
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)
3f6b7aa1 15 } else if (physicalFile.path !== destination) { // 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
6302d599 20 physicalFile.filename = videoCaption.filename
f4001cf4
C
21 physicalFile.path = destination
22}
23
24// ---------------------------------------------------------------------------
25
26export {
27 moveAndProcessCaptionFile
28}
29
30// ---------------------------------------------------------------------------
31
32function convertSrtToVtt (source: string, destination: string) {
ba5a8d89 33 return new Promise<void>((res, rej) => {
f4001cf4
C
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}