X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fvideo-transcoding.ts;h=612d388ee7ba01acc5c5c978b937b80a15c8ff5e;hb=a0e6d267598839c8a5508a65876ce0e07d1b3d74;hp=a204c0c634d473aa8fdb901eb4811bffa31fe21e;hpb=282e61e6c11f79e919c543871783fe1a00298d18;p=github%2FChocobozzz%2FPeerTube.git 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) }