1 import { move, pathExists, readdir, remove } from 'fs-extra'
2 import { dirname, join } from 'path'
3 import { CONFIG } from '@server/initializers/config'
4 import { isVideoFileExtnameValid } from '../custom-validators/videos'
5 import { logger, loggerTagsFactory } from '../logger'
6 import { generateVideoImportTmpPath } from '../utils'
7 import { YoutubeDLCLI } from './youtube-dl-cli'
8 import { YoutubeDLInfo, YoutubeDLInfoBuilder } from './youtube-dl-info-builder'
10 const lTags = loggerTagsFactory('youtube-dl')
12 export type YoutubeDLSubs = {
18 const processOptions = {
19 maxBuffer: 1024 * 1024 * 30 // 30MB
22 class YoutubeDLWrapper {
25 private readonly url: string,
26 private readonly enabledResolutions: number[],
27 private readonly useBestFormat: boolean
32 async getInfoForDownload (youtubeDLArgs: string[] = []): Promise<YoutubeDLInfo> {
33 const youtubeDL = await YoutubeDLCLI.safeGet()
35 const info = await youtubeDL.getInfo({
37 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
38 additionalYoutubeDLArgs: youtubeDLArgs,
42 if (!info) throw new Error(`YoutubeDL could not get info from ${this.url}`)
44 if (info.is_live === true) throw new Error('Cannot download a live streaming.')
46 const infoBuilder = new YoutubeDLInfoBuilder(info)
48 return infoBuilder.getInfo()
51 async getInfoForListImport (options: {
52 latestVideosCount?: number
54 const youtubeDL = await YoutubeDLCLI.safeGet()
56 const list = await youtubeDL.getListInfo({
58 latestVideosCount: options.latestVideosCount,
62 return list.map(info => {
63 const infoBuilder = new YoutubeDLInfoBuilder(info)
65 return infoBuilder.getInfo()
69 async getSubtitles (): Promise<YoutubeDLSubs> {
70 const cwd = CONFIG.STORAGE.TMP_DIR
72 const youtubeDL = await YoutubeDLCLI.safeGet()
74 const files = await youtubeDL.getSubs({ url: this.url, format: 'vtt', processOptions: { cwd } })
77 logger.debug('Get subtitles from youtube dl.', { url: this.url, files, ...lTags() })
79 const subtitles = files.reduce((acc, filename) => {
80 const matched = filename.match(/\.([a-z]{2})(-[a-z]+)?\.(vtt|ttml)/i)
81 if (!matched || !matched[1]) return acc
87 path: join(cwd, filename),
96 async downloadVideo (fileExt: string, timeout: number): Promise<string> {
97 // Leave empty the extension, youtube-dl will add it
98 const pathWithoutExtension = generateVideoImportTmpPath(this.url, '')
100 logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
102 const youtubeDL = await YoutubeDLCLI.safeGet()
105 await youtubeDL.download({
107 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
108 output: pathWithoutExtension,
113 // If youtube-dl did not guess an extension for our file, just use .mp4 as default
114 if (await pathExists(pathWithoutExtension)) {
115 await move(pathWithoutExtension, pathWithoutExtension + '.mp4')
118 return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
120 this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
122 logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
126 .catch(innerErr => logger.error('Cannot remove file in youtubeDL error.', { innerErr, ...lTags() }))
132 private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {
133 if (!isVideoFileExtnameValid(sourceExt)) {
134 throw new Error('Invalid video extension ' + sourceExt)
137 const extensions = [ sourceExt, '.mp4', '.mkv', '.webm' ]
139 for (const extension of extensions) {
140 const path = tmpPath + extension
142 if (await pathExists(path)) return path
145 const directoryContent = await readdir(dirname(tmpPath))
147 throw new Error(`Cannot guess path of ${tmpPath}. Directory content: ${directoryContent.join(', ')}`)
151 // ---------------------------------------------------------------------------