Skip to content

Commit

Permalink
remove width
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Aug 13, 2020
1 parent 3daac2e commit 417a4fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 60 deletions.
45 changes: 18 additions & 27 deletions services/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -21,12 +20,10 @@ import (
// Generator generates previews for video content
type Generator struct {
sourceURL string
format string
offset time.Duration
length time.Duration
infoHash string
path string
width int
inited bool
b []byte
err error
Expand All @@ -35,51 +32,45 @@ type Generator struct {
}

// NewGenerator initializes new Generator instance
func NewGenerator(s3 *S3Storage, sourceURL string, offset time.Duration, format string, width int, length time.Duration, infoHash string, path string) *Generator {
return &Generator{s3: s3, sourceURL: sourceURL, offset: offset, format: format, width: width,
length: length, infoHash: infoHash, path: path, inited: false}
func NewGenerator(s3 *S3Storage, sourceURL string, offset time.Duration, length time.Duration, infoHash string, path string) *Generator {
return &Generator{s3: s3, sourceURL: sourceURL, offset: offset,
length: length, infoHash: infoHash, path: path}
}

// Using following links:
// https://superuser.com/questions/538112/meaningful-thumbnails-for-a-video-using-ffmpeg
// https://stackoverflow.com/questions/52303867/how-do-i-set-ffmpeg-pipe-output
// https://www.bogotobogo.com/FFMpeg/ffmpeg_image_scaling_jpeg.php
func (s *Generator) getParams() ([]string, error) {
var codec, format string
switch s.format {
case "webp":
codec = "libwebp"
format = "image2"
default:
return nil, errors.Errorf("Unsupported format type %v", s.format)
}
codec := "libwebp"
format := "image2"
params := []string{
"-ss", fmt.Sprintf("%d", int(s.offset.Seconds())),
"-i", s.sourceURL,
"-frames:v", "1",
"-c:v", codec,
"-f", format,
}
if s.length == 0 {
params = append(params, "-frames:v", "1")
} else {
params = append(params, "-t", strconv.Itoa(int(s.length.Seconds())))
}
// if s.length == 0 {
// params = append(params, "-frames:v", "1")
// } else {
// params = append(params, "-t", strconv.Itoa(int(s.length.Seconds())))
// }
// vf := []string{"select=gt(scene\\,0.5)"}
vf := []string{}
if s.width != 0 {
vf = append(vf, fmt.Sprintf("scale=%d:-1", s.width))
}
if len(vf) > 0 {
params = append(params, "-vf", strings.Join(vf, ","))
}
// vf := []string{}
// if s.width != 0 {
// vf = append(vf, fmt.Sprintf("scale=%d:-1", s.width))
// }
// if len(vf) > 0 {
// params = append(params, "-vf", strings.Join(vf, ","))
// }
params = append(params, "-")
logrus.Infof("FFmpeg params %v", params)
return params, nil
}

func (s *Generator) getKey() string {
name := fmt.Sprintf("%v-%v-%v.%v", s.width, int(s.offset.Seconds()), int(s.length.Seconds()), s.format)
name := fmt.Sprintf("%v-%v.%v", int(s.offset.Seconds()), int(s.length.Seconds()), "webp")
path := ""
if s.infoHash != "" && s.path != "" {
path = s.infoHash + strings.Split(s.path, "~")[0]
Expand Down
6 changes: 3 additions & 3 deletions services/generator_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func NewGeneratorPool(s3 *S3Storage) *GeneratorPool {
}

// Get gets Generator
func (s *GeneratorPool) Get(sourceURL string, offset time.Duration, format string, width int, length time.Duration, infoHash string, path string) *Generator {
key := fmt.Sprintf("%v%v%v%v%v%v", offset, format, width, length, infoHash, path)
v, _ := s.sm.LoadOrStore(key, NewGenerator(s.s3, sourceURL, offset, format, width, length, infoHash, path))
func (s *GeneratorPool) Get(sourceURL string, offset time.Duration, length time.Duration, infoHash string, path string) *Generator {
key := fmt.Sprintf("%v%v%v%v%v%v", offset, length, infoHash, path)
v, _ := s.sm.LoadOrStore(key, NewGenerator(s.s3, sourceURL, offset, length, infoHash, path))
t, tLoaded := s.timers.LoadOrStore(key, time.NewTimer(s.expire))
timer := t.(*time.Timer)
if !tLoaded {
Expand Down
31 changes: 1 addition & 30 deletions services/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"net"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"time"

logrusmiddleware "github.com/bakins/logrus-middleware"
Expand Down Expand Up @@ -95,25 +93,6 @@ func (s *Web) getLength(r *http.Request) (time.Duration, error) {
return time.Duration(i) * time.Second, nil
}

func (s *Web) getFormat(r *http.Request) (string, error) {
format := strings.TrimPrefix(filepath.Ext(r.URL.Path), ".")
if format == "" {
return "webp", nil
}
return format, nil
}
func (s *Web) getWidth(r *http.Request) (int, error) {
width := r.URL.Query().Get("width")
if width == "" {
return 0, nil
}
i, err := strconv.Atoi(width)
if err != nil {
return 0, errors.Wrapf(err, "Failed to parse width")
}
return i, nil
}

func (s *Web) getReader(r *http.Request) (io.Reader, error) {
sURL := s.getSourceURL(r)
parsedURL, err := url.Parse(sURL)
Expand All @@ -131,15 +110,7 @@ func (s *Web) getReader(r *http.Request) (io.Reader, error) {
if err != nil {
return nil, errors.Errorf("Failed to get length")
}
format, err := s.getFormat(r)
if err != nil {
return nil, errors.Errorf("Failed to get format")
}
width, err := s.getWidth(r)
if err != nil {
return nil, errors.Errorf("Failed to get width")
}
g := s.gp.Get(sURL, offset, format, width, length, infoHash, path)
g := s.gp.Get(sURL, offset, length, infoHash, path)
rr, err := g.Get()
if err != nil {
return nil, errors.Wrapf(err, "Failed to get reader")
Expand Down
Binary file removed test.webp
Binary file not shown.

0 comments on commit 417a4fc

Please sign in to comment.