Skip to content

Commit

Permalink
Add playlistUrl metadata in json to hls files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 13, 2025
1 parent 7895697 commit eaf6fcc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export interface VideoStreamingPlaylist {
baseUrl: string
}[]

files: VideoFile[]
files: (VideoFile & { playlistUrl: string })[]
}
2 changes: 2 additions & 0 deletions packages/tests/src/shared/streaming-playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ export async function completeCheckHlsPlaylist (options: {
expect(file.magnetUri).to.have.lengthOf.above(2)
await checkWebTorrentWorks(file.magnetUri)

expect(file.playlistUrl).to.equal(file.fileUrl.replace(/-fragmented.mp4$/, '.m3u8'))

{
const nameReg = `${uuidRegex}-${file.resolution.id}`

Expand Down
2 changes: 2 additions & 0 deletions packages/tests/src/shared/videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export async function completeWebVideoFilesCheck (options: {
expect(file.id).to.exist
expect(file.magnetUri).to.have.lengthOf.above(2)

expect((file as any).playlistUrl).to.not.exist

if (server.internalServerNumber === originServer.internalServerNumber) {
if (objectStorageBaseUrl) {
expect(file.storage).to.equal(FileStorage.OBJECT_STORAGE)
Expand Down
25 changes: 22 additions & 3 deletions server/core/models/video/formatter/video-api-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { uuidToShort } from '@peertube/peertube-node-utils'
import { generateMagnetUri } from '@server/helpers/webtorrent.js'
import { tracer } from '@server/lib/opentelemetry/tracing.js'
import { getHlsResolutionPlaylistFilename } from '@server/lib/paths.js'
import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls.js'
import { VideoViewsManager } from '@server/lib/views/video-views-manager.js'
import { isArray } from '../../../helpers/custom-validators/misc.js'
Expand Down Expand Up @@ -202,18 +203,30 @@ export function streamingPlaylistsModelToFormattedJSON (
? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
: [],

files: videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
files: videoFilesModelToFormattedJSON(video, playlist.VideoFiles, { includePlaylistUrl: true })
}))
}

// ---------------------------------------------------------------------------

export function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
videoFiles: MVideoFile[],
options?: {
includePlaylistUrl?: true
includeMagnet?: boolean
}
): (VideoFile & { playlistUrl: string })[]

export function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
videoFiles: MVideoFile[],
options: {
includePlaylistUrl?: boolean // default false
includeMagnet?: boolean // default true
} = {}
): VideoFile[] {
const { includeMagnet = true } = options
const { includePlaylistUrl = false, includeMagnet = true } = options

if (isArray(videoFiles) === false) return []

Expand All @@ -225,6 +238,8 @@ export function videoFilesModelToFormattedJSON (
.filter(f => !f.isLive())
.sort(sortByResolutionDesc)
.map(videoFile => {
const fileUrl = videoFile.getFileUrl(video)

return {
id: videoFile.id,

Expand All @@ -251,14 +266,18 @@ export function videoFilesModelToFormattedJSON (
torrentUrl: videoFile.getTorrentUrl(),
torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),

fileUrl: videoFile.getFileUrl(video),
fileUrl,
fileDownloadUrl: videoFile.getFileDownloadUrl(video),

metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile),

hasAudio: videoFile.hasAudio(),
hasVideo: videoFile.hasVideo(),

playlistUrl: includePlaylistUrl === true
? getHlsResolutionPlaylistFilename(fileUrl)
: undefined,

storage: video.remote
? null
: videoFile.storage
Expand Down
4 changes: 4 additions & 0 deletions support/doc/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8214,6 +8214,10 @@ components:
type: string
description: Direct URL of the video
format: url
playlistUrl:
type: string
description: Playlist URL of the file if it is owned by a playlist
format: url
fileDownloadUrl:
type: string
description: URL endpoint that transfers the video file as an attachment (so that the browser opens a download dialog)
Expand Down

0 comments on commit eaf6fcc

Please sign in to comment.