]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/youtube-dl/youtube-dl-wrapper.ts
Refactor video views
[github/Chocobozzz/PeerTube.git] / server / helpers / youtube-dl / youtube-dl-wrapper.ts
CommitLineData
62549e6c
C
1import { move, pathExists, readdir, remove } from 'fs-extra'
2import { dirname, join } from 'path'
3import { CONFIG } from '@server/initializers/config'
4import { isVideoFileExtnameValid } from '../custom-validators/videos'
5import { logger, loggerTagsFactory } from '../logger'
6import { generateVideoImportTmpPath } from '../utils'
7import { YoutubeDLCLI } from './youtube-dl-cli'
8import { YoutubeDLInfo, YoutubeDLInfoBuilder } from './youtube-dl-info-builder'
9
10const lTags = loggerTagsFactory('youtube-dl')
11
12export type YoutubeDLSubs = {
13 language: string
14 filename: string
15 path: string
16}[]
17
18const processOptions = {
19 maxBuffer: 1024 * 1024 * 10 // 10MB
20}
21
22class 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
62549e6c
C
76 logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
77
78 const youtubeDL = await YoutubeDLCLI.safeGet()
79
5480933b 80 let timer: NodeJS.Timeout
62549e6c
C
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(async err => {
102 const path = await this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
103
5480933b 104 logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
62549e6c
C
105 remove(path)
106 .catch(err => logger.error('Cannot remove file in youtubeDL timeout.', { err, ...lTags() }))
107
108 throw err
109 })
110 }
111
112 private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {
113 if (!isVideoFileExtnameValid(sourceExt)) {
114 throw new Error('Invalid video extension ' + sourceExt)
115 }
116
117 const extensions = [ sourceExt, '.mp4', '.mkv', '.webm' ]
118
119 for (const extension of extensions) {
120 const path = tmpPath + extension
121
122 if (await pathExists(path)) return path
123 }
124
125 const directoryContent = await readdir(dirname(tmpPath))
126
127 throw new Error(`Cannot guess path of ${tmpPath}. Directory content: ${directoryContent.join(', ')}`)
128 }
129}
130
131// ---------------------------------------------------------------------------
132
133export {
134 YoutubeDLWrapper
135}