Fix resolution for portrait videos

pull/318/head
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 { renamePromise } from '../../../helpers/core-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 { logger } from '../../../helpers/logger'
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)
video.url = getVideoActivityPubUrl(video)
const videoFileHeight = await getVideoFileHeight(videoPhysicalFile.path)
const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
const videoFileData = {
extname: extname(videoPhysicalFile.filename),
resolution: videoFileHeight,
resolution: videoFileResolution,
size: videoPhysicalFile.size
}
const videoFile = new VideoFileModel(videoFileData)

View File

@ -6,9 +6,13 @@ import { unlinkPromise } from './core-utils'
import { processImage } from './image-utils'
import { logger } from './logger'
async function getVideoFileHeight (path: string) {
async function getVideoFileResolution (path: string) {
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) {
@ -74,6 +78,7 @@ type TranscodeOptions = {
inputPath: string
outputPath: string
resolution?: VideoResolution
isPortraitMode?: boolean
}
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 (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)
}
@ -103,7 +109,7 @@ function transcode (options: TranscodeOptions) {
// ---------------------------------------------------------------------------
export {
getVideoFileHeight,
getVideoFileResolution,
getDurationFromVideoFile,
generateImageFromVideoFile,
transcode,

View File

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

View File

@ -11,7 +11,8 @@ import { JobQueue } from '../job-queue'
export type VideoFilePayload = {
videoUUID: string
resolution?: VideoResolution
resolution?: VideoResolution,
isPortraitMode?: boolean
}
async function processVideoFile (job: kue.Job) {
@ -27,7 +28,7 @@ async function processVideoFile (job: kue.Job) {
// Transcoding in other resolution
if (payload.resolution) {
await video.transcodeOriginalVideofile(payload.resolution)
await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
await onVideoFileTranscoderSuccess(video)
} else {
await video.optimizeOriginalVideofile()
@ -66,12 +67,12 @@ async function onVideoFileOptimizerSuccess (video: VideoModel) {
await shareVideoByServerAndChannel(video, undefined)
}
const originalFileHeight = await videoDatabase.getOriginalFileHeight()
const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
// Create transcoding jobs if there are enabled resolutions
const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
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 }
)

View File

@ -42,7 +42,7 @@ import {
isVideoNameValid,
isVideoPrivacyValid, isVideoSupportValid
} 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 { getServerActor } from '../../helpers/utils'
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 extname = '.mp4'
@ -1158,7 +1158,8 @@ export class VideoModel extends Model<VideoModel> {
const transcodeOptions = {
inputPath: videoInputPath,
outputPath: videoOutputPath,
resolution
resolution,
isPortraitMode
}
await transcode(transcodeOptions)
@ -1174,10 +1175,10 @@ export class VideoModel extends Model<VideoModel> {
this.VideoFiles.push(newVideoFile)
}
getOriginalFileHeight () {
getOriginalFileResolution () {
const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
return getVideoFileHeight(originalFilePath)
return getVideoFileResolution(originalFilePath)
}
getDescriptionPath () {