/
thumbnails.go
67 lines (62 loc) · 1.83 KB
/
thumbnails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package worker
import (
"io"
"os"
"time"
"github.com/joschahenningsen/thumbgen"
log "github.com/sirupsen/logrus"
)
const (
ThumbWidth = 160 // Width in pixels, height is inferred by thumbgen
LargeThumbWidth = 720
Compression = 90 // Compression in percent
)
// createThumbnailSprite creates a thumbnail sprite from the given video file for the seekbar and stores it in mass storage.
func createThumbnailSprite(ctx *StreamContext, source string) error {
secondsPerHour := uint32(time.Hour.Seconds())
switch {
case ctx.duration < secondsPerHour:
ctx.thumbInterval = 10
case ctx.duration > secondsPerHour*3:
ctx.thumbInterval = 60
default:
ctx.thumbInterval = 30
}
log.WithField("File", ctx.getThumbnailSpriteFileName()).Info("Start creating thumbnail sprite")
g, err := thumbgen.New(source, ThumbWidth, int(ctx.thumbInterval), ctx.getThumbnailSpriteFileName(), thumbgen.WithJpegCompression(Compression))
if err != nil {
return err
}
err = g.Generate()
log.WithField("file", ctx.getThumbnailSpriteFileName()).Info("Finished creating thumbnail sprite")
return err
}
// createVideoThumbnail creates a thumbnail from the given video file and stores it in mass storage.
func createVideoThumbnail(ctx *StreamContext, source string) error {
g, err := thumbgen.New(source, LargeThumbWidth, 1, "", thumbgen.WithJpegCompression(Compression))
if err != nil {
return err
}
path, err := g.GenerateOne()
if err != nil {
return err
}
thumb, err := os.Open(path)
if err != nil {
return err
}
file, err := os.OpenFile(ctx.getLargeThumbnailSpriteFileName(), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, thumb)
if err != nil {
return err
}
err = os.Remove(path)
if err != nil {
log.WithError(err).Warn("Could not remove temporary thumbnail file")
}
return nil
}