Fix resolution for portrait videos

This commit is contained in:
Chocobozzz 2018-02-27 15:57:28 +01:00
parent 6fdc553adb
commit 056aa7f2b4
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 27 additions and 19 deletions

View File

@ -3,7 +3,7 @@ import { extname, join } from 'path'
import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared' import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
import { renamePromise } from '../../../helpers/core-utils' import { renamePromise } from '../../../helpers/core-utils'
import { retryTransactionWrapper } from '../../../helpers/database-utils' import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils' import { getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
import { processImage } from '../../../helpers/image-utils' import { processImage } from '../../../helpers/image-utils'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
@ -187,11 +187,11 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
const video = new VideoModel(videoData) const video = new VideoModel(videoData)
video.url = getVideoActivityPubUrl(video) video.url = getVideoActivityPubUrl(video)
const videoFileHeight = await getVideoFileHeight(videoPhysicalFile.path) const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
const videoFileData = { const videoFileData = {
extname: extname(videoPhysicalFile.filename), extname: extname(videoPhysicalFile.filename),
resolution: videoFileHeight, resolution: videoFileResolution,
size: videoPhysicalFile.size size: videoPhysicalFile.size
} }
const videoFile = new VideoFileModel(videoFileData) const videoFile = new VideoFileModel(videoFileData)

View File

@ -6,9 +6,13 @@ import { unlinkPromise } from './core-utils'
import { processImage } from './image-utils' import { processImage } from './image-utils'
import { logger } from './logger' import { logger } from './logger'
async function getVideoFileHeight (path: string) { async function getVideoFileResolution (path: string) {
const videoStream = await getVideoFileStream(path) const videoStream = await getVideoFileStream(path)
return videoStream.height
return {
videoFileResolution: Math.min(videoStream.height, videoStream.width),
isPortraitMode: videoStream.height > videoStream.width
}
} }
async function getVideoFileFPS (path: string) { async function getVideoFileFPS (path: string) {
@ -74,6 +78,7 @@ type TranscodeOptions = {
inputPath: string inputPath: string
outputPath: string outputPath: string
resolution?: VideoResolution resolution?: VideoResolution
isPortraitMode?: boolean
} }
function transcode (options: TranscodeOptions) { function transcode (options: TranscodeOptions) {
@ -90,7 +95,8 @@ function transcode (options: TranscodeOptions) {
if (fps > MAX_VIDEO_TRANSCODING_FPS) command = command.withFPS(MAX_VIDEO_TRANSCODING_FPS) if (fps > MAX_VIDEO_TRANSCODING_FPS) command = command.withFPS(MAX_VIDEO_TRANSCODING_FPS)
if (options.resolution !== undefined) { if (options.resolution !== undefined) {
const size = `?x${options.resolution}` // '?x720' for example // '?x720' or '720x?' for example
const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
command = command.size(size) command = command.size(size)
} }
@ -103,7 +109,7 @@ function transcode (options: TranscodeOptions) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
getVideoFileHeight, getVideoFileResolution,
getDurationFromVideoFile, getDurationFromVideoFile,
generateImageFromVideoFile, generateImageFromVideoFile,
transcode, transcode,

View File

@ -3,7 +3,7 @@ import { join } from 'path'
import { readdirPromise, renamePromise } from '../../helpers/core-utils' import { readdirPromise, renamePromise } from '../../helpers/core-utils'
import { CONFIG } from '../../initializers/constants' import { CONFIG } from '../../initializers/constants'
import { getVideoFileHeight } from '../../helpers/ffmpeg-utils' import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
function up (utils: { function up (utils: {
transaction: Sequelize.Transaction, transaction: Sequelize.Transaction,
@ -27,7 +27,7 @@ function up (utils: {
const uuid = matches[1] const uuid = matches[1]
const ext = matches[2] const ext = matches[2]
const p = getVideoFileHeight(join(videoFileDir, videoFile)) const p = getVideoFileResolution(join(videoFileDir, videoFile))
.then(height => { .then(height => {
const oldTorrentName = uuid + '.torrent' const oldTorrentName = uuid + '.torrent'
const newTorrentName = uuid + '-' + height + '.torrent' const newTorrentName = uuid + '-' + height + '.torrent'

View File

@ -11,7 +11,8 @@ import { JobQueue } from '../job-queue'
export type VideoFilePayload = { export type VideoFilePayload = {
videoUUID: string videoUUID: string
resolution?: VideoResolution resolution?: VideoResolution,
isPortraitMode?: boolean
} }
async function processVideoFile (job: kue.Job) { async function processVideoFile (job: kue.Job) {
@ -27,7 +28,7 @@ async function processVideoFile (job: kue.Job) {
// Transcoding in other resolution // Transcoding in other resolution
if (payload.resolution) { if (payload.resolution) {
await video.transcodeOriginalVideofile(payload.resolution) await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
await onVideoFileTranscoderSuccess(video) await onVideoFileTranscoderSuccess(video)
} else { } else {
await video.optimizeOriginalVideofile() await video.optimizeOriginalVideofile()
@ -66,12 +67,12 @@ async function onVideoFileOptimizerSuccess (video: VideoModel) {
await shareVideoByServerAndChannel(video, undefined) await shareVideoByServerAndChannel(video, undefined)
} }
const originalFileHeight = await videoDatabase.getOriginalFileHeight() const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
// Create transcoding jobs if there are enabled resolutions // Create transcoding jobs if there are enabled resolutions
const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight) const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
logger.info( logger.info(
'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight, 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
{ resolutions: resolutionsEnabled } { resolutions: resolutionsEnabled }
) )

View File

@ -42,7 +42,7 @@ import {
isVideoNameValid, isVideoNameValid,
isVideoPrivacyValid, isVideoSupportValid isVideoPrivacyValid, isVideoSupportValid
} from '../../helpers/custom-validators/videos' } from '../../helpers/custom-validators/videos'
import { generateImageFromVideoFile, getVideoFileHeight, transcode } from '../../helpers/ffmpeg-utils' import { generateImageFromVideoFile, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils'
import { logger } from '../../helpers/logger' import { logger } from '../../helpers/logger'
import { getServerActor } from '../../helpers/utils' import { getServerActor } from '../../helpers/utils'
import { import {
@ -1140,7 +1140,7 @@ export class VideoModel extends Model<VideoModel> {
} }
} }
transcodeOriginalVideofile = async function (resolution: VideoResolution) { transcodeOriginalVideofile = async function (resolution: VideoResolution, isPortraitMode: boolean) {
const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
const extname = '.mp4' const extname = '.mp4'
@ -1158,7 +1158,8 @@ export class VideoModel extends Model<VideoModel> {
const transcodeOptions = { const transcodeOptions = {
inputPath: videoInputPath, inputPath: videoInputPath,
outputPath: videoOutputPath, outputPath: videoOutputPath,
resolution resolution,
isPortraitMode
} }
await transcode(transcodeOptions) await transcode(transcodeOptions)
@ -1174,10 +1175,10 @@ export class VideoModel extends Model<VideoModel> {
this.VideoFiles.push(newVideoFile) this.VideoFiles.push(newVideoFile)
} }
getOriginalFileHeight () { getOriginalFileResolution () {
const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
return getVideoFileHeight(originalFilePath) return getVideoFileResolution(originalFilePath)
} }
getDescriptionPath () { getDescriptionPath () {