-
Notifications
You must be signed in to change notification settings - Fork 0
/
step1_readVideo.go
83 lines (66 loc) · 1.85 KB
/
step1_readVideo.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
package main
import (
"github.com/AlexEidt/Vidio"
"github.com/AlexEidt/aio"
"image"
"path/filepath"
"time"
"fmt"
)
var (
fps float64
)
// ---------------- Step Function ----------------
func initalResize() {
fmt.Printf("Initial resize to %dx%dpx\n", screenWidth, squareSize)
err := resizeTiny(inputVideo, filepath.Join(tempDirTinyVid,getBasenameWithoutExt(inputVideo)+".mp4"), screenWidth, squareSize)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Resize completed, Running algorithm...")
}
func readVideo() {
video, _ := vidio.NewVideo(filepath.Join(tempDirTinyVid,getBasenameWithoutExt(inputVideo)+".mp4"))
vbuffer := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
video.SetFrameBuffer(vbuffer.Pix)
audio, _ := aio.NewAudio(filepath.Join(tempDirTinyVid,getBasenameWithoutExt(inputVideo)+".mp4"), nil)
player, _ := aio.NewPlayer(audio.Channels(), audio.SampleRate(), audio.Format())
defer player.Close()
fps = video.FPS()
maxFrameDuration := time.Second / time.Duration(fps)
if realTime {
go func(){
for audio.Read() {
player.Play(audio.Buffer())
}
}()
} else {
player.Close()
}
for video.Read() {
startTime := time.Now()
if drawUI && !playOnlyMode {
inputFrameRW.Lock()
inputFrame = vbuffer
inputFrameRW.Unlock()
}
procFrameQueue1 <- vbuffer
elapsedTime := time.Since(startTime)
remainingTime := maxFrameDuration - elapsedTime
if remainingTime > 0 && realTime {
// 2% speed up
time.Sleep(time.Duration(int64(float64(remainingTime) * 0.98)))
}
}
video.Close()
}
// ---------------- Helper Functions ----------------
func countFrames(videoFile string) (int, error) {
// Open the video file
video, err := vidio.NewVideo(videoFile)
if err != nil {
return 0, err
}
defer video.Close()
return video.Frames(), nil
}