forked from Chocobozzz/PeerTube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add ctranslate2 and timestamped
- Loading branch information
Showing
13 changed files
with
207 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createLogger } from 'winston' | ||
import { join } from 'path' | ||
import { expect } from 'chai' | ||
import { existsSync } from 'node:fs' | ||
import { rm, mkdir, readFile } from 'node:fs/promises' | ||
import { buildAbsoluteFixturePath, root } from '@peertube/peertube-node-utils' | ||
import { transcriberFactory } from '@peertube/peertube-transcription' | ||
|
||
describe('Transcribers', function () { | ||
const transcriptDirectory = join(root(), 'test-transcript') | ||
const vttTranscriptPath = join(transcriptDirectory, 'video_short.vtt') | ||
const transcribers = [ | ||
'openai-whisper', | ||
'whisper-ctranslate2', | ||
'whisper-timestamped' | ||
] | ||
|
||
before(async function () { | ||
await mkdir(transcriptDirectory, { recursive: true }) | ||
}) | ||
|
||
transcribers.forEach(function (transcriberName) { | ||
describe(`${transcriberName}`, function () { | ||
it(`Should instanciate`, function () { | ||
transcriberFactory.createFromEngineName(transcriberName) | ||
}) | ||
|
||
it('Should run transcription on a media file without raising any errors', async function () { | ||
const transcriber = transcriberFactory.createFromEngineName( | ||
transcriberName, | ||
createLogger(), | ||
transcriptDirectory | ||
) | ||
const mediaFilePath = buildAbsoluteFixturePath('video_short.mp4') | ||
const transcript = await transcriber.transcribe( | ||
mediaFilePath, | ||
{ name: 'tiny' }, | ||
'fr', | ||
'vtt' | ||
) | ||
expect(transcript).to.deep.equals({ | ||
path: vttTranscriptPath, | ||
language: 'fr', | ||
format: 'vtt' | ||
}) | ||
expect(transcript.path).to.equals(vttTranscriptPath) | ||
|
||
expect(existsSync(transcript.path), `Transcript file ${transcript.path} doesn't exist.`).to.be.true | ||
|
||
console.log(await readFile(transcript.path, 'utf8')) | ||
await rm(transcript.path) | ||
}) | ||
}) | ||
}) | ||
|
||
after(async function () { | ||
await rm(transcriptDirectory, { recursive: true, force: true }) | ||
}) | ||
}) |
28 changes: 0 additions & 28 deletions
28
packages/tests/src/transcription/whisper/transcriber/faster-whisper-transcriber.spec.ts
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
packages/tests/src/transcription/whisper/transcriber/openai-transcriber.spec.ts
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
packages/tests/src/transcription/whisper/transcribers.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { basename, extname } from 'path' | ||
|
||
export const getFileInfo = (path: string) => { | ||
const extension = extname(path) | ||
const baseName = basename(path, extension) | ||
const name = `${baseName}${extension}` | ||
|
||
return ({ | ||
extension, | ||
baseName, | ||
name | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './ctranslate2-transcriber.js' | ||
export * from './transformers-js-transcriber.js' | ||
export * from './transformers-transcriber.js' | ||
export * from './openai-transcriber.js' | ||
export * from './timestamped-transcriber.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/transcription/src/whisper/transcriber/timestamped-transcriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import assert from 'node:assert' | ||
import { join } from 'node:path' | ||
import { existsSync } from 'node:fs' | ||
import { rename } from 'node:fs/promises' | ||
import { $ } from 'execa' | ||
import { TranscriptionModel } from '../../transcription-model.js' | ||
import { Transcript, TranscriptFormat } from '../../transcript.js' | ||
import { AbstractTranscriber } from '../../abstract-transcriber.js' | ||
import { getFileInfo } from '../../file-utils.js' | ||
|
||
export class WhisperTimestampedTranscriber extends AbstractTranscriber { | ||
async transcribe ( | ||
mediaFilePath: string, | ||
model: TranscriptionModel, | ||
language: string, | ||
format: TranscriptFormat = 'vtt' | ||
): Promise<Transcript> { | ||
const $$ = $({ verbose: true }) | ||
const { baseName, name } = getFileInfo(mediaFilePath) | ||
await $$`whisper_timestamped ${[ | ||
mediaFilePath, | ||
'--model', | ||
model.name, | ||
'--output_format', | ||
'all', | ||
'--output_dir', | ||
this.transcriptDirectory | ||
]}` | ||
|
||
const internalTranscriptPath = join(this.transcriptDirectory, `${name}.${format}`) | ||
const transcriptPath = join(this.transcriptDirectory, `${baseName}.${format}`) | ||
assert(existsSync(internalTranscriptPath), '') | ||
|
||
await rename(internalTranscriptPath, transcriptPath) | ||
await $$`ls ${this.transcriptDirectory}` | ||
|
||
return { | ||
language, | ||
path: transcriptPath, | ||
format | ||
} | ||
} | ||
} |
Oops, something went wrong.