-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamg.go
167 lines (141 loc) · 3.13 KB
/
amg.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bytes"
"fmt"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"net/http"
"os"
"os/exec"
ffmpeg "github.com/u2takey/ffmpeg-go"
)
var (
// ascii grayscale rank, If you find a better way to combine grayscale values than this, submit plz!
ramp = " .,-=+CNMGB@"
// specify Reverse to change contrast
Reverse = false
// specify ScaleX and ScaleY to resize the pictures
ScaleX, ScaleY = 3, 1
)
func init() {
if Reverse {
ramp = reverseString(ramp)
}
}
func reverseString(str string) string {
s := []rune(str)
l, r := 0, len(s)-1
for l < r {
s[l], s[r] = s[r], s[l]
l++
r--
}
return string(s)
}
// load image
func LoadImage(path string) (image.Image, error) {
if path[0:4] == "http" {
// http get
res, err := http.Get(path)
if err != nil {
return nil, err
}
defer res.Body.Close()
image, _, err := image.Decode(res.Body)
return image, err
} else {
// local path
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
image, _, err := image.Decode(f)
return image, err
}
}
// rgb to gray
func rgb2gray(c color.Color) int {
r, g, b, _ := c.RGBA()
return int(0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b))
}
// image to ascii character
func image2Ascii(img image.Image) []byte {
buf := bytes.Buffer{}
max := img.Bounds().Max
for y := 0; y < max.Y; y += ScaleX {
for x := 0; x < max.X; x += ScaleY {
c := avgPixel(img, x, y, ScaleX, ScaleY)
buf.WriteByte(ramp[len(ramp)*c/65536])
}
buf.WriteByte('\n')
}
return buf.Bytes()
}
func SaveFrameAsAscii(path string, index int) error {
// get frame from video
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(path).Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", index)}).
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "png"}).
WithOutput(buf).Run()
if err != nil {
return err
}
// decode
img, _, err := image.Decode(buf)
if err != nil {
return err
}
// img to ascii
file, err := os.OpenFile(fmt.Sprintf("frames/frame:%d", index), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer file.Close()
bytes := image2Ascii(img)
file.Write(bytes)
file.Close()
fmt.Printf("save video frame:%d as file success\n", index)
return nil
}
func avgPixel(img image.Image, x, y, w, h int) int {
cnt, sum, max := 0, 0, img.Bounds().Max
for i := x; i < x+w && i < max.X; i++ {
for j := y; j < y+h && j < max.Y; j++ {
sum += rgb2gray(img.At(i, j))
cnt++
}
}
return sum / cnt
}
func main() {
// img, err := LoadImage("https://i0.hdslb.com/bfs/new_dyn/615c8071c1c4beba47e6c7971b8561e4470962000.jpg")
// if err != nil {
// panic(err)
// }
// bytes := image2Ascii(img)
// fmt.Println(string(bytes))
// file := "download/test1.flv"
// for i := 0; ; i++ {
// if err := SaveFrameAsAscii(file, i); err != nil {
// break
// }
// }
// fmt.Println("ascii videos complete")
Play()
}
// play ascii video on console
func Play() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
for i := 0; ; i++ {
file, err := os.ReadFile(fmt.Sprintf("frames/frame:%d", i))
if err != nil {
break
}
cmd.Run()
os.Stdout.Write(file)
}
}