import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../initializers/constants'
import { logger } from './logger'
import { generateVideoImportTmpPath } from './utils'
+import { getEnabledResolutions } from '../lib/video-transcoding'
import { join } from 'path'
import { peertubeTruncate, root } from './core-utils'
import { ensureDir, remove, writeFile } from 'fs-extra'
import { createWriteStream } from 'fs'
import { CONFIG } from '@server/initializers/config'
import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
+import { VideoResolution } from '../../shared/models/videos'
export type YoutubeDLInfo = {
name?: string
nsfw?: boolean
tags?: string[]
thumbnailUrl?: string
- fileExt?: string
+ ext?: string
+ mergeExt?: string
originallyPublishedAt?: Date
}
}
args = wrapWithProxyOptions(args)
+ args = [ '-f', getYoutubeDLVideoFormat() ].concat(args)
safeGetYoutubeDL()
.then(youtubeDL => {
youtubeDL.getInfo(url, args, processOptions, (err, info) => {
if (err) return rej(err)
if (info.is_live === true) return rej(new Error('Cannot download a live streaming.'))
+ if (info.format_id?.includes('+')) {
+ // this is a merge format and its extension will be appended
+ if (info.ext === 'mp4') {
+ info.mergeExt = 'mp4'
+ } else {
+ info.mergeExt = 'mkv'
+ }
+ }
const obj = buildVideoInfo(normalizeObject(info))
if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
})
}
-function downloadYoutubeDLVideo (url: string, extension: string, timeout: number) {
+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
+ ].join('/')
+}
+
+function downloadYoutubeDLVideo (url: string, extension: string, timeout: number, mergeExtension?: string) {
const path = generateVideoImportTmpPath(url, extension)
+ const finalPath = mergeExtension ? path.replace(new RegExp(`${extension}$`), mergeExtension) : path
let timer
- logger.info('Importing youtubeDL video %s to %s', url, path)
+ logger.info('Importing youtubeDL video %s to %s', url, finalPath)
- let options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
+ let options = [ '-f', getYoutubeDLVideoFormat(), '-o', path ]
options = wrapWithProxyOptions(options)
if (process.env.FFMPEG_PATH) {
return rej(err)
}
- return res(path)
+ return res(finalPath)
})
timer = setTimeout(() => {
export {
updateYoutubeDLBinary,
+ getYoutubeDLVideoFormat,
downloadYoutubeDLVideo,
getYoutubeDLSubs,
getYoutubeDLInfo,
tags: getTags(obj.tags),
thumbnailUrl: obj.thumbnail || undefined,
originallyPublishedAt: buildOriginallyPublishedAt(obj),
- fileExt: obj.ext
+ ext: obj.ext,
+ mergeExt: obj.mergeExt
}
}