diff --git a/Dockerfile b/Dockerfile index 31e1b7b..e731559 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ ADD handlers /src/handlers ADD media /src/media ADD originals /src/originals Add playlists /src/playlists +Add ytdlp /src/ytdlp ADD go.mod /src/. RUN cd /src && go mod tidy diff --git a/handlers.go b/handlers.go index 5eacf84..d2bd520 100644 --- a/handlers.go +++ b/handlers.go @@ -11,13 +11,15 @@ import ( "strconv" "strings" "time" - "ytdlp-site/media" - "ytdlp-site/originals" - "ytdlp-site/playlists" "github.com/labstack/echo/v4" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" + + "ytdlp-site/media" + "ytdlp-site/originals" + "ytdlp-site/playlists" + "ytdlp-site/ytdlp" ) type Footer struct { @@ -175,7 +177,7 @@ type Meta struct { func getYtdlpTitle(url string, args []string) (string, error) { args = append(args, "--simulate", "--print", "%(title)s", url) - stdout, _, err := runYtdlp(args...) + stdout, _, err := ytdlp.Run(args...) if err != nil { log.Errorln(err) return "", err @@ -195,7 +197,7 @@ type PlaylistData struct { func getYtdlpPlaylist(url string) (PlaylistData, error) { var data PlaylistData - stdout, _, err := runYtdlp("--flat-playlist", "--dump-single-json", url) + stdout, _, err := ytdlp.Run("--flat-playlist", "--dump-single-json", url) if err != nil { log.Errorln(err) return data, err @@ -211,7 +213,7 @@ func getYtdlpPlaylist(url string) (PlaylistData, error) { func getYtdlpArtist(url string, args []string) (string, error) { args = append(args, "--simulate", "--print", "%(uploader)s", url) - stdout, _, err := runYtdlp(args...) + stdout, _, err := ytdlp.Run(args...) if err != nil { log.Errorln(err) return "", err @@ -221,7 +223,7 @@ func getYtdlpArtist(url string, args []string) (string, error) { func getYtdlpExt(url string, args []string) (string, error) { args = append(args, "--simulate", "--print", "%(ext)s", url) - stdout, _, err := runYtdlp(args...) + stdout, _, err := ytdlp.Run(args...) if err != nil { log.Errorln(err) return "", err diff --git a/main.go b/main.go index 8f7f112..cd5c88a 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,6 @@ import ( "os" "path/filepath" "time" - "ytdlp-site/database" - "ytdlp-site/handlers" - "ytdlp-site/media" - "ytdlp-site/originals" - "ytdlp-site/playlists" "github.com/gorilla/sessions" "github.com/labstack/echo/v4" @@ -20,6 +15,13 @@ import ( "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" + + "ytdlp-site/database" + "ytdlp-site/handlers" + "ytdlp-site/media" + "ytdlp-site/originals" + "ytdlp-site/playlists" + "ytdlp-site/ytdlp" ) var db *gorm.DB @@ -51,6 +53,7 @@ func main() { log.Infof("BuildDate: %s", getBuildDate()) handlers.Init(log) + ytdlp.Init(log) gormLogger := logger.New( golog.New(os.Stdout, "\r\n", golog.LstdFlags), // io writer diff --git a/ytdlp/init.go b/ytdlp/init.go new file mode 100644 index 0000000..6af5ef5 --- /dev/null +++ b/ytdlp/init.go @@ -0,0 +1,12 @@ +package ytdlp + +import "github.com/sirupsen/logrus" + +var log *logrus.Logger + +func Init(logger *logrus.Logger) error { + log = logger.WithFields(logrus.Fields{ + "component": "ytdlp", + }).Logger + return nil +} diff --git a/ytdlp.go b/ytdlp/ytdlp.go similarity index 68% rename from ytdlp.go rename to ytdlp/ytdlp.go index 89f685a..7caf08b 100644 --- a/ytdlp.go +++ b/ytdlp/ytdlp.go @@ -1,13 +1,14 @@ -package main +package ytdlp import ( "bytes" + "fmt" "os/exec" "strings" ) // runs ffprobe with the provided args and returns (stdout, stderr, error) -func runYtdlp(args ...string) ([]byte, []byte, error) { +func Run(args ...string) ([]byte, []byte, error) { ytdlp := "yt-dlp" log.Infoln(ytdlp, strings.Join(args, " ")) cmd := exec.Command(ytdlp, args...) @@ -24,3 +25,11 @@ func runYtdlp(args ...string) ([]byte, []byte, error) { log.Infoln("stderr:", stderr.String()) return stdout.Bytes(), stderr.Bytes(), err } + +func Clip(src, dst string, from, to float64) error { + _, _, err := Run("-i", src, + "-ss", fmt.Sprintf("%f", from), + "-to", fmt.Sprintf("%f", to), + dst) + return err +}