2021-10-21 10:28:39 -04:00
|
|
|
import { move, pathExists, readdir, remove } from 'fs-extra'
|
|
|
|
import { dirname, join } from 'path'
|
2022-09-07 11:30:21 -04:00
|
|
|
import { inspect } from 'util'
|
2021-10-21 10:28:39 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { isVideoFileExtnameValid } from '../custom-validators/videos'
|
|
|
|
import { logger, loggerTagsFactory } from '../logger'
|
|
|
|
import { generateVideoImportTmpPath } from '../utils'
|
|
|
|
import { YoutubeDLCLI } from './youtube-dl-cli'
|
|
|
|
import { YoutubeDLInfo, YoutubeDLInfoBuilder } from './youtube-dl-info-builder'
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('youtube-dl')
|
|
|
|
|
|
|
|
export type YoutubeDLSubs = {
|
|
|
|
language: string
|
|
|
|
filename: string
|
|
|
|
path: string
|
|
|
|
}[]
|
|
|
|
|
|
|
|
const processOptions = {
|
2022-02-22 09:03:13 -05:00
|
|
|
maxBuffer: 1024 * 1024 * 30 // 30MB
|
2021-10-21 10:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class YoutubeDLWrapper {
|
|
|
|
|
2022-08-05 09:05:20 -04:00
|
|
|
constructor (
|
|
|
|
private readonly url: string,
|
|
|
|
private readonly enabledResolutions: number[],
|
|
|
|
private readonly useBestFormat: boolean
|
|
|
|
) {
|
2021-10-21 10:28:39 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async getInfoForDownload (youtubeDLArgs: string[] = []): Promise<YoutubeDLInfo> {
|
|
|
|
const youtubeDL = await YoutubeDLCLI.safeGet()
|
|
|
|
|
|
|
|
const info = await youtubeDL.getInfo({
|
|
|
|
url: this.url,
|
2022-08-05 09:05:20 -04:00
|
|
|
format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
|
2021-10-21 10:28:39 -04:00
|
|
|
additionalYoutubeDLArgs: youtubeDLArgs,
|
|
|
|
processOptions
|
|
|
|
})
|
|
|
|
|
2022-08-18 05:40:07 -04:00
|
|
|
if (!info) throw new Error(`YoutubeDL could not get info from ${this.url}`)
|
|
|
|
|
2021-10-21 10:28:39 -04:00
|
|
|
if (info.is_live === true) throw new Error('Cannot download a live streaming.')
|
|
|
|
|
|
|
|
const infoBuilder = new YoutubeDLInfoBuilder(info)
|
|
|
|
|
|
|
|
return infoBuilder.getInfo()
|
|
|
|
}
|
|
|
|
|
2022-08-10 03:53:39 -04:00
|
|
|
async getInfoForListImport (options: {
|
|
|
|
latestVideosCount?: number
|
|
|
|
}) {
|
|
|
|
const youtubeDL = await YoutubeDLCLI.safeGet()
|
|
|
|
|
|
|
|
const list = await youtubeDL.getListInfo({
|
|
|
|
url: this.url,
|
|
|
|
latestVideosCount: options.latestVideosCount,
|
|
|
|
processOptions
|
|
|
|
})
|
|
|
|
|
2022-09-07 11:30:21 -04:00
|
|
|
if (!Array.isArray(list)) throw new Error(`YoutubeDL could not get list info from ${this.url}: ${inspect(list)}`)
|
2022-08-18 08:58:27 -04:00
|
|
|
|
2022-09-07 11:30:21 -04:00
|
|
|
return list.map(info => info.webpage_url)
|
2022-08-10 03:53:39 -04:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:28:39 -04:00
|
|
|
async getSubtitles (): Promise<YoutubeDLSubs> {
|
|
|
|
const cwd = CONFIG.STORAGE.TMP_DIR
|
|
|
|
|
|
|
|
const youtubeDL = await YoutubeDLCLI.safeGet()
|
|
|
|
|
|
|
|
const files = await youtubeDL.getSubs({ url: this.url, format: 'vtt', processOptions: { cwd } })
|
|
|
|
if (!files) return []
|
|
|
|
|
|
|
|
logger.debug('Get subtitles from youtube dl.', { url: this.url, files, ...lTags() })
|
|
|
|
|
|
|
|
const subtitles = files.reduce((acc, filename) => {
|
|
|
|
const matched = filename.match(/\.([a-z]{2})(-[a-z]+)?\.(vtt|ttml)/i)
|
|
|
|
if (!matched || !matched[1]) return acc
|
|
|
|
|
|
|
|
return [
|
|
|
|
...acc,
|
|
|
|
{
|
|
|
|
language: matched[1],
|
|
|
|
path: join(cwd, filename),
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return subtitles
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadVideo (fileExt: string, timeout: number): Promise<string> {
|
|
|
|
// Leave empty the extension, youtube-dl will add it
|
|
|
|
const pathWithoutExtension = generateVideoImportTmpPath(this.url, '')
|
|
|
|
|
|
|
|
logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
|
|
|
|
|
|
|
|
const youtubeDL = await YoutubeDLCLI.safeGet()
|
|
|
|
|
2022-02-09 05:40:47 -05:00
|
|
|
try {
|
|
|
|
await youtubeDL.download({
|
|
|
|
url: this.url,
|
2022-08-05 09:05:20 -04:00
|
|
|
format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
|
2022-02-09 05:40:47 -05:00
|
|
|
output: pathWithoutExtension,
|
|
|
|
timeout,
|
|
|
|
processOptions
|
2021-10-21 10:28:39 -04:00
|
|
|
})
|
|
|
|
|
2022-02-09 05:40:47 -05:00
|
|
|
// If youtube-dl did not guess an extension for our file, just use .mp4 as default
|
|
|
|
if (await pathExists(pathWithoutExtension)) {
|
|
|
|
await move(pathWithoutExtension, pathWithoutExtension + '.mp4')
|
|
|
|
}
|
2021-11-09 09:07:06 -05:00
|
|
|
|
2022-02-09 05:40:47 -05:00
|
|
|
return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
|
|
|
|
} catch (err) {
|
|
|
|
this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
|
|
|
|
.then(path => {
|
|
|
|
logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
|
2021-10-21 10:28:39 -04:00
|
|
|
|
2022-02-09 05:40:47 -05:00
|
|
|
return remove(path)
|
|
|
|
})
|
2022-08-10 03:53:39 -04:00
|
|
|
.catch(innerErr => logger.error('Cannot remove file in youtubeDL error.', { innerErr, ...lTags() }))
|
2022-02-09 05:40:47 -05:00
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2021-10-21 10:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {
|
|
|
|
if (!isVideoFileExtnameValid(sourceExt)) {
|
|
|
|
throw new Error('Invalid video extension ' + sourceExt)
|
|
|
|
}
|
|
|
|
|
|
|
|
const extensions = [ sourceExt, '.mp4', '.mkv', '.webm' ]
|
|
|
|
|
|
|
|
for (const extension of extensions) {
|
|
|
|
const path = tmpPath + extension
|
|
|
|
|
|
|
|
if (await pathExists(path)) return path
|
|
|
|
}
|
|
|
|
|
|
|
|
const directoryContent = await readdir(dirname(tmpPath))
|
|
|
|
|
|
|
|
throw new Error(`Cannot guess path of ${tmpPath}. Directory content: ${directoryContent.join(', ')}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
YoutubeDLWrapper
|
|
|
|
}
|