+function getYoutubeDLSubs (url: string, opts?: object): Promise<YoutubeDLSubs> {
+ return new Promise<YoutubeDLSubs>((res, rej) => {
+ const cwd = CONFIG.STORAGE.TMP_DIR
+ const options = opts || { all: true, format: 'vtt', cwd }
+
+ safeGetYoutubeDL()
+ .then(youtubeDL => {
+ youtubeDL.getSubs(url, options, (err, files) => {
+ if (err) return rej(err)
+ if (!files) return []
+
+ logger.debug('Get subtitles from youtube dl.', { url, files })
+
+ const subtitles = files.reduce((acc, filename) => {
+ const matched = filename.match(/\.([a-z]{2})\.(vtt|ttml)/i)
+ if (!matched || !matched[1]) return acc
+
+ return [
+ ...acc,
+ {
+ language: matched[1],
+ path: join(cwd, filename),
+ filename
+ }
+ ]
+ }, [])
+
+ return res(subtitles)
+ })
+ })
+ .catch(err => rej(err))
+ })
+}
+
+function getYoutubeDLVideoFormat () {
+ /**
+ * list of format selectors in order or preference
+ * see https://github.com/ytdl-org/youtube-dl#format-selection
+ *
+ * case #1 asks for a mp4 using h264 (avc1) and the exact resolution in the hope
+ * of being able to do a "quick-transcode"
+ * case #2 is the first fallback. No "quick-transcode" means we can get anything else (like vp9)
+ * case #3 is the resolution-degraded equivalent of #1, and already a pretty safe fallback
+ *
+ * in any case we avoid AV1, see https://github.com/Chocobozzz/PeerTube/issues/3499
+ **/
+ const enabledResolutions = getEnabledResolutions('vod')
+ const resolution = enabledResolutions.length === 0
+ ? VideoResolution.H_720P
+ : Math.max(...enabledResolutions)
+
+ return [
+ `bestvideo[vcodec^=avc1][height=${resolution}]+bestaudio[ext=m4a]`, // case #1
+ `bestvideo[vcodec!*=av01][vcodec!*=vp9.2][height=${resolution}]+bestaudio`, // case #2
+ `bestvideo[vcodec^=avc1][height<=${resolution}]+bestaudio[ext=m4a]`, // case #3
+ `bestvideo[vcodec!*=av01][vcodec!*=vp9.2]+bestaudio`,
+ 'best[vcodec!*=av01][vcodec!*=vp9.2]', // case fallback for known formats
+ 'best' // Ultimate fallback
+ ].join('/')
+}
+
+function downloadYoutubeDLVideo (url: string, fileExt: string, timeout: number) {
+ // Leave empty the extension, youtube-dl will add it
+ const pathWithoutExtension = generateVideoImportTmpPath(url, '')
+