Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate latest thumbnail image for each segment #37

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion core/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -38,7 +41,14 @@
// 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
}

Check warning on line 46 in core/uploader.go

View check run for this annotation

Codecov / codecov/patch

core/uploader.go#L44-L46

Added lines #L44 - L46 were not covered by tests

if err = extractThumb(session); err != nil {
log.Printf("extracting thumbnail failed: %s", err)
}
return nil

Check warning on line 51 in core/uploader.go

View check run for this annotation

Codecov / codecov/patch

core/uploader.go#L48-L51

Added lines #L48 - L51 were not covered by tests
}

var fileContents = []byte{}
Expand Down Expand Up @@ -88,3 +98,51 @@

return nil
}

func extractThumb(session drivers.OSSession) error {
presigned, err := session.Presign("", 5*time.Minute)
if err != nil {
return fmt.Errorf("presigning failed: %w", err)
}

Check warning on line 106 in core/uploader.go

View check run for this annotation

Codecov / codecov/patch

core/uploader.go#L102-L106

Added lines #L102 - L106 were not covered by tests

outDir, err := os.MkdirTemp(os.TempDir(), "thumb-*")
if err != nil {
return fmt.Errorf("temp file creation failed: %w", err)
}
defer os.RemoveAll(outDir)
outFile := filepath.Join(outDir, "out.jpg")

args := []string{
"-i", presigned,
"-ss", "00:00:00",
"-vframes", "1",
"-vf", "scale=320:240:force_original_aspect_ratio=decrease",
"-y",
outFile,
}

timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
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 {
return fmt.Errorf("ffmpeg failed[%s] [%s]: %w", outputBuf.String(), stdErr.String(), err)
}

Check warning on line 136 in core/uploader.go

View check run for this annotation

Codecov / codecov/patch

core/uploader.go#L108-L136

Added lines #L108 - L136 were not covered by tests

f, err := os.Open(outFile)
if err != nil {
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 {
return fmt.Errorf("saving thumbnail failed: %w", err)
}
return nil

Check warning on line 147 in core/uploader.go

View check run for this annotation

Codecov / codecov/patch

core/uploader.go#L138-L147

Added lines #L138 - L147 were not covered by tests
}
Loading