Skip to content

Commit

Permalink
Generate latest thumbnail image for each segment (#37)
Browse files Browse the repository at this point in the history
* Generate latest thumbnail image for each segment

* more efficient to just set the timepoint to extract from

* log context

* resize

* Revert "log context"

This reverts commit 2bc1688.

* return error and log
  • Loading branch information
mjh1 authored Oct 23, 2023
1 parent 7306780 commit de93a9a
Showing 1 changed file with 59 additions and 1 deletion.
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 @@ import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

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

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

var fileContents = []byte{}
Expand Down Expand Up @@ -88,3 +98,51 @@ func Upload(input io.Reader, outputURI string, waitBetweenWrites, writeTimeout t

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

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

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
}

0 comments on commit de93a9a

Please sign in to comment.