ActivityPub-federated video streaming platform using P2P directly in your web browser
Add ability to force transcoding
Chocobozzz89aa33311068
diff --git a/client/src/app/+admin/overview/videos/video-list.component.ts b/client/src/app/+admin/overview/videos/video-list.component.tsindex 52f02d8d04..2792a2d8a0 100644--- a/client/src/app/+admin/overview/videos/video-list.component.ts+++ b/client/src/app/+admin/overview/videos/video-list.component.ts@@ -3,7 +3,7 @@ import { finalize } from 'rxjs/operators'import { Component, OnInit, ViewChild } from '@angular/core'import { ActivatedRoute, Router } from '@angular/router'import { AuthService, ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'-import { formatICU } from '@app/helpers'+import { formatICU, getAbsoluteAPIUrl } from '@app/helpers'import { AdvancedInputFilter } from '@app/shared/shared-forms'import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'import { VideoBlockComponent, VideoBlockService } from '@app/shared/shared-moderation'@@ -166,7 +166,7 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {const files = getAllFiles(video)- return files.some(f => !f.fileUrl.startsWith(window.location.origin))+ return files.some(f => !f.fileUrl.startsWith(getAbsoluteAPIUrl()))}canRemoveOneFile (video: Video) {@@ -294,7 +294,7 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {}private runTranscoding (videos: Video[], type: 'hls' | 'web-video') {- this.videoService.runTranscoding(videos.map(v => v.id), type)+ this.videoService.runTranscoding({ videoIds: videos.map(v => v.id), type, askForForceTranscodingIfNeeded: false }).subscribe({next: () => {this.notifier.success($localize`Transcoding jobs created.`)diff --git a/client/src/app/shared/shared-main/video/video.model.ts b/client/src/app/shared/shared-main/video/video.model.tsindex a5bf1db8b3..392dcadd06 100644--- a/client/src/app/shared/shared-main/video/video.model.ts+++ b/client/src/app/shared/shared-main/video/video.model.ts@@ -257,9 +257,12 @@ export class Video implements VideoServerModel {}canRunTranscoding (user: AuthUser) {+ return this.canRunForcedTranscoding(user) && this.state.id !== VideoState.TO_TRANSCODE+ }++ canRunForcedTranscoding (user: AuthUser) {return this.isLocal &&- user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&- this.state.id !== VideoState.TO_TRANSCODE+ user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING)}hasHLS () {diff --git a/client/src/app/shared/shared-main/video/video.service.ts b/client/src/app/shared/shared-main/video/video.service.tsindex 20145b9c5a..cd0a300e26 100644--- a/client/src/app/shared/shared-main/video/video.service.ts+++ b/client/src/app/shared/shared-main/video/video.service.ts@@ -1,9 +1,9 @@import { SortMeta } from 'primeng/api'-import { from, Observable, of } from 'rxjs'+import { from, Observable, of, throwError } from 'rxjs'import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'import { Injectable } from '@angular/core'-import { AuthService, ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core'+import { AuthService, ComponentPaginationLight, ConfirmService, RestExtractor, RestService, ServerService, UserService } from '@app/core'import { objectToFormData } from '@app/helpers'import { arrayify } from '@shared/core-utils'import {@@ -11,6 +11,7 @@ import {FeedFormat,NSFWPolicyType,ResultList,+ ServerErrorCode,Storyboard,UserVideoRate,UserVideoRateType,@@ -33,8 +34,8 @@ import { AccountService } from '../account/account.service'import { VideoChannel, VideoChannelService } from '../video-channel'import { VideoDetails } from './video-details.model'import { VideoEdit } from './video-edit.model'-import { Video } from './video.model'import { VideoPasswordService } from './video-password.service'+import { Video } from './video.model'export type CommonVideoParams = {videoPagination?: ComponentPaginationLight@@ -64,7 +65,8 @@ export class VideoService {private authHttp: HttpClient,private restExtractor: RestExtractor,private restService: RestService,- private serverService: ServerService+ private serverService: ServerService,+ private confirmService: ConfirmService) {}getVideoViewUrl (uuid: string) {@@ -325,17 +327,53 @@ export class VideoService {.pipe(catchError(err => this.restExtractor.handleError(err)))}- runTranscoding (videoIds: (number | string)[], type: 'hls' | 'web-video') {- const body: VideoTranscodingCreate = { transcodingType: type }+ runTranscoding (options: {+ videoIds: (number | string)[]+ type: 'hls' | 'web-video'+ askForForceTranscodingIfNeeded: boolean+ forceTranscoding?: boolean+ }): Observable<any> {+ const { videoIds, type, askForForceTranscodingIfNeeded, forceTranscoding } = options++ if (askForForceTranscodingIfNeeded && videoIds.length !== 1) {+ throw new Error('Cannot ask to force transcoding on multiple videos')+ }++ const body: VideoTranscodingCreate = { transcodingType: type, forceTranscoding }return from(videoIds).pipe(- concatMap(id => this.authHttp.post(VideoService.BASE_VIDEO_URL + '/' + id + '/transcoding', body)),+ concatMap(id => {+ return this.authHttp.post(VideoService.BASE_VIDEO_URL + '/' + id + '/transcoding', body)+ .pipe(+ catchError(err => {+ if (askForForceTranscodingIfNeeded && err.error?.code === ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCODED) {+ const message = $localize`PeerTube considers this video is already being transcoded.` ++ // eslint-disable-next-line max-len+ $localize` If you think PeerTube is wrong (video in broken state after a crash etc.), you can force transcoding on this video.` ++ ` Do you still want to run transcoding?`++ return from(this.confirmService.confirm(message, $localize`Force transcoding`))+ .pipe(+ switchMap(res => {+ if (res === false) return throwError(() => err)++ return this.runTranscoding({ videoIds, type, askForForceTranscodingIfNeeded: false, forceTranscoding: true })+ })+ )+ }++ return throwError(() => err)+ })+ )+ }),toArray(),catchError(err => this.restExtractor.handleError(err)))}+ // ---------------------------------------------------------------------------+loadCompleteDescription (descriptionPath: string) {return this.authHttp.get<{ description: string }>(environment.apiUrl + descriptionPath)diff --git a/client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.ts b/client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.tsindex 0a3ada7113..9891aae2ee 100644--- a/client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.ts+++ b/client/src/app/shared/shared-video-miniature/video-actions-dropdown.component.ts@@ -198,8 +198,8 @@ export class VideoActionsDropdownComponent implements OnChanges {return this.video.canRemoveFiles(this.user)}- canRunTranscoding () {- return this.video.canRunTranscoding(this.user)+ canRunForcedTranscoding () {+ return this.video.canRunForcedTranscoding(this.user)}/* Action handlers */@@ -291,10 +291,10 @@ export class VideoActionsDropdownComponent implements OnChanges {}runTranscoding (video: Video, type: 'hls' | 'web-video') {- this.videoService.runTranscoding([ video.id ], type)+ this.videoService.runTranscoding({ videoIds: [ video.id ], type, askForForceTranscodingIfNeeded: true }).subscribe({next: () => {- this.notifier.success($localize`Transcoding jobs created for ${video.name}.`)+ this.notifier.success($localize`Transcoding jobs created for "${video.name}".`)this.transcodingCreated.emit()},@@ -390,13 +390,13 @@ export class VideoActionsDropdownComponent implements OnChanges {{label: $localize`Run HLS transcoding`,handler: ({ video }) => this.runTranscoding(video, 'hls'),- isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),+ isDisplayed: () => this.displayOptions.transcoding && this.canRunForcedTranscoding(),iconName: 'cog'},{label: $localize`Run Web Video transcoding`,handler: ({ video }) => this.runTranscoding(video, 'web-video'),- isDisplayed: () => this.displayOptions.transcoding && this.canRunTranscoding(),+ isDisplayed: () => this.displayOptions.transcoding && this.canRunForcedTranscoding(),iconName: 'cog'},{diff --git a/server/controllers/api/videos/transcoding.ts b/server/controllers/api/videos/transcoding.tsindex 54f484b2b8..c0b93742f2 100644--- a/server/controllers/api/videos/transcoding.ts+++ b/server/controllers/api/videos/transcoding.ts@@ -3,6 +3,7 @@ import { logger, loggerTagsFactory } from '@server/helpers/logger'import { Hooks } from '@server/lib/plugins/hooks'import { createTranscodingJobs } from '@server/lib/transcoding/create-transcoding-job'import { computeResolutionsToTranscode } from '@server/lib/transcoding/transcoding-resolutions'+import { VideoJobInfoModel } from '@server/models/video/video-job-info'import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'@@ -30,6 +31,8 @@ async function createTranscoding (req: express.Request, res: express.Response) {const body: VideoTranscodingCreate = req.body+ await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingTranscode')+const { resolution: maxResolution, hasAudio } = await video.probeMaxQualityFile()const resolutions = await Hooks.wrapObject(diff --git a/server/middlewares/validators/videos/video-transcoding.ts b/server/middlewares/validators/videos/video-transcoding.tsindex 3eb2d3141e..2f99ff42c2 100644--- a/server/middlewares/validators/videos/video-transcoding.ts+++ b/server/middlewares/validators/videos/video-transcoding.ts@@ -1,9 +1,10 @@import express from 'express'import { body } from 'express-validator'+import { isBooleanValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc'import { isValidCreateTranscodingType } from '@server/helpers/custom-validators/video-transcoding'import { CONFIG } from '@server/initializers/config'import { VideoJobInfoModel } from '@server/models/video/video-job-info'-import { HttpStatusCode } from '@shared/models'+import { HttpStatusCode, ServerErrorCode, VideoTranscodingCreate } from '@shared/models'import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'const createTranscodingValidator = [@@ -12,6 +13,11 @@ const createTranscodingValidator = [body('transcodingType').custom(isValidCreateTranscodingType),+ body('forceTranscoding')+ .optional()+ .customSanitizer(toBooleanOrNull)+ .custom(isBooleanValid),+async (req: express.Request, res: express.Response, next: express.NextFunction) => {if (areValidationErrors(req, res)) returnif (!await doesVideoExist(req.params.videoId, res, 'all')) return@@ -32,11 +38,14 @@ const createTranscodingValidator = [})}- // Prefer using job info table instead of video state because before 4.0 failed transcoded video were stuck in "TO_TRANSCODE" state+ const body = req.body as VideoTranscodingCreate+ if (body.forceTranscoding === true) return next()+const info = await VideoJobInfoModel.load(video.id)if (info && info.pendingTranscode > 0) {return res.fail({status: HttpStatusCode.CONFLICT_409,+ type: ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCODED,message: 'This video is already being transcoded'})}diff --git a/server/tests/api/check-params/transcoding.ts b/server/tests/api/check-params/transcoding.tsindex 4bebcb5283..d5899e11ba 100644--- a/server/tests/api/check-params/transcoding.ts+++ b/server/tests/api/check-params/transcoding.ts@@ -93,15 +93,17 @@ describe('Test transcoding API validators', function () {await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'hls' })await waitJobs(servers)- await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'web-video' })+ await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'web-video', forceTranscoding: true })await waitJobs(servers)})- it('Should not run transcoding on a video that is already being transcoded', async function () {+ it('Should not run transcoding on a video that is already being transcoded if forceTranscoding is not set', async function () {await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'web-video' })const expectedStatus = HttpStatusCode.CONFLICT_409await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'web-video', expectedStatus })++ await servers[0].videos.runTranscoding({ videoId: validId, transcodingType: 'web-video', forceTranscoding: true })})after(async function () {diff --git a/shared/models/server/server-error-code.enum.ts b/shared/models/server/server-error-code.enum.tsindex 77d1e1d3f6..583e8245f4 100644--- a/shared/models/server/server-error-code.enum.ts+++ b/shared/models/server/server-error-code.enum.ts@@ -52,7 +52,9 @@ export const enum ServerErrorCode {UNKNOWN_RUNNER_TOKEN = 'unknown_runner_token',VIDEO_REQUIRES_PASSWORD = 'video_requires_password',- INCORRECT_VIDEO_PASSWORD = 'incorrect_video_password'+ INCORRECT_VIDEO_PASSWORD = 'incorrect_video_password',++ VIDEO_ALREADY_BEING_TRANSCODED = 'video_already_being_transcoded'}/**diff --git a/shared/models/videos/transcoding/video-transcoding-create.model.ts b/shared/models/videos/transcoding/video-transcoding-create.model.tsindex c6e756a0a6..6c2dbefa62 100644--- a/shared/models/videos/transcoding/video-transcoding-create.model.ts+++ b/shared/models/videos/transcoding/video-transcoding-create.model.ts@@ -1,3 +1,5 @@export interface VideoTranscodingCreate {transcodingType: 'hls' | 'webtorrent' | 'web-video' // TODO: remove webtorrent in v7++ forceTranscoding?: boolean // Default false}diff --git a/shared/server-commands/videos/videos-command.ts b/shared/server-commands/videos/videos-command.tsindex a58f1c545f..4c3513ed4c 100644--- a/shared/server-commands/videos/videos-command.ts+++ b/shared/server-commands/videos/videos-command.ts@@ -775,19 +775,16 @@ export class VideosCommand extends AbstractCommand {})}- runTranscoding (options: OverrideCommandOptions & {+ runTranscoding (options: OverrideCommandOptions & VideoTranscodingCreate & {videoId: number | string- transcodingType: 'hls' | 'webtorrent' | 'web-video'}) {const path = '/api/v1/videos/' + options.videoId + '/transcoding'- const fields: VideoTranscodingCreate = pick(options, [ 'transcodingType' ])-return this.postBodyRequest({...options,path,- fields,+ fields: pick(options, [ 'transcodingType', 'forceTranscoding' ]),implicitToken: true,defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204})diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yamlindex 44daecf852..0cbc58678b 100644--- a/support/doc/api/openapi.yaml+++ b/support/doc/api/openapi.yaml@@ -4914,6 +4914,10 @@ paths:enum:- hls- web-video+ forceTranscoding:+ type: boolean+ default: false+ description: If the video is stuck in transcoding state, do it anywayrequired:- transcodingTyperesponses: