]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/youtube-dl/youtube-dl-wrapper.ts
Remove contributors list from /about/peertube
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-wrapper.ts
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'
9
10 const lTags = loggerTagsFactory('youtube-dl')
11
12 export type YoutubeDLSubs = {
13 language: string
14 filename: string
15 path: string
16 }[]
17
18 const processOptions = {
19 maxBuffer: 1024 * 1024 * 10 // 10MB
20 }
21
22 class YoutubeDLWrapper {
23
24 constructor (private readonly url: string = '', private readonly enabledResolutions: number[] = []) {
25
26 }
27
28 async getInfoForDownload (youtubeDLArgs: string[] = []): Promise<YoutubeDLInfo> {
29 const youtubeDL = await YoutubeDLCLI.safeGet()
30
31 const info = await youtubeDL.getInfo({
32 url: this.url,
33 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions),
34 additionalYoutubeDLArgs: youtubeDLArgs,
35 processOptions
36 })
37
38 if (info.is_live === true) throw new Error('Cannot download a live streaming.')
39
40 const infoBuilder = new YoutubeDLInfoBuilder(info)
41
42 return infoBuilder.getInfo()
43 }
44
45 async getSubtitles (): Promise<YoutubeDLSubs> {
46 const cwd = CONFIG.STORAGE.TMP_DIR
47
48 const youtubeDL = await YoutubeDLCLI.safeGet()
49
50 const files = await youtubeDL.getSubs({ url: this.url, format: 'vtt', processOptions: { cwd } })
51 if (!files) return []
52
53 logger.debug('Get subtitles from youtube dl.', { url: this.url, files, ...lTags() })
54
55 const subtitles = files.reduce((acc, filename) => {
56 const matched = filename.match(/\.([a-z]{2})(-[a-z]+)?\.(vtt|ttml)/i)
57 if (!matched || !matched[1]) return acc
58
59 return [
60 ...acc,
61 {
62 language: matched[1],
63 path: join(cwd, filename),
64 filename
65 }
66 ]
67 }, [])
68
69 return subtitles
70 }
71
72 async downloadVideo (fileExt: string, timeout: number): Promise<string> {
73 // Leave empty the extension, youtube-dl will add it
74 const pathWithoutExtension = generateVideoImportTmpPath(this.url, '')
75
76 logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
77
78 const youtubeDL = await YoutubeDLCLI.safeGet()
79
80 let timer: NodeJS.Timeout
81 const timeoutPromise = new Promise<string>((_, rej) => {
82 timer = setTimeout(() => rej(new Error('YoutubeDL download timeout.')), timeout)
83 })
84
85 const downloadPromise = youtubeDL.download({
86 url: this.url,
87 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions),
88 output: pathWithoutExtension,
89 processOptions
90 }).then(() => clearTimeout(timer))
91 .then(async () => {
92 // If youtube-dl did not guess an extension for our file, just use .mp4 as default
93 if (await pathExists(pathWithoutExtension)) {
94 await move(pathWithoutExtension, pathWithoutExtension + '.mp4')
95 }
96
97 return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
98 })
99
100 return Promise.race([ downloadPromise, timeoutPromise ])
101 .catch(err => {
102 this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
103 .then(path => {
104 logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
105
106 return remove(path)
107 })
108 .catch(innerErr => logger.error('Cannot remove file in youtubeDL timeout.', { innerErr, ...lTags() }))
109
110 throw err
111 })
112 }
113
114 private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {
115 if (!isVideoFileExtnameValid(sourceExt)) {
116 throw new Error('Invalid video extension ' + sourceExt)
117 }
118
119 const extensions = [ sourceExt, '.mp4', '.mkv', '.webm' ]
120
121 for (const extension of extensions) {
122 const path = tmpPath + extension
123
124 if (await pathExists(path)) return path
125 }
126
127 const directoryContent = await readdir(dirname(tmpPath))
128
129 throw new Error(`Cannot guess path of ${tmpPath}. Directory content: ${directoryContent.join(', ')}`)
130 }
131 }
132
133 // ---------------------------------------------------------------------------
134
135 export {
136 YoutubeDLWrapper
137 }