Add ability to force transcription

This commit is contained in:
Chocobozzz 2024-07-02 11:19:18 +02:00
parent ad9eb48bab
commit 5ab681802b
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 58 additions and 26 deletions

View File

@ -417,7 +417,7 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
} }
private generateCaption (videos: Video[]) { private generateCaption (videos: Video[]) {
this.videoCaptionService.generateCaption(videos.map(v => v.id)) this.videoCaptionService.generateCaption({ videos })
.subscribe({ .subscribe({
next: result => { next: result => {
if (result.success) { if (result.success) {

View File

@ -1,9 +1,9 @@
import { HttpClient } from '@angular/common/http' import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { RestExtractor, ServerService } from '@app/core' import { ConfirmService, RestExtractor, ServerService } from '@app/core'
import { objectToFormData } from '@app/helpers' import { objectToFormData } from '@app/helpers'
import { peertubeTranslate, sortBy } from '@peertube/peertube-core-utils' import { peertubeTranslate, sortBy } from '@peertube/peertube-core-utils'
import { PeerTubeProblemDocument, ResultList, ServerErrorCode, VideoCaption } from '@peertube/peertube-models' import { PeerTubeProblemDocument, ResultList, ServerErrorCode, Video, VideoCaption, VideoCaptionGenerate } from '@peertube/peertube-models'
import { Observable, from, of, throwError } from 'rxjs' import { Observable, from, of, throwError } from 'rxjs'
import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators' import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'
import { environment } from '../../../../environments/environment' import { environment } from '../../../../environments/environment'
@ -16,7 +16,8 @@ export class VideoCaptionService {
constructor ( constructor (
private authHttp: HttpClient, private authHttp: HttpClient,
private serverService: ServerService, private serverService: ServerService,
private restExtractor: RestExtractor private restExtractor: RestExtractor,
private confirmService: ConfirmService
) {} ) {}
listCaptions (videoId: string, videoPassword?: string): Observable<ResultList<VideoCaption>> { listCaptions (videoId: string, videoPassword?: string): Observable<ResultList<VideoCaption>> {
@ -75,28 +76,16 @@ export class VideoCaptionService {
return this.authHttp.get(environment.originServerUrl + captionPath, { responseType: 'text' }) return this.authHttp.get(environment.originServerUrl + captionPath, { responseType: 'text' })
} }
generateCaption (videoIds: (number | string)[]) { // ---------------------------------------------------------------------------
return from(videoIds)
generateCaption (options: {
videos: Video[]
}): Observable<{ success: number, alreadyHasCaptions: number, alreadyBeingTranscribed: number }> {
const { videos } = options
return from(videos)
.pipe( .pipe(
concatMap(videoId => { concatMap(video => this.generateCaptionOfSpecificVideo({ video, forceTranscription: false })),
return this.authHttp.post(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/generate`, {})
.pipe(
map(() => 'success' as 'success'),
catchError(err => {
const error: PeerTubeProblemDocument = err.error
if (error?.code === ServerErrorCode.VIDEO_ALREADY_HAS_CAPTIONS) {
return of('already-has-captions' as 'already-has-captions')
}
if (error?.code === ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCRIBED) {
return of('already-being-transcribed' as 'already-being-transcribed')
}
return throwError(() => err)
})
)
}),
toArray(), toArray(),
map(data => { map(data => {
return data.reduce((p, c) => { return data.reduce((p, c) => {
@ -110,4 +99,47 @@ export class VideoCaptionService {
catchError(err => this.restExtractor.handleError(err)) catchError(err => this.restExtractor.handleError(err))
) )
} }
private generateCaptionOfSpecificVideo (options: {
video: Video
forceTranscription: boolean
}): Observable<'success' | 'already-has-captions' | 'already-being-transcribed'> {
const { video, forceTranscription } = options
const body: VideoCaptionGenerate = { forceTranscription }
return this.authHttp.post(`${VideoService.BASE_VIDEO_URL}/${video.id}/captions/generate`, body)
.pipe(
map(() => 'success' as 'success'),
catchError(err => {
const error: PeerTubeProblemDocument = err.error
if (error?.code === ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCRIBED && !forceTranscription) {
const message = $localize`PeerTube considers video "${video.name}" is already being transcripted.` +
// eslint-disable-next-line max-len
$localize` If you think PeerTube is wrong (video in broken state after a crash etc.), you can force transcription on this video.` +
` Do you still want to run transcription?`
return from(this.confirmService.confirm(message, $localize`Force transcription`))
.pipe(
switchMap(res => {
if (res === false) return throwError(() => err)
return this.generateCaptionOfSpecificVideo({ video, forceTranscription: true })
})
)
}
if (error?.code === ServerErrorCode.VIDEO_ALREADY_HAS_CAPTIONS) {
return of('already-has-captions' as 'already-has-captions')
}
if (error?.code === ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCRIBED) {
return of('already-being-transcribed' as 'already-being-transcribed')
}
return throwError(() => err)
})
)
}
} }

View File

@ -354,7 +354,7 @@ export class VideoActionsDropdownComponent implements OnChanges {
} }
generateCaption (video: Video) { generateCaption (video: Video) {
this.videoCaptionService.generateCaption([ video.id ]) this.videoCaptionService.generateCaption({ videos: [ video ] })
.subscribe({ .subscribe({
next: result => { next: result => {
if (result.success) this.notifier.success($localize`Transcription job created for "${video.name}".`) if (result.success) this.notifier.success($localize`Transcription job created for "${video.name}".`)