From eba06469b70ffd722532881e40574d2a38a2a19b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 18 Oct 2019 11:44:01 +0200 Subject: Fix audio merging when specifying a preview --- server/lib/video-transcoding.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'server/lib/video-transcoding.ts') diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index a204c0c63..612d388ee 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts @@ -1,7 +1,14 @@ import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' -import { join } from 'path' -import { canDoQuickTranscode, getVideoFileFPS, transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils' -import { ensureDir, move, remove, stat } from 'fs-extra' +import { basename, join } from 'path' +import { + canDoQuickTranscode, + getDurationFromVideoFile, + getVideoFileFPS, + transcode, + TranscodeOptions, + TranscodeOptionsType +} from '../helpers/ffmpeg-utils' +import { copyFile, ensureDir, move, remove, stat } from 'fs-extra' import { logger } from '../helpers/logger' import { VideoResolution } from '../../shared/models/videos' import { VideoFileModel } from '../models/video/video-file' @@ -97,22 +104,37 @@ async function mergeAudioVideofile (video: MVideoWithFileThumbnail, resolution: const audioInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile())) const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) + // If the user updates the video preview during transcoding + const previewPath = video.getPreview().getPath() + const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath)) + await copyFile(previewPath, tmpPreviewPath) + const transcodeOptions = { type: 'merge-audio' as 'merge-audio', - inputPath: video.getPreview().getPath(), + inputPath: tmpPreviewPath, outputPath: videoTranscodedPath, audioPath: audioInputPath, resolution } - await transcode(transcodeOptions) + try { + await transcode(transcodeOptions) - await remove(audioInputPath) + await remove(audioInputPath) + await remove(tmpPreviewPath) + } catch (err) { + await remove(tmpPreviewPath) + throw err + } // Important to do this before getVideoFilename() to take in account the new file extension inputVideoFile.extname = newExtname const videoOutputPath = video.getVideoFilePath(inputVideoFile) + // ffmpeg generated a new video file, so update the video duration + // See https://trac.ffmpeg.org/ticket/5456 + video.duration = await getDurationFromVideoFile(videoTranscodedPath) + await video.save() return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) } -- cgit v1.2.3