1
0
Fork 0

Add video aspect ratio in server

This commit is contained in:
Chocobozzz 2024-02-27 11:18:56 +01:00
parent c75381208f
commit b6b1aaa56f
No known key found for this signature in database
GPG key ID: 583A612D890159BE
52 changed files with 345 additions and 237 deletions

View file

@ -1,22 +1,22 @@
import { Job } from 'bullmq'
import { move, remove } from 'fs-extra/esm'
import { copyFile, stat } from 'fs/promises'
import { copyFile } from 'fs/promises'
import { basename, join } from 'path'
import { FileStorage } from '@peertube/peertube-models'
import { computeOutputFPS } from '@server/helpers/ffmpeg/index.js'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
import { ffprobePromise, getVideoStreamDuration, getVideoStreamFPS, TranscodeVODOptionsType } from '@peertube/peertube-ffmpeg'
import { getVideoStreamDuration, TranscodeVODOptionsType } from '@peertube/peertube-ffmpeg'
import { CONFIG } from '../../initializers/config.js'
import { VideoFileModel } from '../../models/video/video-file.js'
import { JobQueue } from '../job-queue/index.js'
import { generateWebVideoFilename } from '../paths.js'
import { buildFileMetadata } from '../video-file.js'
import { buildNewFile } from '../video-file.js'
import { VideoPathManager } from '../video-path-manager.js'
import { buildFFmpegVOD } from './shared/index.js'
import { buildOriginalFileResolution } from './transcoding-resolutions.js'
import { buildStoryboardJobIfNeeded } from '../video-jobs.js'
import { buildAspectRatio } from '@peertube/peertube-core-utils'
// Optimize the original video file and replace it. The resolution is not changed.
export async function optimizeOriginalVideofile (options: {
@ -62,19 +62,7 @@ export async function optimizeOriginalVideofile (options: {
fps
})
// Important to do this before getVideoFilename() to take in account the new filename
inputVideoFile.resolution = resolution
inputVideoFile.extname = newExtname
inputVideoFile.filename = generateWebVideoFilename(resolution, newExtname)
inputVideoFile.storage = FileStorage.FILE_SYSTEM
const { videoFile } = await onWebVideoFileTranscoding({
video,
videoFile: inputVideoFile,
videoOutputPath
})
await remove(videoInputPath)
const { videoFile } = await onWebVideoFileTranscoding({ video, videoOutputPath, deleteWebInputVideoFile: inputVideoFile })
return { transcodeType, videoFile }
})
@ -104,15 +92,8 @@ export async function transcodeNewWebVideoResolution (options: {
const file = video.getMaxQualityFile().withVideoOrPlaylist(video)
const result = await VideoPathManager.Instance.makeAvailableVideoFile(file, async videoInputPath => {
const newVideoFile = new VideoFileModel({
resolution,
extname: newExtname,
filename: generateWebVideoFilename(resolution, newExtname),
size: 0,
videoId: video.id
})
const videoOutputPath = join(transcodeDirectory, newVideoFile.filename)
const filename = generateWebVideoFilename(resolution, newExtname)
const videoOutputPath = join(transcodeDirectory, filename)
const transcodeOptions = {
type: 'video' as 'video',
@ -128,7 +109,7 @@ export async function transcodeNewWebVideoResolution (options: {
await buildFFmpegVOD(job).transcode(transcodeOptions)
return onWebVideoFileTranscoding({ video, videoFile: newVideoFile, videoOutputPath })
return onWebVideoFileTranscoding({ video, videoOutputPath })
})
return result
@ -188,20 +169,10 @@ export async function mergeAudioVideofile (options: {
throw err
}
// Important to do this before getVideoFilename() to take in account the new file extension
inputVideoFile.extname = newExtname
inputVideoFile.resolution = resolution
inputVideoFile.filename = generateWebVideoFilename(inputVideoFile.resolution, newExtname)
// ffmpeg generated a new video file, so update the video duration
// See https://trac.ffmpeg.org/ticket/5456
video.duration = await getVideoStreamDuration(videoOutputPath)
await video.save()
return onWebVideoFileTranscoding({
await onWebVideoFileTranscoding({
video,
videoFile: inputVideoFile,
videoOutputPath,
deleteWebInputVideoFile: inputVideoFile,
wasAudioFile: true
})
})
@ -214,36 +185,42 @@ export async function mergeAudioVideofile (options: {
export async function onWebVideoFileTranscoding (options: {
video: MVideoFullLight
videoFile: MVideoFile
videoOutputPath: string
wasAudioFile?: boolean // default false
deleteWebInputVideoFile?: MVideoFile
}) {
const { video, videoFile, videoOutputPath, wasAudioFile } = options
const { video, videoOutputPath, wasAudioFile, deleteWebInputVideoFile } = options
const mutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
const videoFile = await buildNewFile({ mode: 'web-video', path: videoOutputPath })
videoFile.videoId = video.id
try {
await video.reload()
// ffmpeg generated a new video file, so update the video duration
// See https://trac.ffmpeg.org/ticket/5456
if (wasAudioFile) {
video.duration = await getVideoStreamDuration(videoOutputPath)
video.aspectRatio = buildAspectRatio({ width: videoFile.width, height: videoFile.height })
await video.save()
}
const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, videoFile)
const stats = await stat(videoOutputPath)
const probe = await ffprobePromise(videoOutputPath)
const fps = await getVideoStreamFPS(videoOutputPath, probe)
const metadata = await buildFileMetadata(videoOutputPath, probe)
await move(videoOutputPath, outputPath, { overwrite: true })
videoFile.size = stats.size
videoFile.fps = fps
videoFile.metadata = metadata
await createTorrentAndSetInfoHash(video, videoFile)
const oldFile = await VideoFileModel.loadWebVideoFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })
if (oldFile) await video.removeWebVideoFile(oldFile)
if (deleteWebInputVideoFile) {
await video.removeWebVideoFile(deleteWebInputVideoFile)
await deleteWebInputVideoFile.destroy()
}
await VideoFileModel.customUpsert(videoFile, 'video', undefined)
video.VideoFiles = await video.$get('VideoFiles')