forked from drmly/drmly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdream.go
337 lines (321 loc) · 11.4 KB
/
dream.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/TableMountain/goydl"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/skratchdot/open-golang/open"
)
var isJob bool
var basePath string
func Truncate(t time.Time) time.Time {
return t.Truncate(24 * time.Hour)
}
// Dream is exported so it can be an api, haha what fun. Games perhaps? Stock trading? Some real time video effect?
func Dream(c *gin.Context) {
start := time.Now()
defer func() {
elapsed := fmt.Sprintf("%s",time.Since(start))
elapsed=strings.Split(elapsed, ".")[0] + "s"
Log.Info("job took ", elapsed)
mel.Broadcast([]byte(elapsed))
}()
yt := c.PostForm("yt")
fps := c.PostForm("fps")
ov := c.PostForm("ov") //data the user uploaded we want
ovf := c.PostForm("ovf")
of := c.PostForm("of")
oo := c.PostForm("oo")
it := c.PostForm("iterations")
oc := c.PostForm("octaves")
la := c.PostForm("layer")
rl := c.PostForm("rl")
Log.Info("rl: ", rl)
ow := c.PostForm("ow")
li := c.PostForm("li")
iw := c.PostForm("iw")
rle := c.PostForm("rle")
// stretch:=c.Postform("stretchvideo")
isJob = true
defer func() {
isJob = false
}()
Log.WithFields(logrus.Fields{
"event": "new job started",
})
jobLog.WithFields(logrus.Fields{
"time": time.Now().UTC().UnixNano(),
"title": name,
"fps": fps,
"it": it,
"oc": oc,
"la": la,
"rl": rl,
"ow": ow,
"li": li,
"iw": iw,
"rle": rle,
})
Log.Info("base path is ", basePath)
newJobLog(name)
//let's save interesting job metadata for the user in a tidy format (err logs, srv logs kept with the binary or maybe put in bind dir? wip)
jobLog.WithFields(logrus.Fields{
"fps": fps,
"iterations": it,
"octaves": oc,
"layer": la,
"linear increase": li,
"iteration waver": iw,
"octave waver": ow,
"randomization type": rl,
"random layer every n frames": rle,
}).Info("job name: ", name)
//
var uploadedFile, framesDirPath string
var name, fullName, ext string
if yt != "" { //if "yt" checkbox checked
youtubeDl := goydl.NewYoutubeDl()
for { //we loop until we got an acceptable ytURL
fmt.Println("waiting...")
youtubeDl.VideoURL = ytURL
fmt.Println("videoURL:", ytURL)
if ytURL == "" { //we didn't get a url, so just cancel the job
Log.Info("the url was blank (therefore no good ytURL yet), so just cancel the job")
return
}
info, err := youtubeDl.GetInfo()
if err != nil {
Log.WithFields(logrus.Fields{
"event": "ytdl",
"error": err,
}).Error("we should never fail here")
continue
}
fmt.Println(youtubeDl.VideoURL, "blah")
ext = info.Ext
name = strings.Split(info.Title, " ")[0]
fullName = name + ".mp4"
if alreadyHave(basePath + "/frames/" + name) {
name = renamer(name)
fullName = name + ".mp4"
Log.Info("\nwe renamed as: ", fullName)
}
uploadedFile := fmt.Sprintf("%s/frames/%s/%s.mp4", basePath, name, name)
fmt.Println("uploaded file: ", uploadedFile)
youtubeDl.Options.Output.Value = uploadedFile
youtubeDl.Options.Format.Value = "mp4"
cmd, err := youtubeDl.Download(youtubeDl.VideoURL)
if err != nil {
Log.WithFields(logrus.Fields{
"event": "error",
"err": err,
"uploadedFile": uploadedFile,
}).Error("dl'ing from yt failed w err")
} else {
Log.WithFields(logrus.Fields{
"event": "download",
"path": uploadedFile,
}).Info("downloaded a yt video")
println("starting download")
cmd.Wait()
println("finished download")
// make new folder for job
framesDirPath = fmt.Sprintf("%s/frames/%s", basePath, name)
if _, err := os.Stat(framesDirPath); os.IsNotExist(err) {
if err = os.Mkdir(framesDirPath, 0777); err != nil {
Log.Error("failed to make a new job dir w/ error: ", err)
}
Log.Info("frames folder for new job was created at ", framesDirPath)
}
break //we got our file, now we move on, we don't need to keep listening for URL
}
}
} else { // if no youtube, then get file from form upload
file, err := c.FormFile("file")
if err != nil {
Log.Error("failed to get file", err) //although this might not be an error as we support ytdl now
return
}
name = strings.Split(file.Filename, ".")[0]
fullName = file.Filename
ext = strings.Split(fullName, ".")[1]
if alreadyHave(basePath + "/frames/" + name) {
name = renamer(name)
fullName = name + "." + strings.Split(file.Filename, ".")[1]
Log.Info("\nwe renamed as: ", fullName)
}
// make new folder for job
framesDirPath = fmt.Sprintf("%s/frames/%s", basePath, name)
if _, err := os.Stat(framesDirPath); os.IsNotExist(err) {
if err = os.Mkdir(framesDirPath, 0777); err != nil {
Log.Error("failed to make a new job dir w/ error: ", err)
}
Log.Info("frames folder for new job was created at ", framesDirPath)
}
uploadedFile = fmt.Sprintf("%s/%s", framesDirPath, fullName)
if err := c.SaveUploadedFile(file, uploadedFile); err != nil {
Log.Error("failed to save file at path ", uploadedFile, " err is: ", err)
} else {
Log.Info("saved file at path ", uploadedFile)
}
}
// make a new output folder
outputPath := fmt.Sprintf("%s/output", framesDirPath)
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
os.Mkdir(outputPath, 0777)
Log.Info("output folder for new job was created at ", outputPath)
}
Log.Info("saved output dir at path ", outputPath)
uploadedFile = fmt.Sprintf("%s/%s", framesDirPath, fullName)
mel.Broadcast([]byte(name))
itsAVideo := false
// decide what to do with the file we've gotten, if it's an image:
if ext == "png" { //it's perfect, leave it alone...
} else if ext == "jpg" || ext == "jpeg" {
cmd, err := exec.Command("ffmpeg", "-i", uploadedFile, framesDirPath+"/"+name+".png").CombinedOutput()
if err != nil {
Log.Error("oops, failed trying to make some image of ext ", ext, " to png")
} else {
Log.Info("that's great, we got an image, those are easy, ffmpeg said:", string(cmd))
}
} else if ext == "gif" {
itsAVideo = true
Log.Info("trying to convert a gif")
// ffmpeg -f gif -i giphy-downsized.gif -pix_fmt yuv420p -c:v libx264 -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' BAR.mp4
savedMp4 := fmt.Sprintf("%s/frames/%s/%s.mp4", basePath, name, name)
cmd := exec.Command("ffmpeg", "-f", "gif", "-i", uploadedFile, "-pix_fmt", "yuv420p", "-c:v", "libx264", "-movflags", "+faststart", "-filter:v", "crop='floor(in_w/2)*2:floor(in_h/2)*2'", savedMp4)
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
Log.Error("failed to make mp4 from gif ", err)
} else {
uploadedFile = strings.Split(uploadedFile, ".")[0] + ".mp4"
Log.Info("made mp4 from GIF")
}
} else { // if file not gif or img try to make it mp4
itsAVideo = true
Log.Info("ext: ", ext)
Log.Info("file.filename ", fullName)
if ext != "mp4" {
cmd, err := exec.Command("ffmpeg", "-i", uploadedFile, strings.Split(uploadedFile, ".")[0]+".mp4").CombinedOutput()
if err != nil {
Log.Error("failed to make a .any to .mp4 , ", err)
} else {
Log.Info("made a ", ext, " into .mp4 with cmd ", string(cmd))
err := os.Remove(uploadedFile)
if err != nil {
Log.Info("err removing original .mp4 as err: ", err)
} else {
uploadedFile = strings.Split(uploadedFile, ".")[0] + ".mp4"
Log.Info("deleted original at ext: ", ext)
}
}
}
}
// open finder
if of == "of" {
open.Run(framesDirPath)
}
if oo == "oo" {
open.Run(outputPath)
}
if itsAVideo {
// create frames from mp4
framesOut := fmt.Sprintf("%s/frames/%s/%s.png", basePath, name, "%d")
Log.Info("framesOut: ", framesOut)
cmd, err := exec.Command("ffmpeg", "-i", uploadedFile, "-vf", "fps="+fps, "-c:v", "png", framesOut).CombinedOutput()
if err != nil {
Log.Error("failed to make frames", err)
} else {
Log.Info("made frames from MP4 with cmd: ", string(cmd))
}
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
select {
case killSignal := <-interrupt:
Log.Info("Got signal:", killSignal)
if killSignal == os.Interrupt {
break
}
}
defer func() { //this way even on os.exit we still make our video (hacky, but then we can quit jobs early)
if !itsAVideo {return}//if it's not a video, don't make an output.mp4
// put frames together into an mp4 in videos dir
newVideo := fmt.Sprintf("%s/videos/%s", basePath, name+".mp4")
frames := fmt.Sprintf("%s/output/%s.png", framesDirPath, "%d")
Log.Info("frames to be turned into mp4 at: ", frames)
// framesDir := fmt.Sprintf("%s/output/%s.png", framesDirPath, "%d")
// ffmpeg -r 5 -f image2 -i '%d.png' -vcodec libx264 -crf 25 -pix_fmt yuv420p out.mp4
_, err := exec.Command("ffmpeg", "-r", fps, "-f", "image2", "-i", frames, "-vcodec", "libx264", "-crf", "25", "-pix_fmt", "yuv420p", newVideo).CombinedOutput()
if err != nil {
Log.Error("still failing to output a video meh, ", err)
} else {
Log.Info("\nmade mp4 from frames")
}
if ov == "ov" {
open.Run(basePath + "/videos")
}
// is there sound?
audio, err := exec.Command("ffprobe", uploadedFile, "-show_streams", "-select_streams", "a", "-loglevel", "error").CombinedOutput()
if err != nil {
Log.Error("Failed to test audio, ", err)
}
// add sound back in if there is any
// ffmpeg -i 2171447000212516064.mp4 -i gold.mp4 -map 0:v -map 1:a output.mp4
if len(audio) > 1 {
Log.Info("there's sound in this clip")
out, err := exec.Command("ffmpeg", "-y", "-i", newVideo, "-i", uploadedFile, "-map", "0:v", "-map", "1:a", basePath+"/videos/audio_"+name+".mp4").CombinedOutput()
if err != nil {
Log.Error("failed to add sound back", err)
} else {
Log.Info("fffmpeg added sound:", string(out))
if ovf == "ovf" {
open.Run(basePath + "/videos/audio_" + name + ".mp4")
}
// todo remove newVideo, so we only save one video, the one w/ audio
}
} else {
Log.Info("there's no sound")
if ovf == "ovf" {
open.Run(newVideo)
}
}
//stretch video enabled?
// ffmpeg -i input.mp4 -vf scale=ih*16/9:ih,scale=iw:-2,setsar=1 -crf 20 -c:a copy YT.mp4
// ffmpeg -i out.mp4 -vf scale=720x406,setdar=16:9 z.mp4
// http://www.bugcodemaster.com/article/changing-resolution-video-using-ffmpeg
// out, err := exec.Command("ffmpeg", "-y", "-i", newVideo, "-i", uploadedFile, "-map", "0:v", "-map", "1:a", basePath+"/videos/"+name+"_audio.mp4").CombinedOutput()
// if err != nil {
// Log.Error("failed to add sound back", err)
// } else {
// Log.Info("fffmpeg added sound:", string(out))
// os.Remove(newVideo) //remove video w/o sound, we don't need it
// if ovf == "ovf" {
// open.Run(basePath + "/videos/" + name + "_audio.mp4")
// }
// // todo remove newVideo, so we only save one w/ audio
// }
}()
}()
Log.Info("entering dreamer goroutine")
// deep dream the frames
cmd, err := exec.Command("python3", "folder.py", "--input", framesDirPath, "-it", it, "-oc", oc, "-la", la, "-rl", rl, "-rle", rle, "-li", li, "-iw", iw, "-ow", ow).CombinedOutput()
if err != nil {
Log.WithFields(logrus.Fields{
"event": "folder.py",
}).Error("failed to dream", err)
c.String(200, "Abort, this app is crashing, can't dream")
}
Log.Info("done w/ dream loop, python said: ", string(cmd))
}