import { OVar } from "../jshelper/mod.ts"; import { EncodingProfile, JhlsMetadata } from "./jhls.d.ts"; export interface EncodingProfileExt extends EncodingProfile { id: number, order: number } export class ProfileSelector { profiles_video: EncodingProfileExt[] = [] profiles_audio: EncodingProfileExt[] = [] profiles_subtitles: EncodingProfileExt[] = [] remux_bandwidth = new Map() constructor(private bandwidth: OVar, private metadata: JhlsMetadata) { for (let id = 0; id < metadata.extra_profiles.length; id++) { const p = metadata.extra_profiles[id]; if (p.audio) this.profiles_audio.push({ id, order: 0, ...p }) if (p.video) this.profiles_video.push({ id, order: 0, ...p }) if (p.subtitles) this.profiles_subtitles.push({ id, order: 0, ...p }) } this.profiles_audio.sort((a, b) => profile_bw(b) - profile_bw(a)) this.profiles_video.sort((a, b) => profile_bw(b) - profile_bw(a)) this.profiles_subtitles.sort((a, b) => profile_bw(b) - profile_bw(a)) for (let i = 0; i < this.profiles_audio.length; i++) this.profiles_audio[i].order = i for (let i = 0; i < this.profiles_video.length; i++) this.profiles_video[i].order = i for (let i = 0; i < this.profiles_subtitles.length; i++) this.profiles_subtitles[i].order = i } profile_list_for_track(track: number): EncodingProfileExt[] { const i = this.metadata.tracks[track].info.kind if (i.audio) return this.profiles_audio if (i.video) return this.profiles_video if (i.subtitles) return this.profiles_subtitles return [] } select_optimal_profile(track: number, profile: OVar) { if (this.bandwidth.value < 5000 * 1000) { profile.value = this.profile_list_for_track(track)[0] } else { profile.value = undefined } } } function profile_bw(p: EncodingProfile): number { if (p.audio) return p.audio.bitrate / 8 if (p.video) return p.video.bitrate / 8 if (p.subtitles) return 100 return 0 }