import { ensureDir, stat } from 'fs-extra'
import { basename } from 'path'
import { isTestInstance } from '@server/helpers/core-utils'
-import {
- computeResolutionsToTranscode,
- getVideoFileFPS,
- getVideoFileResolution,
- runLiveMuxing,
- runLiveTranscoding
-} from '@server/helpers/ffmpeg-utils'
+import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils'
+import { computeResolutionsToTranscode, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
import { logger } from '@server/helpers/logger'
import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config'
import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME, WEBSERVER } from '@server/initializers/constants'
import { PeerTubeSocket } from './peertube-socket'
import { isAbleToUploadVideo } from './user'
import { getHLSDirectory } from './video-paths'
+import { availableEncoders } from './video-transcoding-profiles'
import memoizee = require('memoizee')
const NodeRtmpServer = require('node-media-server/node_rtmp_server')
const deleteSegments = videoLive.saveReplay === false
const ffmpegExec = CONFIG.LIVE.TRANSCODING.ENABLED
- ? runLiveTranscoding(rtmpUrl, outPath, allResolutions, fps, deleteSegments)
- : runLiveMuxing(rtmpUrl, outPath, deleteSegments)
+ ? await getLiveTranscodingCommand({
+ rtmpUrl,
+ outPath,
+ resolutions: allResolutions,
+ fps,
+ deleteSegments,
+ availableEncoders,
+ profile: 'default'
+ })
+ : getLiveMuxingCommand(rtmpUrl, outPath, deleteSegments)
logger.info('Running live muxing/transcoding for %s.', videoUUID)
this.transSessions.set(sessionId, ffmpegExec)
const segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
const playlistIdMatcher = /^([\d+])-/
- const addHandler = segmentPath => {
- const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
- const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || []
-
+ const processHashSegments = (segmentsToProcess: string[]) => {
// Add sha hash of previous segments, because ffmpeg should have finished generating them
for (const previousSegment of segmentsToProcess) {
this.addSegmentSha(videoUUID, previousSegment)
.catch(err => logger.error('Cannot add sha segment of video %s -> %s.', videoUUID, previousSegment, { err }))
}
+ }
+
+ const addHandler = segmentPath => {
+ logger.debug('Live add handler of %s.', segmentPath)
+
+ const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
+
+ const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || []
+ processHashSegments(segmentsToProcess)
segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
this.transSessions.delete(sessionId)
this.watchersPerVideo.delete(videoLive.videoId)
- Promise.all([ tsWatcher.close(), masterWatcher.close() ])
- .catch(err => logger.error('Cannot close watchers of %s.', outPath, { err }))
-
- this.onEndTransmuxing(videoLive.Video.id)
- .catch(err => logger.error('Error in closed transmuxing.', { err }))
+ setTimeout(() => {
+ // Wait latest segments generation, and close watchers
+
+ Promise.all([ tsWatcher.close(), masterWatcher.close() ])
+ .then(() => {
+ // Process remaining segments hash
+ for (const key of Object.keys(segmentsToProcessPerPlaylist)) {
+ processHashSegments(segmentsToProcessPerPlaylist[key])
+ }
+ })
+ .catch(err => logger.error('Cannot close watchers of %s or process remaining hash segments.', outPath, { err }))
+
+ this.onEndTransmuxing(videoLive.Video.id)
+ .catch(err => logger.error('Error in closed transmuxing.', { err }))
+ }, 1000)
}
ffmpegExec.on('error', (err, stdout, stderr) => {
})
ffmpegExec.on('end', () => onFFmpegEnded())
+
+ ffmpegExec.run()
}
private async onEndTransmuxing (videoId: number, cleanupNow = false) {