This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nyt-devito.go
76 lines (71 loc) · 2.24 KB
/
nyt-devito.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
68
69
70
71
72
73
74
75
76
package main
import (
"errors"
"flag"
"io"
"log"
"net/url"
"os"
"regexp"
"strings"
"time"
sprite "github.com/fsouza/vod-module-sprite"
)
func main() {
packagerEndpoint := flag.String("packager", "http://localhost:3030", "endpoint of the packager")
maxWorkers := flag.Uint("max-workers", 32, "maximum number of workers to be used for thumbnail generation")
output := flag.String("o", "thumb.jpg", "output file")
url := flag.String("url", "http://localhost:3030/videos/devito480p.mp4", "url of the source video")
width := flag.Uint("width", 0, "width of each sprite item - 0 for keeping the aspect ratio/source")
height := flag.Uint("height", 0, "height of each sprite item - 0 for keeping the aspect ratio/source")
interval := flag.Duration("interval", 2*time.Second, "interval between captures")
start := flag.Duration("start", 0, "timecode for the starting point")
end := flag.Duration("end", 2*time.Minute, "timecode for the end point")
keepRatio := flag.Bool("keep-ratio", false, "keep aspect ratio?")
flag.Parse()
generator := sprite.Generator{
Translator: getTranslator(*packagerEndpoint),
MaxWorkers: *maxWorkers,
}
data, err := generator.GenSprite(sprite.GenSpriteOptions{
VideoURL: *url,
Width: *width,
Height: *height,
Start: *start,
End: *end,
Interval: *interval,
KeepAspectRatio: *keepRatio,
JPEGQuality: 80,
})
if err != nil {
log.Fatalf("failed to generate sprite: %v", err)
}
f, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
log.Fatal(err)
}
if n != len(data) {
log.Fatalf("failed to write %q: %v", *output, io.ErrShortWrite)
}
log.Printf("successfully generated thumbnail %q", *output)
}
func getTranslator(packagerEndpoint string) sprite.VideoURLTranslator {
videoMatch := regexp.MustCompile(`^/videos/(.*)$`)
packagerEndpoint = strings.TrimRight(packagerEndpoint, "/")
return func(videoURL string) (string, error) {
vurl, err := url.Parse(videoURL)
if err != nil {
return "", err
}
path := videoMatch.ReplaceAllString(vurl.Path, "/thumb/$1")
if path == vurl.Path {
return "", errors.New("invalid video url")
}
return packagerEndpoint + path, nil
}
}