-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.go
129 lines (103 loc) · 3.96 KB
/
main.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
package main
import (
"fmt"
"image"
"log"
"strings"
"github.com/asticode/go-astiav"
)
func main() {
// Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(c astiav.Classer, l astiav.LogLevel, fmt, msg string) {
var cs string
if c != nil {
if cl := c.Class(); cl != nil {
cs = " - class: " + cl.String()
}
}
log.Printf("ffmpeg log: %s%s - level: %d\n", strings.TrimSpace(msg), cs, l)
})
/*
In this first part we're going to manipulate an audio frame
*/
// Allocate frame
audioFrame := astiav.AllocFrame()
defer audioFrame.Free()
// To write data manually into a frame, proper attributes need to be set and allocated
audioFrame.SetChannelLayout(astiav.ChannelLayoutStereo)
audioFrame.SetNbSamples(960)
audioFrame.SetSampleFormat(astiav.SampleFormatFlt)
audioFrame.SetSampleRate(48000)
// Allocate buffer
align := 0
if err := audioFrame.AllocBuffer(align); err != nil {
log.Fatal(fmt.Errorf("main: allocating buffer failed: %w", err))
}
// Allocate samples
if err := audioFrame.AllocSamples(align); err != nil {
log.Fatal(fmt.Errorf("main: allocating image failed: %w", err))
}
// When writing data manually into a frame, you need to make sure the frame is writable
if err := audioFrame.MakeWritable(); err != nil {
log.Fatal(fmt.Errorf("main: making frame writable failed: %w", err))
}
// Let's say b1 contains an actual audio buffer, we can update the audio frame's data based on the buffer
var b1 []byte
if err := audioFrame.Data().SetBytes(b1, align); err != nil {
log.Fatal(fmt.Errorf("main: setting frame's data based from bytes failed: %w", err))
}
// We can also retrieve the audio frame's data as buffer
if _, err := audioFrame.Data().Bytes(align); err != nil {
log.Fatal(fmt.Errorf("main: getting frame's data as bytes failed: %w", err))
}
/*
In this second part we're going to manipulate a video frame
*/
// Allocate frame
videoFrame := astiav.AllocFrame()
defer videoFrame.Free()
// To write data manually into a frame, proper attributes need to be set and allocated
videoFrame.SetHeight(256)
videoFrame.SetPixelFormat(astiav.PixelFormatRgba)
videoFrame.SetWidth(256)
// Allocate buffer
align = 1
if err := videoFrame.AllocBuffer(align); err != nil {
log.Fatal(fmt.Errorf("main: allocating buffer failed: %w", err))
}
// Allocate image
if err := videoFrame.AllocImage(align); err != nil {
log.Fatal(fmt.Errorf("main: allocating image failed: %w", err))
}
// When writing data manually into a frame, you need to make sure the frame is writable
if err := videoFrame.MakeWritable(); err != nil {
log.Fatal(fmt.Errorf("main: making frame writable failed: %w", err))
}
// Let's say b2 contains an actual video buffer, we can update the video frame's data based on the buffer
var b2 []byte
if err := videoFrame.Data().SetBytes(b2, align); err != nil {
log.Fatal(fmt.Errorf("main: setting frame's data based from bytes failed: %w", err))
}
// We can also retrieve the video frame's data as buffer
if _, err := videoFrame.Data().Bytes(align); err != nil {
log.Fatal(fmt.Errorf("main: getting frame's data as bytes failed: %w", err))
}
// Let's say i1 is an actual Go image.Image, we can update the video frame's data based on the image
var i1 image.Image
if err := videoFrame.Data().FromImage(i1); err != nil {
log.Fatal(fmt.Errorf("main: setting frame's data based on Go image failed: %w", err))
}
// We can also retrieve the video frame's data as a Go image
// For that we first need to guess the Go image format based on the frame's attributes before providing
// it to .ToImage(). You may not need this and can provide your own image.Image to .ToImage()
i2, err := videoFrame.Data().GuessImageFormat()
if err != nil {
log.Fatal(fmt.Errorf("main: guessing image format failed: %w", err))
}
if err := videoFrame.Data().ToImage(i2); err != nil {
log.Fatal(fmt.Errorf("main: getting frame's data as Go image failed: %w", err))
}
// Success
log.Println("success")
}