Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Add codec information in HLS playlist

Chocobozzz52201311e197
diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts
index 4564b6c3ec..bda718cffd 100644
--- a/client/src/assets/player/peertube-player-manager.ts
+++ b/client/src/assets/player/peertube-player-manager.ts
@@ -262,6 +262,7 @@ export class PeertubePlayerManager {
},
html5: {
hlsjsConfig: {
+ capLevelToPlayerSize: true,
autoStartLoad: false,
liveSyncDurationCount: 7,
loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass()
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts
index ff80991b2c..1eea05d1ed 100644
--- a/server/helpers/ffmpeg-utils.ts
+++ b/server/helpers/ffmpeg-utils.ts
@@ -32,7 +32,7 @@ function computeResolutionsToTranscode (videoFileHeight: number) {
return resolutionsEnabled
}
-async function getVideoFileSize (path: string) {
+async function getVideoStreamSize (path: string) {
const videoStream = await getVideoStreamFromFile(path)
return videoStream === null
@@ -40,8 +40,45 @@ async function getVideoFileSize (path: string) {
: { width: videoStream.width, height: videoStream.height }
}
+async function getVideoStreamCodec (path: string) {
+ const videoStream = await getVideoStreamFromFile(path)
+
+ if (!videoStream) return ''
+
+ const videoCodec = videoStream.codec_tag_string
+
+ const baseProfileMatrix = {
+ 'High': '6400',
+ 'Main': '4D40',
+ 'Baseline': '42E0'
+ }
+
+ let baseProfile = baseProfileMatrix[videoStream.profile]
+ if (!baseProfile) {
+ logger.warn('Cannot get video profile codec of %s.', path, { videoStream })
+ baseProfile = baseProfileMatrix['High'] // Fallback
+ }
+
+ const level = videoStream.level.toString(16)
+
+ return `${videoCodec}.${baseProfile}${level}`
+}
+
+async function getAudioStreamCodec (path: string) {
+ const { audioStream } = await audio.get(path)
+
+ if (!audioStream) return ''
+
+ const audioCodec = audioStream.codec_name
+ if (audioCodec.codec_name === 'aac') return 'mp4a.40.2'
+
+ logger.warn('Cannot get audio codec of %s.', path, { audioStream })
+
+ return 'mp4a.40.2' // Fallback
+}
+
async function getVideoFileResolution (path: string) {
- const size = await getVideoFileSize(path)
+ const size = await getVideoStreamSize(path)
return {
videoFileResolution: Math.min(size.height, size.width),
@@ -229,7 +266,9 @@ async function canDoQuickTranscode (path: string): Promise<boolean> {
// ---------------------------------------------------------------------------
export {
- getVideoFileSize,
+ getVideoStreamCodec,
+ getAudioStreamCodec,
+ getVideoStreamSize,
getVideoFileResolution,
getDurationFromVideoFile,
generateImageFromVideoFile,
@@ -448,8 +487,8 @@ async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolut
let localCommand = command
.format('mp4')
.videoCodec('libx264')
- .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution
- .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it
+ .outputOption('-level 3.1') // 3.1 is the minimal resource allocation for our highest supported resolution
+ .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
.outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
.outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
.outputOption('-map_metadata -1') // strip all metadata
diff --git a/server/lib/hls.ts b/server/lib/hls.ts
index 943721dd77..c94b599df4 100644
--- a/server/lib/hls.ts
+++ b/server/lib/hls.ts
@@ -1,7 +1,7 @@
import { basename, dirname, join } from 'path'
import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION } from '../initializers/constants'
import { close, ensureDir, move, open, outputJSON, pathExists, read, readFile, remove, writeFile } from 'fs-extra'
-import { getVideoFileSize } from '../helpers/ffmpeg-utils'
+import { getVideoStreamSize, getAudioStreamCodec, getVideoStreamCodec } from '../helpers/ffmpeg-utils'
import { sha256 } from '../helpers/core-utils'
import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
import { logger } from '../helpers/logger'
@@ -42,7 +42,7 @@ async function updateMasterHLSPlaylist (video: MVideoWithFile) {
const videoFilePath = getVideoFilePath(streamingPlaylist, file)
- const size = await getVideoFileSize(videoFilePath)
+ const size = await getVideoStreamSize(videoFilePath)
const bandwidth = 'BANDWIDTH=' + video.getBandwidthBits(file)
const resolution = `RESOLUTION=${size.width}x${size.height}`
@@ -50,6 +50,10 @@ async function updateMasterHLSPlaylist (video: MVideoWithFile) {
let line = `#EXT-X-STREAM-INF:${bandwidth},${resolution}`
if (file.fps) line += ',FRAME-RATE=' + file.fps
+ const audioCodec = await getAudioStreamCodec(filePlaylistPath)
+ const videoCodec = await getVideoStreamCodec(filePlaylistPath)
+ line += `,CODECS="${videoCodec},${audioCodec}"`
+
masterPlaylists.push(line)
masterPlaylists.push(VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
}
diff --git a/server/tests/api/videos/audio-only.ts b/server/tests/api/videos/audio-only.ts
index f5b6a26e58..f12d730ccf 100644
--- a/server/tests/api/videos/audio-only.ts
+++ b/server/tests/api/videos/audio-only.ts
@@ -22,7 +22,7 @@ import { VideoDetails } from '../../../../shared/models/videos'
import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
import { join } from 'path'
import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
-import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, audio, getVideoFileSize } from '@server/helpers/ffmpeg-utils'
+import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, audio, getVideoStreamSize } from '@server/helpers/ffmpeg-utils'
const expect = chai.expect
@@ -96,7 +96,7 @@ describe('Test audio only video transcoding', function () {
expect(audioStream[ 'codec_name' ]).to.be.equal('aac')
expect(audioStream[ 'bit_rate' ]).to.be.at.most(384 * 8000)
- const size = await getVideoFileSize(path)
+ const size = await getVideoStreamSize(path)
expect(size.height).to.equal(0)
expect(size.width).to.equal(0)
}
diff --git a/server/tests/api/videos/video-hls.ts b/server/tests/api/videos/video-hls.ts
index 2892091770..bde3b56569 100644
--- a/server/tests/api/videos/video-hls.ts
+++ b/server/tests/api/videos/video-hls.ts
@@ -66,7 +66,10 @@ async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOn
const masterPlaylist = res.text
for (const resolution of resolutions) {
- expect(masterPlaylist).to.match(new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+'))
+ const reg = new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+,CODECS="avc1.64001f,mp4a.40.2"')
+
+ expect(masterPlaylist).to.match(reg)
+ expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
}
}