From 30cb171612a26c1f34b0331f0e9c07eeae2dc1ca Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 17 Jan 2024 18:17:06 +0100 Subject: [PATCH] Add basic extraction methods --- transcoder/main.go | 43 +++++++++++++++++++++++++++++++++++++++ transcoder/src/extract.go | 42 ++++++++++++++++++++++++++++++++++++++ transcoder/utils.go | 7 +++++++ 3 files changed, 92 insertions(+) create mode 100644 transcoder/src/extract.go diff --git a/transcoder/main.go b/transcoder/main.go index b1953f87..7ff1b6d8 100644 --- a/transcoder/main.go +++ b/transcoder/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net/http" "strconv" @@ -208,6 +209,46 @@ func GetInfo(c echo.Context) error { return c.JSON(http.StatusOK, ret) } +// Get attachments +// +// Get a specific attachment +// +// Path: /:sha/attachment/:name +func GetAttachment(c echo.Context) error { + sha := c.Param("sha") + name := c.Param("name") + + if err := SanitizePath(sha); err != nil { + return err + } + if err := SanitizePath(name); err != nil { + return err + } + + path := fmt.Sprintf("%s/%s/att/%s", src.GetMetadataPath(), sha, name) + return c.File(path) +} + +// Get subtitle +// +// Get a specific subtitle +// +// Path: /:sha/sub/:name +func GetSubtitle(c echo.Context) error { + sha := c.Param("sha") + name := c.Param("name") + + if err := SanitizePath(sha); err != nil { + return err + } + if err := SanitizePath(name); err != nil { + return err + } + + path := fmt.Sprintf("%s/%s/sub/%s", src.GetMetadataPath(), sha, name) + return c.File(path) +} + type Handler struct { transcoder *src.Transcoder } @@ -231,6 +272,8 @@ func main() { e.GET("/:resource/:slug/:quality/:chunk", h.GetVideoSegment) e.GET("/:resource/:slug/audio/:audio/:chunk", h.GetAudioSegment) e.GET("/:resource/:slug/info", GetInfo) + e.GET("/:sha/attachment/:name", GetAttachment) + e.GET("/:sha/sub/:name", GetSubtitle) e.Logger.Fatal(e.Start(":7666")) } diff --git a/transcoder/src/extract.go b/transcoder/src/extract.go new file mode 100644 index 00000000..0ec4d24e --- /dev/null +++ b/transcoder/src/extract.go @@ -0,0 +1,42 @@ +package src + +import ( + "fmt" + "os" + "os/exec" +) + +func GetMetadataPath() string { + out := os.Getenv("GOCODER_METADATA_ROOT") + if out == "" { + return "/metadata" + } + return out +} + +func extract(path string, sha string, subs *[]Subtitle) { + fmt.Printf("Extract subs and fonts for %s", path) + cmd := exec.Command( + "ffmpeg", + "-dump_attachment:t", "", + "-i", path, + ) + cmd.Dir = fmt.Sprintf("/%s/%s/att/", GetMetadataPath(), sha) + + for _, sub := range *subs { + if ext := sub.Extension; ext != nil { + cmd.Args = append( + cmd.Args, + "-map", fmt.Sprintf("0:s:%d", sub.Index), + "-c:s", "copy", + fmt.Sprintf("/%s/%s/sub/%d.%s", GetMetadataPath(), sha, sub.Index, *ext), + ) + } + } + fmt.Printf("Starting extraction with the command: %s", cmd) + cmd.Stdout = nil + err := cmd.Run() + if err != nil { + fmt.Println("Error starting ffmpeg extract:", err) + } +} diff --git a/transcoder/utils.go b/transcoder/utils.go index c1c6e1aa..bc3f3901 100644 --- a/transcoder/utils.go +++ b/transcoder/utils.go @@ -56,6 +56,13 @@ func GetPath(resource string, slug string) (string, error) { return ret.Path, nil } +func SanitizePath(path string) error { + if strings.Contains(path, "/") || strings.Contains(path, "..") { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid parameter. Can't contains path delimiters or ..") + } + return nil +} + func GetClientId(c echo.Context) (string, error) { key := c.Request().Header.Get("X-CLIENT-ID") if key == "" {