]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/captions-utils.ts
Translated using Weblate (Kabyle)
[github/Chocobozzz/PeerTube.git] / server / helpers / captions-utils.ts
... / ...
CommitLineData
1import { createReadStream, createWriteStream, move, remove } from 'fs-extra'
2import { join } from 'path'
3import * as srt2vtt from 'srt-to-vtt'
4import { MVideoCaption } from '@server/types/models'
5import { CONFIG } from '../initializers/config'
6
7async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) {
8 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
9 const destination = join(videoCaptionsDir, videoCaption.filename)
10
11 // Convert this srt file to vtt
12 if (physicalFile.path.endsWith('.srt')) {
13 await convertSrtToVtt(physicalFile.path, destination)
14 await remove(physicalFile.path)
15 } else if (physicalFile.path !== destination) { // Just move the vtt file
16 await move(physicalFile.path, destination, { overwrite: true })
17 }
18
19 // This is important in case if there is another attempt in the retry process
20 physicalFile.filename = videoCaption.filename
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<void>((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}