]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/youtube-dl/youtube-dl-wrapper.ts
Set scroll position at top of the textarea when opening the subtitle editor.
[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 = {
8296984d 19 maxBuffer: 1024 * 1024 * 30 // 30MB
62549e6c
C
20}
21
22class YoutubeDLWrapper {
23
5e2afe42
C
24 constructor (
25 private readonly url: string,
26 private readonly enabledResolutions: number[],
27 private readonly useBestFormat: boolean
28 ) {
62549e6c
C
29
30 }
31
32 async getInfoForDownload (youtubeDLArgs: string[] = []): Promise<YoutubeDLInfo> {
33 const youtubeDL = await YoutubeDLCLI.safeGet()
34
35 const info = await youtubeDL.getInfo({
36 url: this.url,
5e2afe42 37 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
62549e6c
C
38 additionalYoutubeDLArgs: youtubeDLArgs,
39 processOptions
40 })
41
ea139ca8
C
42 if (!info) throw new Error(`YoutubeDL could not get info from ${this.url}`)
43
62549e6c
C
44 if (info.is_live === true) throw new Error('Cannot download a live streaming.')
45
46 const infoBuilder = new YoutubeDLInfoBuilder(info)
47
48 return infoBuilder.getInfo()
49 }
50
2a491182
F
51 async getInfoForListImport (options: {
52 latestVideosCount?: number
53 }) {
54 const youtubeDL = await YoutubeDLCLI.safeGet()
55
56 const list = await youtubeDL.getListInfo({
57 url: this.url,
58 latestVideosCount: options.latestVideosCount,
59 processOptions
60 })
61
62 return list.map(info => {
63 const infoBuilder = new YoutubeDLInfoBuilder(info)
64
65 return infoBuilder.getInfo()
66 })
67 }
68
62549e6c
C
69 async getSubtitles (): Promise<YoutubeDLSubs> {
70 const cwd = CONFIG.STORAGE.TMP_DIR
71
72 const youtubeDL = await YoutubeDLCLI.safeGet()
73
74 const files = await youtubeDL.getSubs({ url: this.url, format: 'vtt', processOptions: { cwd } })
75 if (!files) return []
76
77 logger.debug('Get subtitles from youtube dl.', { url: this.url, files, ...lTags() })
78
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
82
83 return [
84 ...acc,
85 {
86 language: matched[1],
87 path: join(cwd, filename),
88 filename
89 }
90 ]
91 }, [])
92
93 return subtitles
94 }
95
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, '')
99
62549e6c
C
100 logger.info('Importing youtubeDL video %s to %s', this.url, pathWithoutExtension, lTags())
101
102 const youtubeDL = await YoutubeDLCLI.safeGet()
103
7630e1c8
C
104 try {
105 await youtubeDL.download({
106 url: this.url,
5e2afe42 107 format: YoutubeDLCLI.getYoutubeDLVideoFormat(this.enabledResolutions, this.useBestFormat),
7630e1c8
C
108 output: pathWithoutExtension,
109 timeout,
110 processOptions
62549e6c
C
111 })
112
7630e1c8
C
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')
116 }
d7ce63d3 117
7630e1c8
C
118 return this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
119 } catch (err) {
120 this.guessVideoPathWithExtension(pathWithoutExtension, fileExt)
121 .then(path => {
122 logger.debug('Error in youtube-dl import, deleting file %s.', path, { err, ...lTags() })
62549e6c 123
7630e1c8
C
124 return remove(path)
125 })
2a491182 126 .catch(innerErr => logger.error('Cannot remove file in youtubeDL error.', { innerErr, ...lTags() }))
7630e1c8
C
127
128 throw err
129 }
62549e6c
C
130 }
131
132 private async guessVideoPathWithExtension (tmpPath: string, sourceExt: string) {
133 if (!isVideoFileExtnameValid(sourceExt)) {
134 throw new Error('Invalid video extension ' + sourceExt)
135 }
136
137 const extensions = [ sourceExt, '.mp4', '.mkv', '.webm' ]
138
139 for (const extension of extensions) {
140 const path = tmpPath + extension
141
142 if (await pathExists(path)) return path
143 }
144
145 const directoryContent = await readdir(dirname(tmpPath))
146
147 throw new Error(`Cannot guess path of ${tmpPath}. Directory content: ${directoryContent.join(', ')}`)
148 }
149}
150
151// ---------------------------------------------------------------------------
152
153export {
154 YoutubeDLWrapper
155}