1 import execa from 'execa'
2 import { ensureDir, pathExists, writeFile } from 'fs-extra'
3 import { dirname, join } from 'path'
4 import { CONFIG } from '@server/initializers/config'
5 import { VideoResolution } from '@shared/models'
6 import { logger, loggerTagsFactory } from '../logger'
7 import { getProxy, isProxyEnabled } from '../proxy'
8 import { isBinaryResponse, peertubeGot } from '../requests'
10 const lTags = loggerTagsFactory('youtube-dl')
12 const youtubeDLBinaryPath = join(CONFIG.STORAGE.BIN_DIR, CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE.NAME)
14 export class YoutubeDLCLI {
16 static async safeGet () {
17 if (!await pathExists(youtubeDLBinaryPath)) {
18 await ensureDir(dirname(youtubeDLBinaryPath))
20 await this.updateYoutubeDLBinary()
23 return new YoutubeDLCLI()
26 static async updateYoutubeDLBinary () {
27 const url = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE.URL
29 logger.info('Updating youtubeDL binary from %s.', url, lTags())
31 const gotOptions = { context: { bodyKBLimit: 20_000 }, responseType: 'buffer' as 'buffer' }
34 let gotResult = await peertubeGot(url, gotOptions)
36 if (!isBinaryResponse(gotResult)) {
37 const json = JSON.parse(gotResult.body.toString())
38 const latest = json.filter(release => release.prerelease === false)[0]
39 if (!latest) throw new Error('Cannot find latest release')
41 const releaseName = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE.NAME
42 const releaseAsset = latest.assets.find(a => a.name === releaseName)
43 if (!releaseAsset) throw new Error(`Cannot find appropriate release with name ${releaseName} in release assets`)
45 gotResult = await peertubeGot(releaseAsset.browser_download_url, gotOptions)
48 if (!isBinaryResponse(gotResult)) {
49 throw new Error('Not a binary response')
52 await writeFile(youtubeDLBinaryPath, gotResult.body)
54 logger.info('youtube-dl updated %s.', youtubeDLBinaryPath, lTags())
56 logger.error('Cannot update youtube-dl from %s.', url, { err, ...lTags() })
60 static getYoutubeDLVideoFormat (enabledResolutions: VideoResolution[]) {
62 * list of format selectors in order or preference
63 * see https://github.com/ytdl-org/youtube-dl#format-selection
65 * case #1 asks for a mp4 using h264 (avc1) and the exact resolution in the hope
66 * of being able to do a "quick-transcode"
67 * case #2 is the first fallback. No "quick-transcode" means we can get anything else (like vp9)
68 * case #3 is the resolution-degraded equivalent of #1, and already a pretty safe fallback
70 * in any case we avoid AV1, see https://github.com/Chocobozzz/PeerTube/issues/3499
72 const resolution = enabledResolutions.length === 0
73 ? VideoResolution.H_720P
74 : Math.max(...enabledResolutions)
77 `bestvideo[vcodec^=avc1][height=${resolution}]+bestaudio[ext=m4a]`, // case #1
78 `bestvideo[vcodec!*=av01][vcodec!*=vp9.2][height=${resolution}]+bestaudio`, // case #2
79 `bestvideo[vcodec^=avc1][height<=${resolution}]+bestaudio[ext=m4a]`, // case #3
80 `bestvideo[vcodec!*=av01][vcodec!*=vp9.2]+bestaudio`,
81 'best[vcodec!*=av01][vcodec!*=vp9.2]', // case fallback for known formats
82 'best' // Ultimate fallback
86 private constructor () {
94 processOptions: execa.NodeOptions
96 additionalYoutubeDLArgs?: string[]
100 processOptions: options.processOptions,
101 timeout: options.timeout,
102 args: (options.additionalYoutubeDLArgs || []).concat([ '-f', options.format, '-o', options.output ])
106 async getInfo (options: {
109 processOptions: execa.NodeOptions
110 additionalYoutubeDLArgs?: string[]
112 const { url, format, additionalYoutubeDLArgs = [], processOptions } = options
114 const completeArgs = additionalYoutubeDLArgs.concat([ '--dump-json', '-f', format ])
116 const data = await this.run({ url, args: completeArgs, processOptions })
117 const info = data.map(this.parseInfo)
119 return info.length === 1
124 async getSubs (options: {
127 processOptions: execa.NodeOptions
129 const { url, format, processOptions } = options
131 const args = [ '--skip-download', '--all-subs', `--sub-format=${format}` ]
133 const data = await this.run({ url, args, processOptions })
134 const files: string[] = []
136 const skipString = '[info] Writing video subtitles to: '
138 for (let i = 0, len = data.length; i < len; i++) {
141 if (line.indexOf(skipString) === 0) {
142 files.push(line.slice(skipString.length))
149 private async run (options: {
153 processOptions: execa.NodeOptions
155 const { url, args, timeout, processOptions } = options
157 let completeArgs = this.wrapWithProxyOptions(args)
158 completeArgs = this.wrapWithIPOptions(completeArgs)
159 completeArgs = this.wrapWithFFmpegOptions(completeArgs)
161 const { PYTHON_PATH } = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE
162 const subProcess = execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions)
165 setTimeout(() => subProcess.cancel(), timeout)
168 const output = await subProcess
170 logger.debug('Runned youtube-dl command.', { command: output.command, ...lTags() })
173 ? output.stdout.trim().split(/\r?\n/)
177 private wrapWithProxyOptions (args: string[]) {
178 if (isProxyEnabled()) {
179 logger.debug('Using proxy %s for YoutubeDL', getProxy(), lTags())
181 return [ '--proxy', getProxy() ].concat(args)
187 private wrapWithIPOptions (args: string[]) {
188 if (CONFIG.IMPORT.VIDEOS.HTTP.FORCE_IPV4) {
189 logger.debug('Force ipv4 for YoutubeDL')
191 return [ '--force-ipv4' ].concat(args)
197 private wrapWithFFmpegOptions (args: string[]) {
198 if (process.env.FFMPEG_PATH) {
199 logger.debug('Using ffmpeg location %s for YoutubeDL', process.env.FFMPEG_PATH, lTags())
201 return [ '--ffmpeg-location', process.env.FFMPEG_PATH ].concat(args)
207 private parseInfo (data: string) {
208 return JSON.parse(data)