]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix audio transcoding with video only file
authorChocobozzz <me@florianbigard.com>
Thu, 6 Jan 2022 16:55:37 +0000 (17:55 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 6 Jan 2022 16:55:37 +0000 (17:55 +0100)
scripts/create-transcoding-job.ts
server/controllers/api/videos/transcoding.ts
server/lib/job-queue/handlers/video-transcoding.ts
server/models/video/video.ts
shared/models/server/job.model.ts

index d462fbf33471b73ec9fb32394ae73c60f28662b3..95e1e66cfdf2307da2a8ef56527b5dc484b19268 100755 (executable)
@@ -60,7 +60,11 @@ async function run () {
         type: 'new-resolution-to-hls',
         videoUUID: video.uuid,
         resolution,
+
+        // FIXME: check the file has audio and is not in portrait mode
         isPortraitMode: false,
+        hasAudio: true,
+
         copyCodecs: false,
         isNewVideo: false,
         isMaxQuality: maxResolution === resolution,
@@ -72,6 +76,10 @@ async function run () {
       dataInput.push({
         type: 'new-resolution-to-webtorrent',
         videoUUID: video.uuid,
+
+        // FIXME: check the file has audio
+        hasAudio: true,
+
         isNewVideo: false,
         resolution: parseInt(options.resolution)
       })
index dd6fbd3de51b0616584c7cf4d09d1cbed25fde8d..388689c8afa0a113696bf5d03b02f36454067c7b 100644 (file)
@@ -29,7 +29,7 @@ async function createTranscoding (req: express.Request, res: express.Response) {
 
   const body: VideoTranscodingCreate = req.body
 
-  const { resolution: maxResolution, isPortraitMode } = await video.getMaxQualityResolution()
+  const { resolution: maxResolution, isPortraitMode, audioStream } = await video.getMaxQualityFileInfo()
   const resolutions = computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ])
 
   video.state = VideoState.TO_TRANSCODE
@@ -42,6 +42,7 @@ async function createTranscoding (req: express.Request, res: express.Response) {
         videoUUID: video.uuid,
         resolution,
         isPortraitMode,
+        hasAudio: !!audioStream,
         copyCodecs: false,
         isNewVideo: false,
         autoDeleteWebTorrentIfNeeded: false,
@@ -53,6 +54,7 @@ async function createTranscoding (req: express.Request, res: express.Response) {
         videoUUID: video.uuid,
         isNewVideo: false,
         resolution: resolution,
+        hasAudio: !!audioStream,
         isPortraitMode
       })
     }
index ef3abcbcd7f5308b5473930b63e602a1a5cb3599..02902b0b8b9faabd23b706bf6319cedb47fde9f3 100644 (file)
@@ -6,11 +6,13 @@ import { moveToFailedTranscodingState, moveToNextState } from '@server/lib/video
 import { UserModel } from '@server/models/user/user'
 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
 import { MUser, MUserId, MVideo, MVideoFullLight, MVideoWithFile } from '@server/types/models'
+import { pick } from '@shared/core-utils'
 import {
   HLSTranscodingPayload,
   MergeAudioTranscodingPayload,
   NewResolutionTranscodingPayload,
   OptimizeTranscodingPayload,
+  VideoResolution,
   VideoTranscodingPayload
 } from '@shared/models'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
@@ -159,6 +161,7 @@ async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, pay
       user,
       videoFileResolution: payload.resolution,
       isPortraitMode: payload.isPortraitMode,
+      hasAudio: payload.hasAudio,
       isNewVideo: payload.isNewVideo ?? true,
       type: 'hls'
     })
@@ -174,7 +177,7 @@ async function onVideoFirstWebTorrentTranscoding (
   transcodeType: TranscodeOptionsType,
   user: MUserId
 ) {
-  const { resolution, isPortraitMode } = await videoArg.getMaxQualityResolution()
+  const { resolution, isPortraitMode, audioStream } = await videoArg.getMaxQualityFileInfo()
 
   // Maybe the video changed in database, refresh it
   const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid)
