From d846d99c6c81028bb7bd3cb20abd433cbf396a22 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 28 Oct 2020 10:49:20 +0100 Subject: Add modal to display live information --- server/lib/job-queue/handlers/video-live-ending.ts | 12 +++++++++- server/lib/job-queue/handlers/video-transcoding.ts | 23 +----------------- server/lib/video.ts | 28 +++++++++++++++++++++- 3 files changed, 39 insertions(+), 24 deletions(-) (limited to 'server') diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts index cd5bb1d1c..32eeff4d1 100644 --- a/server/lib/job-queue/handlers/video-live-ending.ts +++ b/server/lib/job-queue/handlers/video-live-ending.ts @@ -1,7 +1,8 @@ import * as Bull from 'bull' import { readdir, remove } from 'fs-extra' import { join } from 'path' -import { getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' +import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' +import { publishAndFederateIfNeeded } from '@server/lib/video' import { getHLSDirectory } from '@server/lib/video-paths' import { generateHlsPlaylist } from '@server/lib/video-transcoding' import { VideoModel } from '@server/models/video/video' @@ -44,6 +45,7 @@ async function saveLive (video: MVideo, live: MVideoLive) { const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8') const resolutions: number[] = [] + let duration: number for (const playlistFile of playlistFiles) { const playlistPath = join(hlsDirectory, playlistFile) @@ -58,6 +60,10 @@ async function saveLive (video: MVideo, live: MVideoLive) { const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpName) + if (!duration) { + duration = await getDurationFromVideoFile(mp4TmpName) + } + resolutions.push(videoFileResolution) } @@ -67,6 +73,8 @@ async function saveLive (video: MVideo, live: MVideoLive) { video.isLive = false video.state = VideoState.TO_TRANSCODE + video.duration = duration + await video.save() const videoWithFiles = await VideoModel.loadWithFiles(video.id) @@ -86,6 +94,8 @@ async function saveLive (video: MVideo, live: MVideoLive) { video.state = VideoState.PUBLISHED await video.save() + + await publishAndFederateIfNeeded(video) } async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { diff --git a/server/lib/job-queue/handlers/video-transcoding.ts b/server/lib/job-queue/handlers/video-transcoding.ts index 2aebc29f7..843a9f1b5 100644 --- a/server/lib/job-queue/handlers/video-transcoding.ts +++ b/server/lib/job-queue/handlers/video-transcoding.ts @@ -1,4 +1,5 @@ import * as Bull from 'bull' +import { publishAndFederateIfNeeded } from '@server/lib/video' import { getVideoFilePath } from '@server/lib/video-paths' import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' import { @@ -174,25 +175,3 @@ function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: numbe return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) } } - -async function publishAndFederateIfNeeded (video: MVideoUUID) { - const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { - // Maybe the video changed in database, refresh it - const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) - // Video does not exist anymore - if (!videoDatabase) return undefined - - // We transcoded the video file in another format, now we can publish it - const videoPublished = await videoDatabase.publishIfNeededAndSave(t) - - // If the video was not published, we consider it is a new one for other instances - await federateVideoIfNeeded(videoDatabase, videoPublished, t) - - return { videoDatabase, videoPublished } - }) - - if (videoPublished) { - Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) - Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) - } -} diff --git a/server/lib/video.ts b/server/lib/video.ts index 6df41e6cd..81b7c4159 100644 --- a/server/lib/video.ts +++ b/server/lib/video.ts @@ -1,9 +1,12 @@ import { Transaction } from 'sequelize/types' +import { sequelizeTypescript } from '@server/initializers/database' import { TagModel } from '@server/models/video/tag' import { VideoModel } from '@server/models/video/video' import { FilteredModelAttributes } from '@server/types' -import { MTag, MThumbnail, MVideoTag, MVideoThumbnail } from '@server/types/models' +import { MTag, MThumbnail, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models' import { ThumbnailType, VideoCreate, VideoPrivacy } from '@shared/models' +import { federateVideoIfNeeded } from './activitypub/videos' +import { Notifier } from './notifier' import { createVideoMiniatureFromExisting } from './thumbnail' function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes { @@ -78,10 +81,33 @@ async function setVideoTags (options: { } } +async function publishAndFederateIfNeeded (video: MVideoUUID) { + const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { + // Maybe the video changed in database, refresh it + const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) + // Video does not exist anymore + if (!videoDatabase) return undefined + + // We transcoded the video file in another format, now we can publish it + const videoPublished = await videoDatabase.publishIfNeededAndSave(t) + + // If the video was not published, we consider it is a new one for other instances + await federateVideoIfNeeded(videoDatabase, videoPublished, t) + + return { videoDatabase, videoPublished } + }) + + if (videoPublished) { + Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) + Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) + } +} + // --------------------------------------------------------------------------- export { buildLocalVideoFromReq, + publishAndFederateIfNeeded, buildVideoThumbnailsFromReq, setVideoTags } -- cgit v1.2.3