X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fffmpeg-utils.ts;h=b6b64de3f604ba8240499cd650068325ee12f213;hb=7ccddd7b5250bd25a917a6e77e58b87b9484a2a4;hp=5ad8ed48e67e501942870f2770076371acac1fc3;hpb=092092969633bbcf6d4891a083ea497a7d5c3154;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 5ad8ed48e..b6b64de3f 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts @@ -5,7 +5,7 @@ import { CONFIG, FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/cons import { processImage } from './image-utils' import { logger } from './logger' import { checkFFmpegEncoders } from '../initializers/checker-before-init' -import { remove } from 'fs-extra' +import { remove, readFile, writeFile } from 'fs-extra' function computeResolutionsToTranscode (videoFileHeight: number) { const resolutionsEnabled: number[] = [] @@ -122,7 +122,9 @@ type TranscodeOptions = { resolution: VideoResolution isPortraitMode?: boolean - generateHlsPlaylist?: boolean + hlsPlaylist?: { + videoFilename: string + } } function transcode (options: TranscodeOptions) { @@ -161,14 +163,16 @@ function transcode (options: TranscodeOptions) { command = command.withFPS(fps) } - if (options.generateHlsPlaylist) { - const segmentFilename = `${dirname(options.outputPath)}/${options.resolution}_%03d.ts` + if (options.hlsPlaylist) { + const videoPath = getHLSVideoPath(options) command = command.outputOption('-hls_time 4') .outputOption('-hls_list_size 0') .outputOption('-hls_playlist_type vod') - .outputOption('-hls_segment_filename ' + segmentFilename) + .outputOption('-hls_segment_filename ' + videoPath) + .outputOption('-hls_segment_type fmp4') .outputOption('-f hls') + .outputOption('-hls_flags single_file') } command @@ -176,7 +180,11 @@ function transcode (options: TranscodeOptions) { logger.error('Error in transcoding job.', { stdout, stderr }) return rej(err) }) - .on('end', res) + .on('end', () => { + return onTranscodingSuccess(options) + .then(() => res()) + .catch(err => rej(err)) + }) .run() } catch (err) { return rej(err) @@ -200,6 +208,25 @@ export { // --------------------------------------------------------------------------- +function getHLSVideoPath (options: TranscodeOptions) { + return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` +} + +async function onTranscodingSuccess (options: TranscodeOptions) { + if (!options.hlsPlaylist) return + + // Fix wrong mapping with some ffmpeg versions + const fileContent = await readFile(options.outputPath) + + const videoFileName = options.hlsPlaylist.videoFilename + const videoFilePath = getHLSVideoPath(options) + + const newContent = fileContent.toString() + .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`) + + await writeFile(options.outputPath, newContent) +} + function getVideoFileStream (path: string) { return new Promise((res, rej) => { ffmpeg.ffprobe(path, (err, metadata) => {