@@ -186,6 +189,7 @@ async function onVideoFirstWebTorrentTranscoding (
     ...payload,
 
     isPortraitMode,
+    hasAudio: !!audioStream,
     resolution: videoDatabase.getMaxQualityFile().resolution,
     // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues
     copyCodecs: transcodeType !== 'quick-transcode',
@@ -196,6 +200,7 @@ async function onVideoFirstWebTorrentTranscoding (
     video: videoDatabase,
     user,
     videoFileResolution: resolution,
+    hasAudio: !!audioStream,
     isPortraitMode,
     type: 'webtorrent',
     isNewVideo: payload.isNewVideo ?? true
@@ -214,7 +219,7 @@ async function onNewWebTorrentFileResolution (
   user: MUserId,
   payload: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload
 ) {
-  await createHlsJobIfEnabled(user, { ...payload, copyCodecs: true, isMaxQuality: false })
+  await createHlsJobIfEnabled(user, { hasAudio: true, copyCodecs: true, isMaxQuality: false, ...payload })
   await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
 
   await retryTransactionWrapper(moveToNextState, video, payload.isNewVideo)
@@ -225,6 +230,7 @@ async function onNewWebTorrentFileResolution (
 async function createHlsJobIfEnabled (user: MUserId, payload: {
   videoUUID: string
   resolution: number
+  hasAudio: boolean
   isPortraitMode?: boolean
   copyCodecs: boolean
   isMaxQuality: boolean
@@ -238,13 +244,9 @@ async function createHlsJobIfEnabled (user: MUserId, payload: {
 
   const hlsTranscodingPayload: HLSTranscodingPayload = {
     type: 'new-resolution-to-hls',
-    videoUUID: payload.videoUUID,
-    resolution: payload.resolution,
-    isPortraitMode: payload.isPortraitMode,
-    copyCodecs: payload.copyCodecs,
-    isMaxQuality: payload.isMaxQuality,
     autoDeleteWebTorrentIfNeeded: true,
-    isNewVideo: payload.isNewVideo
+
+    ...pick(payload, [ 'videoUUID', 'resolution', 'isPortraitMode', 'copyCodecs', 'isMaxQuality', 'isNewVideo', 'hasAudio' ])
   }
 
   await addTranscodingJob(hlsTranscodingPayload, jobOptions)
@@ -257,16 +259,19 @@ async function createLowerResolutionsJobs (options: {
   user: MUserId
   videoFileResolution: number
   isPortraitMode: boolean
+  hasAudio: boolean
   isNewVideo: boolean
   type: 'hls' | 'webtorrent'
 }) {
-  const { video, user, videoFileResolution, isPortraitMode, isNewVideo, type } = options
+  const { video, user, videoFileResolution, isPortraitMode, isNewVideo, hasAudio, type } = options
 
   // Create transcoding jobs if there are enabled resolutions
   const resolutionsEnabled = computeLowerResolutionsToTranscode(videoFileResolution, 'vod')
   const resolutionCreated: string[] = []
 
   for (const resolution of resolutionsEnabled) {
+    if (resolution === VideoResolution.H_NOVIDEO && hasAudio === false) continue
+
     let dataInput: VideoTranscodingPayload
 
     if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') {
@@ -276,6 +281,7 @@ async function createLowerResolutionsJobs (options: {
         videoUUID: video.uuid,
         resolution,
         isPortraitMode,
+        hasAudio,
         isNewVideo
       }
 
@@ -288,6 +294,7 @@ async function createLowerResolutionsJobs (options: {
         videoUUID: video.uuid,
         resolution,
         isPortraitMode,
+        hasAudio,
         copyCodecs: false,
         isMaxQuality: false,
         autoDeleteWebTorrentIfNeeded: true,
index e5077487a9c31710e382c20605f1867af0191ff3..12b9375743702ce9c4778584ad7d66e0dbbcd957 100644 (file)
@@ -33,7 +33,7 @@ import { VideoPathManager } from '@server/lib/video-path-manager'
 import { getServerActor } from '@server/models/application/application'
 import { ModelCache } from '@server/models/model-cache'
 import { buildVideoEmbedPath, buildVideoWatchPath, pick } from '@shared/core-utils'
-import { uuidToShort } from '@shared/extra-utils'
+import { ffprobePromise, getAudioStream, uuidToShort } from '@shared/extra-utils'
 import {
   ResultList,
   ThumbnailType,
@@ -1678,12 +1678,20 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
     return peertubeTruncate(this.description, { length: maxLength })
   }
 
-  getMaxQualityResolution () {
+  getMaxQualityFileInfo () {
     const file = this.getMaxQualityFile()
     const videoOrPlaylist = file.getVideoOrStreamingPlaylist()
 
-    return VideoPathManager.Instance.makeAvailableVideoFile(file.withVideoOrPlaylist(videoOrPlaylist), originalFilePath => {
-      return getVideoFileResolution(originalFilePath)
+    return VideoPathManager.Instance.makeAvailableVideoFile(file.withVideoOrPlaylist(videoOrPlaylist), async originalFilePath => {
+      const probe = await ffprobePromise(originalFilePath)
+
+      const { audioStream } = await getAudioStream(originalFilePath, probe)
+
+      return {
+        audioStream,
+
+        ...await getVideoFileResolution(originalFilePath, probe)
+      }
     })
   }
 
index ecc960da55eb27cf7e6d8d6e607b5f6ca175ab62..8a69d11fa86148786872f7c83199a56e6ffe8536 100644 (file)
@@ -103,18 +103,23 @@ interface BaseTranscodingPayload {
 
 export interface HLSTranscodingPayload extends BaseTranscodingPayload {
   type: 'new-resolution-to-hls'
-  isPortraitMode?: boolean
   resolution: VideoResolution
   copyCodecs: boolean
 
+  hasAudio: boolean
+  isPortraitMode?: boolean
+
   autoDeleteWebTorrentIfNeeded: boolean
   isMaxQuality: boolean
 }
 
 export interface NewResolutionTranscodingPayload extends BaseTranscodingPayload {
   type: 'new-resolution-to-webtorrent'
-  isPortraitMode?: boolean
   resolution: VideoResolution
+
+  hasAudio: boolean
+
+  isPortraitMode?: boolean
 }
 
 export interface MergeAudioTranscodingPayload extends BaseTranscodingPayload {