ytdlp package

This commit is contained in:
Carl Pearson
2024-10-13 06:09:23 -06:00
parent d5e3f5c656
commit ccd21636cf
5 changed files with 41 additions and 14 deletions

View File

@@ -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

View File

@@ -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

13
main.go
View File

@@ -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

12
ytdlp/init.go Normal file
View File

@@ -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
}

View File

@@ -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
}