-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframes.go
More file actions
127 lines (111 loc) · 4.03 KB
/
frames.go
File metadata and controls
127 lines (111 loc) · 4.03 KB
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
package main
import (
"fmt"
"image"
"image/draw"
"image/jpeg"
"image/png"
"os"
"sync"
ffmpeg "github.com/adefreitas/go-fluent-ffmpeg"
)
func extractFrame(framePath string) image.Image {
// Extrated from https://stackoverflow.com/a/49595770
existingImageFile, err := os.Open(framePath)
if err != nil {
fmt.Printf("Error opening image %w\n"+framePath, err)
}
defer existingImageFile.Close()
_, _, decodingError := image.Decode(existingImageFile)
if decodingError != nil {
fmt.Printf("Error decoding image %w\n"+framePath, decodingError)
}
// fmt.Println(imageData)
// fmt.Println(imageType)
existingImageFile.Seek(0, 0)
loadedImage, err := png.Decode(existingImageFile)
if err != nil {
fmt.Printf("Error decoding png image %w\n"+framePath, err)
}
// fmt.Println(loadedImage)
return loadedImage
}
func extractBackground() image.Image {
return extractFrame(INPUT_DIR + "/background.png")
}
func combineAttributesForFrame(frames Frames, prefix int, frameNumber int, wg *sync.WaitGroup) {
// defer wg.Done()
generatedFrames := make([]image.Image, 0)
generatedFrames = append(generatedFrames, extractFrame(frames.auras[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.watchers[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.gems[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.stairs[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.blips[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.blipAura[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.arches[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.handTopLeft[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.handTopRight[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.handBottomLeft[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.handBottomRight[frameNumber]))
generatedFrames = append(generatedFrames, extractFrame(frames.elements[frameNumber]))
bgImage := image.NewRGBA(image.Rect(0, 0, 1080, 1920))
for _, img := range generatedFrames {
draw.Draw(bgImage, img.Bounds(), img, image.ZP, draw.Over)
}
path := fmt.Sprintf("%s/raw/%d/%d_%d.jpeg", OUTPUT_FRAMES_DIR, prefix, prefix, frameNumber)
out, err := os.Create(path)
if err != nil {
fmt.Printf("Error creating image file: %s\n", path)
}
err = jpeg.Encode(out, bgImage, &jpeg.Options{Quality: 60})
if err != nil {
fmt.Printf("Error creating image file: %+v\n", err)
return
}
}
func combineAttributes(frames Frames, audioInputPath string, prefix int) {
fmt.Println("Generating frames for asset", prefix)
var wg sync.WaitGroup
paralelization := 10
wg.Add(paralelization)
c := make(chan int)
lo, hi := 0, 199
// Creating an array from 0 to 200 for paralelization
frameNumbers := make([]int, hi-lo+1)
for i := range frameNumbers {
frameNumbers[i] = i + lo
}
// List creating ends
for i := 0; i < paralelization; i++ {
go func(c chan int) {
for {
v, more := <-c
if more == false {
wg.Done()
return
}
combineAttributesForFrame(frames, prefix, v, &wg)
}
}(c)
}
// Adding frame numbers to the channel to be consumed by the loop above
for _, a := range frameNumbers {
c <- a
}
// closing channel
close(c)
fmt.Println("Waiting for paralel frame creation to finish for asset", prefix)
wg.Wait()
fmt.Println("Paralel frame creation done for asset", prefix)
fmt.Println("Generating video", prefix, audioInputPath)
fileExtension := "%01d.jpeg"
framesInputPath := fmt.Sprintf("%s/raw/%d/%d_%s", OUTPUT_FRAMES_DIR, prefix, prefix, fileExtension)
// audioInputPath := fmt.Sprintf("%s/bliptunes.mp3", INPUT_AUDIO_DIR)
outputVideoPath := fmt.Sprintf("%s/%d/%d_output.webm", OUTPUT_VIDEO_DIR, prefix, prefix)
ffmpeg.NewCommand("").
Input(framesInputPath, nil, "", false).
Input(audioInputPath, nil, "", false).
OutputPath(outputVideoPath).
Run()
fmt.Println("Video generation finished for asset", prefix)
}