- return video
-}
-
-// ---------------------------------------------------------------------------
-// Available encoders profiles
-// ---------------------------------------------------------------------------
-
-const defaultX264OptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => {
- if (!fps) return { outputOptions: [] }
-
- let targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
-
- // Don't transcode to an higher bitrate than the original file
- const fileBitrate = await getVideoFileBitrate(input)
- targetBitrate = Math.min(targetBitrate, fileBitrate)
-
- return {
- outputOptions: [
- // Constrained Encoding (VBV)
- // https://slhck.info/video/2017/03/01/rate-control.html
- // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
- `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}`
- ]
- }
-}
-
-const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input }) => {
- const parsedAudio = await getAudioStream(input)
-
- // we try to reduce the ceiling bitrate by making rough matches of bitrates
- // of course this is far from perfect, but it might save some space in the end
-
- const audioCodecName = parsedAudio.audioStream['codec_name']
-
- const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
-
- if (bitrate !== undefined && bitrate !== -1) {
- return { outputOptions: [ '-b:a', bitrate + 'k' ] }
- }
-
- return { outputOptions: [] }
-}
-
-const defaultLibFDKAACOptionsBuilder: EncoderOptionsBuilder = () => {
- return { outputOptions: [ '-aq', '5' ] }
-}
-
-const availableEncoders: AvailableEncoders = {
- vod: {
- libx264: {
- default: defaultX264OptionsBuilder
- },
- aac: {
- default: defaultAACOptionsBuilder
- },
- libfdkAAC: {
- default: defaultLibFDKAACOptionsBuilder
- }
- },
- live: {
- libx264: {
- default: defaultX264OptionsBuilder
- },
- aac: {
- default: defaultAACOptionsBuilder
- },
- libfdkAAC: {
- default: defaultLibFDKAACOptionsBuilder
- }
- }
-}
-
-// ---------------------------------------------------------------------------
-
-export {
- generateHlsPlaylist,
- optimizeOriginalVideofile,
- transcodeNewResolution,
- mergeAudioVideofile
-}
-
-// ---------------------------------------------------------------------------
-
-async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
- const stats = await stat(transcodingPath)
- const fps = await getVideoFileFPS(transcodingPath)
- const metadata = await getMetadataFromFile(transcodingPath)
-
- await move(transcodingPath, outputPath, { overwrite: true })
-
- videoFile.size = stats.size
- videoFile.fps = fps
- videoFile.metadata = metadata
-
- await createTorrentAndSetInfoHash(video, videoFile)
-
- await VideoFileModel.customUpsert(videoFile, 'video', undefined)
- video.VideoFiles = await video.$get('VideoFiles')
-
- return video