diff options
author | Chocobozzz <me@florianbigard.com> | 2021-04-27 09:00:09 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-04-27 09:00:16 +0200 |
commit | c9ae74d6bc2918ac38ce2dd25aa8c542132529ef (patch) | |
tree | ec8e7fd4f822bae2ef979e9eb629638f0f614c8a | |
parent | 27b785dcfc4163b6ee3e1589e72e21667ef4bf70 (diff) | |
download | PeerTube-c9ae74d6bc2918ac38ce2dd25aa8c542132529ef.tar.gz PeerTube-c9ae74d6bc2918ac38ce2dd25aa8c542132529ef.tar.zst PeerTube-c9ae74d6bc2918ac38ce2dd25aa8c542132529ef.zip |
Try to fix bad timestamps in .srt
-rw-r--r-- | server/helpers/captions-utils.ts | 27 |
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' | |||
3 | import * as srt2vtt from 'srt-to-vtt' | 3 | import * as srt2vtt from 'srt-to-vtt' |
4 | import { MVideoCaption } from '@server/types/models' | 4 | import { MVideoCaption } from '@server/types/models' |
5 | import { CONFIG } from '../initializers/config' | 5 | import { CONFIG } from '../initializers/config' |
6 | import { pipelinePromise } from './core-utils' | ||
7 | import { Transform } from 'stream' | ||
6 | 8 | ||
7 | async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) { | 9 | async 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 | ||
32 | function convertSrtToVtt (source: string, destination: string) { | 34 | function 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 | } |