1 import execa from 'execa'
2 import { pathExists, writeFile } from 'fs-extra'
3 import { 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 this.updateYoutubeDLBinary()
21 return new YoutubeDLCLI()
24 static async updateYoutubeDLBinary () {
25 const url = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE.URL
27 logger.info('Updating youtubeDL binary from %s.', url, lTags())
29 const gotOptions = { context: { bodyKBLimit: 20_000 }, responseType: 'buffer' as 'buffer' }
32 let gotResult = await peertubeGot(url, gotOptions)
34 if (!isBinaryResponse(gotResult)) {
35 const json = JSON.parse(gotResult.body.toString())
36 const latest = json.filter(release => release.prerelease === false)[0]
37 if (!latest) throw new Error('Cannot find latest release')
39 const releaseName = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE.NAME
40 const releaseAsset = latest.assets.find(a => a.name === releaseName)
41 if (!releaseAsset) throw new Error(`Cannot find appropriate release with name ${releaseName} in release assets`)
43 gotResult = await peertubeGot(releaseAsset.browser_download_url, gotOptions)
46 if (!isBinaryResponse(gotResult)) {
47 throw new Error('Not a binary response')
50 await writeFile(youtubeDLBinaryPath, gotResult.body)
52 logger.info('youtube-dl updated %s.', youtubeDLBinaryPath, lTags())
54 logger.error('Cannot update youtube-dl from %s.', url, { err, ...lTags() })
58 static getYoutubeDLVideoFormat (enabledResolutions: VideoResolution[]) {
60 * list of format selectors in order or preference
61 * see https://github.com/ytdl-org/youtube-dl#format-selection
63 * case #1 asks for a mp4 using h264 (avc1) and the exact resolution in the hope
64 * of being able to do a "quick-transcode"
65 * case #2 is the first fallback. No "quick-transcode" means we can get anything else (like vp9)
66 * case #3 is the resolution-degraded equivalent of #1, and already a pretty safe fallback
68 * in any case we avoid AV1, see https://github.com/Chocobozzz/PeerTube/issues/3499
70 const resolution = enabledResolutions.length === 0
71 ? VideoResolution.H_720P
72 : Math.max(...enabledResolutions)
75 `bestvideo[vcodec^=avc1][height=${resolution}]+bestaudio[ext=m4a]`, // case #1
76 `bestvideo[vcodec!*=av01][vcodec!*=vp9.2][height=${resolution}]+bestaudio`, // case #2
77 `bestvideo[vcodec^=avc1][height<=${resolution}]+bestaudio[ext=m4a]`, // case #3
78 `bestvideo[vcodec!*=av01][vcodec!*=vp9.2]+bestaudio`,
79 'best[vcodec!*=av01][vcodec!*=vp9.2]', // case fallback for known formats
80 'best' // Ultimate fallback
84 private constructor () {
92 processOptions: execa.NodeOptions
94 additionalYoutubeDLArgs?: string[]
98 processOptions: options.processOptions,
99 timeout: options.timeout,
100 args: (options.additionalYoutubeDLArgs || []).concat([ '-f', options.format, '-o', options.output ])
104 async getInfo (options: {
107 processOptions: execa.NodeOptions
108 additionalYoutubeDLArgs?: string[]
110 const { url, format, additionalYoutubeDLArgs = [], processOptions } = options
112 const completeArgs = additionalYoutubeDLArgs.concat([ '--dump-json', '-f', format ])
114 const data = await this.run({ url, args: completeArgs, processOptions })
115 const info = data.map(this.parseInfo)
117 return info.length === 1
122 async getSubs (options: {
125 processOptions: execa.NodeOptions
127 const { url, format, processOptions } = options
129 const args = [ '--skip-download', '--all-subs', `--sub-format=${format}` ]
131 const data = await this.run({ url, args, processOptions })
132 const files: string[] = []
134 const skipString = '[info] Writing video subtitles to: '
136 for (let i = 0, len = data.length; i < len; i++) {
139 if (line.indexOf(skipString) === 0) {
140 files.push(line.slice(skipString.length))
147 private async run (options: {
151 processOptions: execa.NodeOptions
153 const { url, args, timeout, processOptions } = options
155 let completeArgs = this.wrapWithProxyOptions(args)
156 completeArgs = this.wrapWithIPOptions(completeArgs)
157 completeArgs = this.wrapWithFFmpegOptions(completeArgs)
159 const { PYTHON_PATH } = CONFIG.IMPORT.VIDEOS.HTTP.YOUTUBE_DL_RELEASE
160 const subProcess = execa(PYTHON_PATH, [ youtubeDLBinaryPath, ...completeArgs, url ], processOptions)
163 setTimeout(() => subProcess.cancel(), timeout)
166 const output = await subProcess
168 logger.debug('Runned youtube-dl command.', { command: output.command, ...lTags() })
171 ? output.stdout.trim().split(/\r?\n/)
175 private wrapWithProxyOptions (args: string[]) {
176 if (isProxyEnabled()) {
177 logger.debug('Using proxy %s for YoutubeDL', getProxy(), lTags())
179 return [ '--proxy', getProxy() ].concat(args)
185 private wrapWithIPOptions (args: string[]) {
186 if (CONFIG.IMPORT.VIDEOS.HTTP.FORCE_IPV4) {
187 logger.debug('Force ipv4 for YoutubeDL')
189 return [ '--force-ipv4' ].concat(args)
195 private wrapWithFFmpegOptions (args: string[]) {
196 if (process.env.FFMPEG_PATH) {
197 logger.debug('Using ffmpeg location %s for YoutubeDL', process.env.FFMPEG_PATH, lTags())
199 return [ '--ffmpeg-location', process.env.FFMPEG_PATH ].concat(args)
205 private parseInfo (data: string) {
206 return JSON.parse(data)