Fix peertube subtitles import
This commit is contained in:
parent
c2eb81227b
commit
c56dd2807f
|
@ -2,6 +2,7 @@ import { UploadFilesForCheck } from 'express'
|
||||||
import { readFile } from 'fs-extra'
|
import { readFile } from 'fs-extra'
|
||||||
import { getFileSize } from '@shared/extra-utils'
|
import { getFileSize } from '@shared/extra-utils'
|
||||||
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
|
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
|
||||||
|
import { logger } from '../logger'
|
||||||
import { exists, isFileValid } from './misc'
|
import { exists, isFileValid } from './misc'
|
||||||
|
|
||||||
function isVideoCaptionLanguageValid (value: any) {
|
function isVideoCaptionLanguageValid (value: any) {
|
||||||
|
@ -24,12 +25,13 @@ function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
|
||||||
|
|
||||||
async function isVTTFileValid (filePath: string) {
|
async function isVTTFileValid (filePath: string) {
|
||||||
const size = await getFileSize(filePath)
|
const size = await getFileSize(filePath)
|
||||||
|
const content = await readFile(filePath, 'utf8')
|
||||||
|
|
||||||
|
logger.debug('Checking VTT file %s', filePath, { size, content })
|
||||||
|
|
||||||
if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
|
if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
|
||||||
|
|
||||||
const content = await readFile(filePath, 'utf8')
|
return content?.startsWith('WEBVTT')
|
||||||
|
|
||||||
return content?.startsWith('WEBVTT\n')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
|
@ -259,7 +259,7 @@ async function forgeThumbnail ({ inputPath, video, downloadUrl, type }: {
|
||||||
try {
|
try {
|
||||||
return await updateVideoMiniatureFromUrl({ downloadUrl, video, type })
|
return await updateVideoMiniatureFromUrl({ downloadUrl, video, type })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('Cannot process thumbnail %s from youtubedl.', downloadUrl, { err })
|
logger.warn('Cannot process thumbnail %s from youtube-dl.', downloadUrl, { err })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
|
@ -269,10 +269,11 @@ async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl:
|
||||||
try {
|
try {
|
||||||
const subtitles = await youtubeDL.getSubtitles()
|
const subtitles = await youtubeDL.getSubtitles()
|
||||||
|
|
||||||
logger.info('Will create %s subtitles from youtube import %s.', subtitles.length, targetUrl)
|
logger.info('Found %s subtitles candidates from youtube-dl import %s.', subtitles.length, targetUrl)
|
||||||
|
|
||||||
for (const subtitle of subtitles) {
|
for (const subtitle of subtitles) {
|
||||||
if (!await isVTTFileValid(subtitle.path)) {
|
if (!await isVTTFileValid(subtitle.path)) {
|
||||||
|
logger.info('%s is not a valid youtube-dl subtitle, skipping', subtitle.path)
|
||||||
await remove(subtitle.path)
|
await remove(subtitle.path)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -289,6 +290,8 @@ async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl:
|
||||||
await sequelizeTypescript.transaction(async t => {
|
await sequelizeTypescript.transaction(async t => {
|
||||||
await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
|
await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
logger.info('Added %s youtube-dl subtitle', subtitle.path)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('Cannot get video subtitles.', { err })
|
logger.warn('Cannot get video subtitles.', { err })
|
||||||
|
|
|
@ -449,6 +449,16 @@ describe('Test video imports', function () {
|
||||||
const video = await server.videos.get({ id: videoUUID })
|
const video = await server.videos.get({ id: videoUUID })
|
||||||
|
|
||||||
expect(video.name).to.equal('E2E tests')
|
expect(video.name).to.equal('E2E tests')
|
||||||
|
|
||||||
|
const { data: captions } = await server.captions.list({ videoId: videoUUID })
|
||||||
|
expect(captions).to.have.lengthOf(1)
|
||||||
|
expect(captions[0].language.id).to.equal('fr')
|
||||||
|
|
||||||
|
const str = `WEBVTT FILE\r?\n\r?\n` +
|
||||||
|
`1\r?\n` +
|
||||||
|
`00:00:04.000 --> 00:00:09.000\r?\n` +
|
||||||
|
`January 1, 1994. The North American`
|
||||||
|
await testCaptionFile(server.url, captions[0].captionPath, new RegExp(str))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user