Add codec information in HLS playlist
authorChocobozzz <me@florianbigard.com>
Tue, 26 Nov 2019 15:25:36 +0000 (16:25 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 26 Nov 2019 15:25:36 +0000 (16:25 +0100)
client/src/assets/player/peertube-player-manager.ts
server/helpers/ffmpeg-utils.ts
server/lib/hls.ts
server/tests/api/videos/audio-only.ts
server/tests/api/videos/video-hls.ts

index 4564b6c3ec2f8c3529ac92b832b6106aa9980100..bda718cffd25ae6d667fc88bf48e444899b6fc9d 100644 (file)
@@ -262,6 +262,7 @@ export class PeertubePlayerManager {
         },
         html5: {
           hlsjsConfig: {
+            capLevelToPlayerSize: true,
             autoStartLoad: false,
             liveSyncDurationCount: 7,
             loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass()
index ff80991b2ccd3260fe1dcf37d9bec9d5f067b003..1eea05d1edaaaeeaecc8cce6b8dfaf052c38bd20 100644 (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
index 943721dd779c6f485c71a14dac3a31e6d3198b5f..c94b599df480ce3b282846e8e227ffc290aefe41 100644 (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))
   }
index f5b6a26e58975b40fe1383a161ed197fb5ba831e..f12d730ccf9fd53f8775c349a26d5f0bcee032e1 100644 (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)
     }
index 289209177097c5dd9aec00e96e6d54d5a3af7485..bde3b56569ce813a4172f8e4e8d366b2faa9ea21 100644 (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`)
       }
     }