Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
6.0 kB · 257 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258package renditions
import ( "fmt" "math" "strings"
"stream.place/streamplace/pkg/placestream")
type FPS struct { Passthrough bool Num uint Den uint}
type Rendition struct { Width int64 Height int64 Bitrate int Framerate FPS Profile string Name string Parent *Rendition AudioOnly bool}
var AudioRendition = Rendition{ Name: "audio", Bitrate: 128_000, AudioOnly: true,}
type JSONProfile struct { Name string `json:"name,omitempty"` Width int `json:"width,omitempty"` Height int `json:"height,omitempty"` Bitrate int `json:"bitrate,omitempty"` FPS uint `json:"fps,omitempty"` FPSDen uint `json:"fpsDen,omitempty"` Profile string `json:"profile,omitempty"` GOP string `json:"gop,omitempty"` Encoder string `json:"encoder,omitempty"` Quality uint `json:"quality,omitempty"`}
// ToLivepeerProfile is the rendition as the gateway's transcode// configuration wants it. Both dimensions are sent: GenerateRenditions has// already fitted them to the source's aspect, and a gateway handed one// dimension takes it as the width (a "720p" asked for by height alone came// back 720 pixels wide). Dimensions are rounded to even for the encoder.func (r Rendition) ToLivepeerProfile() JSONProfile { return JSONProfile{ Name: r.Name, Width: even(r.Width), Height: even(r.Height), Bitrate: r.Bitrate, FPS: r.Framerate.Num, FPSDen: r.Framerate.Den, Profile: r.Profile, }}
func even(n int64) int { if n%2 != 0 { n++ } return int(n)}
type Renditions []Rendition
func (rs Renditions) ToLivepeerProfiles() []JSONProfile { profiles := make([]JSONProfile, len(rs)) for i, r := range rs { profiles[i] = r.ToLivepeerProfile() } return profiles}
var DesiredRenditions = []Rendition{ { Name: "1080p", Width: 1920, Height: 1080, Bitrate: 6_000_000, Framerate: FPS{ Num: 60, Den: 1, }, Profile: "h264constrainedhigh", }, { Name: "720p", Width: 1280, Height: 720, Bitrate: 3_000_000, Framerate: FPS{ Num: 60, Den: 1, }, Profile: "h264constrainedhigh", }, { Name: "360p", Width: 640, Height: 360, Bitrate: 1_000_000, Framerate: FPS{ Num: 30, Den: 1, }, Profile: "h264constrainedhigh", }, { Name: "240p", Width: 426, Height: 240, Bitrate: 500_000, Framerate: FPS{ Num: 30, Den: 1, }, Profile: "h264constrainedhigh", }, { Name: "160p", Width: 284, Height: 160, Bitrate: 250_000, Framerate: FPS{ Num: 30, Den: 1, }, Profile: "h264baseline", },}
// Ladder resolves a comma-separated selection of rendition names ("240p,160p")// from DesiredRenditions, in ladder order; empty means the whole ladder. An// unknown name is an error.func Ladder(spec string) ([]Rendition, error) { spec = strings.TrimSpace(spec) if spec == "" { return DesiredRenditions, nil } want := map[string]bool{} for _, n := range strings.Split(spec, ",") { n = strings.TrimSpace(n) if n == "" { continue } known := false for _, r := range DesiredRenditions { if r.Name == n { known = true } } if !known { names := make([]string, 0, len(DesiredRenditions)) for _, r := range DesiredRenditions { names = append(names, r.Name) } return nil, fmt.Errorf("unknown rendition %q (known: %s)", n, strings.Join(names, ", ")) } want[n] = true } out := make([]Rendition, 0, len(want)) for _, r := range DesiredRenditions { if want[r.Name] { out = append(out, r) } } return out, nil}
// GenerateRenditions generates renditions for a given spseg from the whole// ladder.func GenerateRenditions(spseg *placestream.Segment) (Renditions, error) { return GenerateRenditionsFrom(spseg, DesiredRenditions)}
// GenerateRenditionsFrom generates the renditions of ladder (see Ladder)// that fit under spseg's video.func GenerateRenditionsFrom(spseg *placestream.Segment, ladder []Rendition) (Renditions, error) { if len(spseg.Video) == 0 { return nil, fmt.Errorf("no video stream found") } vid := spseg.Video[0] rs := []Rendition{} for _, r := range ladder { vidWidth := int64(vid.Width) vidHeight := int64(vid.Height) vertical := vid.Height > vid.Width // do all the math as if it's horizontal then flip at the end if vertical { vidWidth, vidHeight = vidHeight, vidWidth } if vidWidth <= r.Width && vidHeight <= r.Height { continue } rAspectRatio := float64(r.Width) / float64(r.Height) vidAspectRatio := float64(vidWidth) / float64(vidHeight) if vidAspectRatio > rAspectRatio { // vid is wider than r // scale down to r.Width scale := float64(r.Width) / float64(vidWidth) vidWidth = r.Width vidHeight = int64(math.Round(float64(vidHeight) * scale)) } else { // vid is taller than r // scale down to r.Height scale := float64(r.Height) / float64(vidHeight) vidHeight = r.Height vidWidth = int64(math.Round(float64(vidWidth) * scale)) } outR := Rendition{ Name: r.Name, Parent: &r, Profile: r.Profile, } if vertical { outR.Width = vidHeight outR.Height = vidWidth } else { outR.Width = vidWidth outR.Height = vidHeight }
// if vertical { // ratio := float64(r.Height) / float64(vid.Height) // outR.Height = int64(float64(vid.Width) * (16.0 / 9.0) * ratio) // outR.Width = r.Height // } else { // ratio := float64(r.Width) / float64(vid.Width) // outR.Width = r.Width // outR.Height = int64(float64(vid.Width) * (9.0 / 16.0) * ratio) // } if vid.Framerate.Den > 0 { vidFPS := float64(vid.Framerate.Num) / float64(vid.Framerate.Den) rFPS := float64(r.Framerate.Num) / float64(r.Framerate.Den) delta := rFPS / vidFPS
if rFPS < vidFPS { if delta < 0.75 { outR.Framerate.Num = uint(vid.Framerate.Num) outR.Framerate.Den = uint(vid.Framerate.Den * 2) } } }
outR.Bitrate = r.Bitrate outR.Profile = r.Profile rs = append(rs, outR) } return rs, nil}