From 5c3028dfe8ee1e9d8ba935cf985b5cc7b0c41a9c Mon Sep 17 00:00:00 2001 From: Max Holland Date: Tue, 17 Oct 2023 16:10:19 +0100 Subject: [PATCH 1/6] Generate latest thumbnail image for each segment --- core/uploader.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/core/uploader.go b/core/uploader.go index 85d2d6a..55a681c 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -7,6 +7,9 @@ import ( "fmt" "io" "log" + "os" + "os/exec" + "path/filepath" "strings" "time" @@ -38,7 +41,12 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t // For segments we just write them in one go here and return early. // (Otherwise the incremental write logic below caused issues with clipping since it results in partial segments being written.) _, err := session.SaveData(context.Background(), "", input, fields, writeTimeout) - return err + if err != nil { + return err + } + + extractThumb(session) + return nil } var fileContents = []byte{} @@ -88,3 +96,56 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return nil } + +func extractThumb(session drivers.OSSession) { + presigned, err := session.Presign("", 5*time.Minute) + if err != nil { + log.Printf("Presigning failed: %s", err) + return + } + + outDir, err := os.MkdirTemp(os.TempDir(), "thumb-*") + if err != nil { + log.Printf("Temp file creation failed: %s", err) + return + } + defer os.RemoveAll(outDir) + outFile := filepath.Join(outDir, "out.jpg") + + args := []string{ + "-i", presigned, + "-vf", "select=eq(pict_type\\,I)", + "-vsync", "vfr", + "-vf", fmt.Sprintf("fps=1/10"), + "-update", "1", + "-y", + outFile, + } + + timeout, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + cmd := exec.CommandContext(timeout, "ffmpeg", args...) + + var outputBuf bytes.Buffer + var stdErr bytes.Buffer + cmd.Stdout = &outputBuf + cmd.Stderr = &stdErr + + err = cmd.Run() + if err != nil { + log.Printf("ffmpeg failed[%s] [%s]: %s", outputBuf.String(), stdErr.String(), err) + return + } + + f, err := os.Open(outFile) + if err != nil { + log.Printf("Opening file failed: %s", err) + return + } + defer f.Close() + _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 1*time.Minute) + if err != nil { + log.Printf("Saving thumbnail failed: %s", err) + return + } +} From 39763c086835d9b343b1d30da4a71ca6dd429ba9 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Wed, 18 Oct 2023 15:17:35 +0100 Subject: [PATCH 2/6] more efficient to just set the timepoint to extract from --- core/uploader.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/uploader.go b/core/uploader.go index 55a681c..7717510 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -114,15 +114,13 @@ func extractThumb(session drivers.OSSession) { args := []string{ "-i", presigned, - "-vf", "select=eq(pict_type\\,I)", - "-vsync", "vfr", - "-vf", fmt.Sprintf("fps=1/10"), - "-update", "1", + "-ss", "00:00:00", + "-vframes", "1", "-y", outFile, } - timeout, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() cmd := exec.CommandContext(timeout, "ffmpeg", args...) @@ -143,7 +141,7 @@ func extractThumb(session drivers.OSSession) { return } defer f.Close() - _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 1*time.Minute) + _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 10*time.Second) if err != nil { log.Printf("Saving thumbnail failed: %s", err) return From 2bc16887368ac69505d509068a9f21e0a80901c1 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Thu, 19 Oct 2023 15:49:57 +0100 Subject: [PATCH 3/6] log context --- core/uploader.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/uploader.go b/core/uploader.go index 7717510..cb36132 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -45,7 +45,7 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return err } - extractThumb(session) + extractThumb(outputURI, session) return nil } @@ -97,16 +97,16 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return nil } -func extractThumb(session drivers.OSSession) { +func extractThumb(uri string, session drivers.OSSession) { presigned, err := session.Presign("", 5*time.Minute) if err != nil { - log.Printf("Presigning failed: %s", err) + log.Printf("Presigning failed for %s: %s", uri, err) return } outDir, err := os.MkdirTemp(os.TempDir(), "thumb-*") if err != nil { - log.Printf("Temp file creation failed: %s", err) + log.Printf("Temp file creation failed for %s: %s", uri, err) return } defer os.RemoveAll(outDir) @@ -131,19 +131,19 @@ func extractThumb(session drivers.OSSession) { err = cmd.Run() if err != nil { - log.Printf("ffmpeg failed[%s] [%s]: %s", outputBuf.String(), stdErr.String(), err) + log.Printf("ffmpeg failed for %s [%s] [%s]: %s", uri, outputBuf.String(), stdErr.String(), err) return } f, err := os.Open(outFile) if err != nil { - log.Printf("Opening file failed: %s", err) + log.Printf("Opening file failed for %s: %s", uri, err) return } defer f.Close() _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 10*time.Second) if err != nil { - log.Printf("Saving thumbnail failed: %s", err) + log.Printf("Saving thumbnail failed for %s: %s", uri, err) return } } From da137ff465836abdb4142a0aae6b4ab67b3f9de6 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Fri, 20 Oct 2023 13:43:46 +0100 Subject: [PATCH 4/6] resize --- core/uploader.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/uploader.go b/core/uploader.go index cb36132..5824d75 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -116,6 +116,7 @@ func extractThumb(uri string, session drivers.OSSession) { "-i", presigned, "-ss", "00:00:00", "-vframes", "1", + "-vf", "scale=320:240:force_original_aspect_ratio=decrease", "-y", outFile, } From 48a03ae88e12d908dda63c6ddc9c1bcc89d81422 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Mon, 23 Oct 2023 10:33:54 +0100 Subject: [PATCH 5/6] Revert "log context" This reverts commit 2bc16887368ac69505d509068a9f21e0a80901c1. --- core/uploader.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/uploader.go b/core/uploader.go index 5824d75..2aad575 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -45,7 +45,7 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return err } - extractThumb(outputURI, session) + extractThumb(session) return nil } @@ -97,16 +97,16 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return nil } -func extractThumb(uri string, session drivers.OSSession) { +func extractThumb(session drivers.OSSession) { presigned, err := session.Presign("", 5*time.Minute) if err != nil { - log.Printf("Presigning failed for %s: %s", uri, err) + log.Printf("Presigning failed: %s", err) return } outDir, err := os.MkdirTemp(os.TempDir(), "thumb-*") if err != nil { - log.Printf("Temp file creation failed for %s: %s", uri, err) + log.Printf("Temp file creation failed: %s", err) return } defer os.RemoveAll(outDir) @@ -132,19 +132,19 @@ func extractThumb(uri string, session drivers.OSSession) { err = cmd.Run() if err != nil { - log.Printf("ffmpeg failed for %s [%s] [%s]: %s", uri, outputBuf.String(), stdErr.String(), err) + log.Printf("ffmpeg failed[%s] [%s]: %s", outputBuf.String(), stdErr.String(), err) return } f, err := os.Open(outFile) if err != nil { - log.Printf("Opening file failed for %s: %s", uri, err) + log.Printf("Opening file failed: %s", err) return } defer f.Close() _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 10*time.Second) if err != nil { - log.Printf("Saving thumbnail failed for %s: %s", uri, err) + log.Printf("Saving thumbnail failed: %s", err) return } } From bb3923c41149ad755632c299366217dcd3db1ac5 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Mon, 23 Oct 2023 10:43:37 +0100 Subject: [PATCH 6/6] return error and log --- core/uploader.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/core/uploader.go b/core/uploader.go index 2aad575..f367892 100644 --- a/core/uploader.go +++ b/core/uploader.go @@ -45,7 +45,9 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return err } - extractThumb(session) + if err = extractThumb(session); err != nil { + log.Printf("extracting thumbnail failed: %s", err) + } return nil } @@ -97,17 +99,15 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t return nil } -func extractThumb(session drivers.OSSession) { +func extractThumb(session drivers.OSSession) error { presigned, err := session.Presign("", 5*time.Minute) if err != nil { - log.Printf("Presigning failed: %s", err) - return + return fmt.Errorf("presigning failed: %w", err) } outDir, err := os.MkdirTemp(os.TempDir(), "thumb-*") if err != nil { - log.Printf("Temp file creation failed: %s", err) - return + return fmt.Errorf("temp file creation failed: %w", err) } defer os.RemoveAll(outDir) outFile := filepath.Join(outDir, "out.jpg") @@ -132,19 +132,17 @@ func extractThumb(session drivers.OSSession) { err = cmd.Run() if err != nil { - log.Printf("ffmpeg failed[%s] [%s]: %s", outputBuf.String(), stdErr.String(), err) - return + return fmt.Errorf("ffmpeg failed[%s] [%s]: %w", outputBuf.String(), stdErr.String(), err) } f, err := os.Open(outFile) if err != nil { - log.Printf("Opening file failed: %s", err) - return + return fmt.Errorf("opening file failed: %w", err) } defer f.Close() _, err = session.SaveData(context.Background(), "../latest.jpg", f, &drivers.FileProperties{CacheControl: "max-age=5"}, 10*time.Second) if err != nil { - log.Printf("Saving thumbnail failed: %s", err) - return + return fmt.Errorf("saving thumbnail failed: %w", err) } + return nil }