feature (dynamic): make configuration dynamic

This commit is contained in:
MickaelK
2024-03-12 23:52:16 +11:00
parent 845c4584d3
commit 9e142d5de5
31 changed files with 631 additions and 542 deletions

View File

@ -2,8 +2,10 @@ package plg_video_transcoder
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"math"
@ -20,11 +22,17 @@ import (
)
const (
HLS_SEGMENT_LENGTH = 30
HLS_SEGMENT_LENGTH = 5
FPS = 30
CLEAR_CACHE_AFTER = 12
VideoCachePath = "data/cache/video/"
)
var (
plugin_enable func() bool
blacklist_format func() string
)
func init() {
ffmpegIsInstalled := false
ffprobeIsInstalled := false
@ -34,7 +42,7 @@ func init() {
if _, err := exec.LookPath("ffprobe"); err == nil {
ffprobeIsInstalled = true
}
plugin_enable := func() bool {
plugin_enable = func() bool {
return Config.Get("features.video.enable_transcoder").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
@ -50,8 +58,7 @@ func init() {
return f
}).Bool()
}
blacklist_format := func() string {
blacklist_format = func() string {
return Config.Get("features.video.blacklist_format").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
@ -67,50 +74,52 @@ func init() {
return f
}).String()
}
blacklist_format()
if plugin_enable() == false {
return
} else if ffmpegIsInstalled == false {
Log.Warning("[plugin video transcoder] ffmpeg needs to be installed")
return
} else if ffprobeIsInstalled == false {
Log.Warning("[plugin video transcoder] ffprobe needs to be installed")
return
}
Hooks.Register.Onload(func() {
blacklist_format()
if plugin_enable() == false {
return
} else if ffmpegIsInstalled == false {
Log.Warning("[plugin video transcoder] ffmpeg needs to be installed")
return
} else if ffprobeIsInstalled == false {
Log.Warning("[plugin video transcoder] ffprobe needs to be installed")
return
}
cachePath := GetAbsolutePath(VideoCachePath)
os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm)
cachePath := GetAbsolutePath(VideoCachePath)
os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm)
Hooks.Register.ProcessFileContentBeforeSend(hls_playlist)
Hooks.Register.HttpEndpoint(func(r *mux.Router, app *App) error {
r.PathPrefix("/hls/hls_{segment}.ts").Handler(NewMiddlewareChain(
hls_transcode,
[]Middleware{SecureHeaders},
*app,
)).Methods("GET")
return nil
})
Hooks.Register.HttpEndpoint(func(r *mux.Router, app *App) error {
r.HandleFunc(OverrideVideoSourceMapper, func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", GetMimeType(req.URL.String()))
res.Write([]byte(`window.overrides["video-map-sources"] = function(sources){`))
res.Write([]byte(` return sources.map(function(source){`))
blacklists := strings.Split(blacklist_format(), ",")
for i := 0; i < len(blacklists); i++ {
blacklists[i] = strings.TrimSpace(blacklists[i])
res.Write([]byte(fmt.Sprintf(`if(source.type == "%s"){ return source; } `, GetMimeType("."+blacklists[i]))))
}
res.Write([]byte(` source.src = source.src + "&transcode=hls";`))
res.Write([]byte(` source.type = "application/x-mpegURL";`))
res.Write([]byte(` return source;`))
res.Write([]byte(` })`))
res.Write([]byte(`}`))
Hooks.Register.ProcessFileContentBeforeSend(hls_playlist)
Hooks.Register.HttpEndpoint(func(r *mux.Router, app *App) error {
r.PathPrefix("/hls/hls_{segment}.ts").Handler(NewMiddlewareChain(
hls_transcode,
[]Middleware{SecureHeaders},
*app,
)).Methods("GET")
return nil
})
Hooks.Register.HttpEndpoint(func(r *mux.Router, app *App) error {
r.HandleFunc(OverrideVideoSourceMapper, func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", GetMimeType(req.URL.String()))
res.Write([]byte(`window.overrides["video-map-sources"] = function(sources){`))
res.Write([]byte(` return sources.map(function(source){`))
blacklists := strings.Split(blacklist_format(), ",")
for i := 0; i < len(blacklists); i++ {
blacklists[i] = strings.TrimSpace(blacklists[i])
res.Write([]byte(fmt.Sprintf(`if(source.type == "%s"){ return source; } `, GetMimeType("."+blacklists[i]))))
}
res.Write([]byte(` source.src = source.src + "&transcode=hls";`))
res.Write([]byte(` source.type = "application/x-mpegURL";`))
res.Write([]byte(` return source;`))
res.Write([]byte(` })`))
res.Write([]byte(`}`))
})
return nil
})
return nil
})
}