-
Notifications
You must be signed in to change notification settings - Fork 13
/
thumbnails.go
73 lines (63 loc) · 1.65 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
68
69
70
71
72
73
package main
import (
"context"
"fmt"
"image/png"
"os"
"path/filepath"
"github.com/djthorpe/gopi/v3"
)
func (this *app) Thumbnails(ctx context.Context) error {
count := uint(0)
// Process files
if paths, err := GetFileArgs(this.Command.Args()); err != nil {
return err
} else if err := this.Walk(ctx, paths, &count, func(path string, info os.FileInfo) error {
if err := this.ProcessThumbnails(path); err != nil {
if *this.quiet == false {
this.Logger.Print(filepath.Base(path), ": ", err)
}
}
return nil
}); err != nil {
return err
}
return nil
}
func (this *app) ProcessThumbnails(path string) error {
media, err := this.MediaManager.OpenFile(path)
if err != nil {
return err
}
defer this.MediaManager.Close(media)
// Get video stream
streams := media.StreamsForFlag(gopi.MEDIA_FLAG_VIDEO)
if len(streams) == 0 {
return fmt.Errorf("No video information found")
}
if err := media.DecodeIterator([]int{streams[0]}, func(ctx gopi.MediaDecodeContext, packet gopi.MediaPacket) error {
return media.DecodeFrameIterator(ctx, packet, func(ctx gopi.MediaDecodeContext, frame gopi.MediaFrame) error {
return this.ProcessFrame(path, ctx, frame)
})
}); err != nil {
return err
}
// Return success
return nil
}
func (this *app) ProcessFrame(path string, ctx gopi.MediaDecodeContext, frame gopi.MediaFrame) error {
filename := fmt.Sprintf("%06d", ctx.Frame()) + ".thumbnail.png"
out := filepath.Join(os.TempDir(), filename)
w, err := os.Create(out)
if err != nil {
return err
}
defer w.Close()
if err := png.Encode(w, frame); err != nil {
return err
} else {
fmt.Println(frame)
fmt.Println(" =>", out)
}
return nil
}