diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-08 16:50:56 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-08 16:50:56 +0200 |
commit | 9f1ddd249652c1e35b45db33885a00a005f9b059 (patch) | |
tree | 193c941c0019e897abf1e12faa504fff912f5546 | |
parent | edb4ffc7e0b13659d7c73b120f2c87b27e4c26a1 (diff) | |
download | PeerTube-9f1ddd249652c1e35b45db33885a00a005f9b059.tar.gz PeerTube-9f1ddd249652c1e35b45db33885a00a005f9b059.tar.zst PeerTube-9f1ddd249652c1e35b45db33885a00a005f9b059.zip |
Change a little bit optimize-old-videos logic
-rw-r--r-- | scripts/optimize-old-videos.ts | 37 | ||||
-rw-r--r-- | server/lib/video-transcoding.ts | 14 | ||||
-rw-r--r-- | server/models/video/video.ts | 10 |
3 files changed, 33 insertions, 28 deletions
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index ab44acfbe..02026b3da 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts | |||
@@ -1,11 +1,7 @@ | |||
1 | import { join } from 'path' | 1 | import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' |
2 | import { readdir } from 'fs-extra' | 2 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils' |
3 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' | ||
4 | import { getVideoFileResolution, getVideoFileBitrate, getVideoFileFPS } from '../server/helpers/ffmpeg-utils' | ||
5 | import { getMaxBitrate } from '../shared/models/videos' | 3 | import { getMaxBitrate } from '../shared/models/videos' |
6 | import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' | ||
7 | import { VideoModel } from '../server/models/video/video' | 4 | import { VideoModel } from '../server/models/video/video' |
8 | import { getUUIDFromFilename } from '../server/helpers/utils' | ||
9 | import { optimizeVideofile } from '../server/lib/video-transcoding' | 5 | import { optimizeVideofile } from '../server/lib/video-transcoding' |
10 | 6 | ||
11 | run() | 7 | run() |
@@ -16,21 +12,24 @@ run() | |||
16 | }) | 12 | }) |
17 | 13 | ||
18 | async function run () { | 14 | async function run () { |
19 | const files = await readdir(CONFIG.STORAGE.VIDEOS_DIR) | 15 | const localVideos = await VideoModel.listLocal() |
20 | for (const file of files) { | ||
21 | const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, file) | ||
22 | const videoBitrate = await getVideoFileBitrate(inputPath) | ||
23 | const fps = await getVideoFileFPS(inputPath) | ||
24 | const resolution = await getVideoFileResolution(inputPath) | ||
25 | const uuid = getUUIDFromFilename(file) | ||
26 | 16 | ||
27 | const isLocalVideo = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid) | 17 | for (const video of localVideos) { |
28 | const isMaxBitrateExceeded = | 18 | for (const file of video.VideoFiles) { |
29 | videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) | 19 | const inputPath = video.getVideoFilename(file) |
30 | if (uuid && isLocalVideo && isMaxBitrateExceeded) { | 20 | |
31 | const videoModel = await VideoModel.loadByUUIDWithFile(uuid) | 21 | const [ videoBitrate, fps, resolution ] = await Promise.all([ |
32 | await optimizeVideofile(videoModel, inputPath) | 22 | getVideoFileBitrate(inputPath), |
23 | getVideoFileFPS(inputPath), | ||
24 | getVideoFileResolution(inputPath) | ||
25 | ]) | ||
26 | |||
27 | const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) | ||
28 | if (isMaxBitrateExceeded) { | ||
29 | await optimizeVideofile(video, file) | ||
30 | } | ||
33 | } | 31 | } |
34 | } | 32 | } |
33 | |||
35 | console.log('Finished optimizing videos') | 34 | console.log('Finished optimizing videos') |
36 | } | 35 | } |
diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index 04cadf74b..a78de61e5 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { CONFIG } from '../initializers' | 1 | import { CONFIG } from '../initializers' |
2 | import { join, extname, basename } from 'path' | 2 | import { extname, join } from 'path' |
3 | import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils' | 3 | import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils' |
4 | import { copy, remove, rename, stat } from 'fs-extra' | 4 | import { copy, remove, rename, stat } from 'fs-extra' |
5 | import { logger } from '../helpers/logger' | 5 | import { logger } from '../helpers/logger' |
@@ -7,16 +7,12 @@ import { VideoResolution } from '../../shared/models/videos' | |||
7 | import { VideoFileModel } from '../models/video/video-file' | 7 | import { VideoFileModel } from '../models/video/video-file' |
8 | import { VideoModel } from '../models/video/video' | 8 | import { VideoModel } from '../models/video/video' |
9 | 9 | ||
10 | async function optimizeVideofile (video: VideoModel, videoInputPath?: string) { | 10 | async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) { |
11 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR | 11 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
12 | const newExtname = '.mp4' | 12 | const newExtname = '.mp4' |
13 | let inputVideoFile = null | 13 | |
14 | if (videoInputPath == null) { | 14 | const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile() |
15 | inputVideoFile = video.getOriginalFile() | 15 | const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) |
16 | videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) | ||
17 | } else { | ||
18 | inputVideoFile = basename(videoInputPath) | ||
19 | } | ||
20 | const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) | 16 | const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) |
21 | 17 | ||
22 | const transcodeOptions = { | 18 | const transcodeOptions = { |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 46d823240..070ac7623 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -788,6 +788,16 @@ export class VideoModel extends Model<VideoModel> { | |||
788 | return VideoModel.scope(ScopeNames.WITH_FILES).findAll() | 788 | return VideoModel.scope(ScopeNames.WITH_FILES).findAll() |
789 | } | 789 | } |
790 | 790 | ||
791 | static listLocal () { | ||
792 | const query = { | ||
793 | where: { | ||
794 | remote: false | ||
795 | } | ||
796 | } | ||
797 | |||
798 | return VideoModel.scope(ScopeNames.WITH_FILES).findAll(query) | ||
799 | } | ||
800 | |||
791 | static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { | 801 | static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { |
792 | function getRawQuery (select: string) { | 802 | function getRawQuery (select: string) { |
793 | const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + | 803 | const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + |