+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, '')
+
+ let timer
+
+ logger.info('Importing youtubeDL video %s to %s', url, pathWithoutExtension)
+
+ let options = [ '-f', getYoutubeDLVideoFormat(), '-o', pathWithoutExtension ]
+ options = wrapWithProxyOptions(options)
+
+ if (process.env.FFMPEG_PATH) {
+ options = options.concat([ '--ffmpeg-location', process.env.FFMPEG_PATH ])
+ }
+
+ logger.debug('YoutubeDL options for %s.', url, { options })
+
+ return new Promise<string>((res, rej) => {
+ safeGetYoutubeDL()
+ .then(youtubeDL => {
+ youtubeDL.exec(url, options, processOptions, async err => {
+ clearTimeout(timer)