From d63dfc88d75a2279ee0917895f1b6ee9ed98ae61 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 7 Jul 2024 01:52:27 +0200 Subject: [PATCH] Add ffprobe Data to scan --- go.mod | 1 + go.sum | 2 ++ ...18_alter_files_table_ffprobe_data.down.sql | 2 ++ ...0018_alter_files_table_ffprobe_data.up.sql | 2 ++ server/scan.go | 21 ++++++++++++++++--- 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 migrations/000018_alter_files_table_ffprobe_data.down.sql create mode 100644 migrations/000018_alter_files_table_ffprobe_data.up.sql diff --git a/go.mod b/go.mod index 1cf601b..a46e664 100644 --- a/go.mod +++ b/go.mod @@ -26,4 +26,5 @@ require ( golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect + gopkg.in/vansante/go-ffprobe.v2 v2.2.0 // indirect ) diff --git a/go.sum b/go.sum index 576771c..002d41e 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/vansante/go-ffprobe.v2 v2.2.0 h1:iuOqTsbfYuqIz4tAU9NWh22CmBGxlGHdgj4iqP+NUmY= +gopkg.in/vansante/go-ffprobe.v2 v2.2.0/go.mod h1:qF0AlAjk7Nqzqf3y333Ly+KxN3cKF2JqA3JT5ZheUGE= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/migrations/000018_alter_files_table_ffprobe_data.down.sql b/migrations/000018_alter_files_table_ffprobe_data.down.sql new file mode 100644 index 0000000..b489a96 --- /dev/null +++ b/migrations/000018_alter_files_table_ffprobe_data.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE files +DROP IF EXISTS ffprobe; diff --git a/migrations/000018_alter_files_table_ffprobe_data.up.sql b/migrations/000018_alter_files_table_ffprobe_data.up.sql new file mode 100644 index 0000000..a0e64bf --- /dev/null +++ b/migrations/000018_alter_files_table_ffprobe_data.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE files +ADD ffprobe_data JSONB; diff --git a/server/scan.go b/server/scan.go index af3f322..6b65947 100644 --- a/server/scan.go +++ b/server/scan.go @@ -16,6 +16,7 @@ import ( "git.lastassault.de/speatzle/morffix/constants" "github.com/jackc/pgx/v5" + "gopkg.in/vansante/go-ffprobe.v2" ) var videoFileExtensions = []string{".mkv", ".mp4", ".webm", ".flv", ".avi"} @@ -226,8 +227,15 @@ func scanLibrary(ctx context.Context, id int) { if errors.Is(err, pgx.ErrNoRows) { // File Does not Exist Yet - slog.InfoContext(ctx, "File is New", "path", fullPath) - _, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health) VALUES ($1, $2, $3, $4, $5)", id, fPath, info.Size(), constants.FILE_STATUS_NEW, constants.FILE_HEALTH_UNKNOWN) + slog.InfoContext(ctx, "File is New, Running FFProbe...", "path", fullPath) + + ffprobeData, err := ffprobe.ProbeURL(ctx, fullPath) + if err != nil { + return fmt.Errorf("ffprobe New File: %w", err) + } + slog.InfoContext(ctx, "ffprobe Done", "path", fullPath) + + _, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health, ffprobe_data) VALUES ($1, $2, $3, $4, $5, $6)", id, fPath, info.Size(), constants.FILE_STATUS_NEW, constants.FILE_HEALTH_UNKNOWN, ffprobeData) if err != nil { return fmt.Errorf("Add New File to DB: %w", err) } @@ -248,7 +256,14 @@ func scanLibrary(ctx context.Context, id int) { } } else { slog.InfoContext(ctx, "File Has Changed", "path", fullPath, "old_mod_time", oldModTime, "new_mod_time", newModTime, "old_size", oldSize, "new_size", info.Size()) - _, err = tx.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4, mod_time = $5 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_CHANGED, constants.FILE_HEALTH_UNKNOWN, newModTime) + + ffprobeData, err := ffprobe.ProbeURL(ctx, fullPath) + if err != nil { + return fmt.Errorf("ffprobe Changed File: %w", err) + } + slog.InfoContext(ctx, "ffprobe Done", "path", fullPath) + + _, err = tx.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4, mod_time = $5, ffprobe_data = $6 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_CHANGED, constants.FILE_HEALTH_UNKNOWN, newModTime, ffprobeData) if err != nil { return fmt.Errorf("Updating Changed File in DB: %w", err) }