aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/helpers/captions-utils.ts27
1 files changed, 17 insertions, 10 deletions
diff --git a/server/helpers/captions-utils.ts b/server/helpers/captions-utils.ts
index 401f2fb7b..ca03f7a49 100644
--- a/server/helpers/captions-utils.ts
+++ b/server/helpers/captions-utils.ts
@@ -3,6 +3,8 @@ import { join } from 'path'
3import * as srt2vtt from 'srt-to-vtt' 3import * as srt2vtt from 'srt-to-vtt'
4import { MVideoCaption } from '@server/types/models' 4import { MVideoCaption } from '@server/types/models'
5import { CONFIG } from '../initializers/config' 5import { CONFIG } from '../initializers/config'
6import { pipelinePromise } from './core-utils'
7import { Transform } from 'stream'
6 8
7async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) { 9async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) {
8 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR 10 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
@@ -30,17 +32,22 @@ export {
30// --------------------------------------------------------------------------- 32// ---------------------------------------------------------------------------
31 33
32function convertSrtToVtt (source: string, destination: string) { 34function convertSrtToVtt (source: string, destination: string) {
33 return new Promise<void>((res, rej) => { 35 const fixVTT = new Transform({
34 const file = createReadStream(source) 36 transform: (chunk, _encoding, cb) => {
35 const converter = srt2vtt() 37 let block: string = chunk.toString()
36 const writer = createWriteStream(destination)
37 38
38 for (const s of [ file, converter, writer ]) { 39 block = block.replace(/(\d\d:\d\d:\d\d)(\s)/g, '$1.000$2')
39 s.on('error', err => rej(err)) 40 .replace(/(\d\d:\d\d:\d\d),(\d)(\s)/g, '$1.00$2$3')
40 } 41 .replace(/(\d\d:\d\d:\d\d),(\d\d)(\s)/g, '$1.0$2$3')
41 42
42 return file.pipe(converter) 43 return cb(undefined, block)
43 .pipe(writer) 44 }
44 .on('finish', () => res())
45 }) 45 })
46
47 return pipelinePromise(
48 createReadStream(source),
49 srt2vtt(),
50 fixVTT,
51 createWriteStream(destination)
52 )
46} 53}