1
0
Fork 0

Add codec information in HLS playlist

This commit is contained in:
Chocobozzz 2019-11-26 16:25:36 +01:00
parent 08eb3dca82
commit 52201311e1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 57 additions and 10 deletions

View File

@ -262,6 +262,7 @@ export class PeertubePlayerManager {
},
html5: {
hlsjsConfig: {
capLevelToPlayerSize: true,
autoStartLoad: false,
liveSyncDurationCount: 7,
loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass()

View File

@ -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

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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`)
}
}