Change a little bit optimize-old-videos logic

This commit is contained in:
Chocobozzz 2018-10-08 16:50:56 +02:00
parent edb4ffc7e0
commit 9f1ddd2496
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 33 additions and 28 deletions

View File

@ -1,11 +1,7 @@
import { join } from 'path' import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
import { readdir } from 'fs-extra' import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
import { getVideoFileResolution, getVideoFileBitrate, getVideoFileFPS } from '../server/helpers/ffmpeg-utils'
import { getMaxBitrate } from '../shared/models/videos' import { getMaxBitrate } from '../shared/models/videos'
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
import { VideoModel } from '../server/models/video/video' import { VideoModel } from '../server/models/video/video'
import { getUUIDFromFilename } from '../server/helpers/utils'
import { optimizeVideofile } from '../server/lib/video-transcoding' import { optimizeVideofile } from '../server/lib/video-transcoding'
run() run()
@ -16,21 +12,24 @@ run()
}) })
async function run () { async function run () {
const files = await readdir(CONFIG.STORAGE.VIDEOS_DIR) const localVideos = await VideoModel.listLocal()
for (const file of files) {
const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, file)
const videoBitrate = await getVideoFileBitrate(inputPath)
const fps = await getVideoFileFPS(inputPath)
const resolution = await getVideoFileResolution(inputPath)
const uuid = getUUIDFromFilename(file)
const isLocalVideo = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid) for (const video of localVideos) {
const isMaxBitrateExceeded = for (const file of video.VideoFiles) {
videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) const inputPath = video.getVideoFilename(file)
if (uuid && isLocalVideo && isMaxBitrateExceeded) {
const videoModel = await VideoModel.loadByUUIDWithFile(uuid) const [ videoBitrate, fps, resolution ] = await Promise.all([
await optimizeVideofile(videoModel, inputPath) getVideoFileBitrate(inputPath),
getVideoFileFPS(inputPath),
getVideoFileResolution(inputPath)
])
const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
if (isMaxBitrateExceeded) {
await optimizeVideofile(video, file)
}
} }
} }
console.log('Finished optimizing videos') console.log('Finished optimizing videos')
} }

View File

@ -1,5 +1,5 @@
import { CONFIG } from '../initializers' import { CONFIG } from '../initializers'
import { join, extname, basename } from 'path' import { extname, join } from 'path'
import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils' import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
import { copy, remove, rename, stat } from 'fs-extra' import { copy, remove, rename, stat } from 'fs-extra'
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
@ -7,16 +7,12 @@ import { VideoResolution } from '../../shared/models/videos'
import { VideoFileModel } from '../models/video/video-file' import { VideoFileModel } from '../models/video/video-file'
import { VideoModel } from '../models/video/video' import { VideoModel } from '../models/video/video'
async function optimizeVideofile (video: VideoModel, videoInputPath?: string) { async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
const newExtname = '.mp4' const newExtname = '.mp4'
let inputVideoFile = null
if (videoInputPath == null) { const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
inputVideoFile = video.getOriginalFile() const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
} else {
inputVideoFile = basename(videoInputPath)
}
const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
const transcodeOptions = { const transcodeOptions = {

View File

@ -788,6 +788,16 @@ export class VideoModel extends Model<VideoModel> {
return VideoModel.scope(ScopeNames.WITH_FILES).findAll() return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
} }
static listLocal () {
const query = {
where: {
remote: false
}
}
return VideoModel.scope(ScopeNames.WITH_FILES).findAll(query)
}
static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
function getRawQuery (select: string) { function getRawQuery (select: string) {
const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +