add video encoding. broken for now

This commit is contained in:
Dmitry Fedotov
2025-03-26 00:51:46 +03:00
parent c3b2036713
commit d72540d0c3
5 changed files with 79 additions and 22 deletions

2
go.mod
View File

@@ -2,4 +2,4 @@ module code.uint32.ru/dmitry/barnsley-fern-go
go 1.24.1
require github.com/AlexEidt/Vidio v1.5.1 // indirect
require github.com/AlexEidt/Vidio v1.5.1

View File

@@ -8,7 +8,7 @@ import (
"io"
)
var erreEcnodePng = errors.New("не удалось создать png изображение")
var errEncodePng = errors.New("не удалось создать png изображение")
func createBarnsleyFernPng(s *settings, w io.Writer) error {
img := newImageWithBackGround(s.X, s.Y, s.BG)
@@ -16,7 +16,7 @@ func createBarnsleyFernPng(s *settings, w io.Writer) error {
drawBarnsleyFern(img, newColorFunc(s.CM), s.Dots)
if err := png.Encode(w, img); err != nil {
return errors.Join(erreEcnodePng, err)
return errors.Join(errEncodePng, err)
}
return nil

21
main.go
View File

@@ -2,13 +2,26 @@ package main
import (
"fmt"
"io"
"os"
)
func main() {
filename, settings := parseSettings()
s := parseSettings()
file, err := os.Create(filename)
var processor func(*settings, io.Writer) error
switch s.OM {
case outputpng:
processor = createBarnsleyFernPng
case outputvideo:
processor = createBarnsLeyFernVideo
default:
fmt.Println("неизвестный тип вывода:", s.OM)
return
}
file, err := os.Create(s.Fname)
if err != nil {
fmt.Println("ошибка открытия файла", err)
return
@@ -20,8 +33,8 @@ func main() {
}
}()
if err := createBarnsleyFernPng(settings, file); err != nil {
fmt.Println("ошибка создания изображения:", err)
if err := processor(s, file); err != nil {
fmt.Println("ошибка создания объекта:", err)
return
}

View File

@@ -22,35 +22,41 @@ const (
)
type settings struct {
X, Y int // image size
Dots int // dots to draw for single image
BG color.Color // background color
CM colormode // settings for colorFunc
X, Y int // image size
Dots int // dots to draw for single image
BG color.Color // background color
CM colormode // settings for colorFunc
OM outputmode // what to output
Fname string // filename for output
}
func parseSettings() (string, *settings) {
func parseSettings() *settings {
var (
x, y int
dots int
cmode int
fname string
x, y int
dots int
cmode int
output int
fname string
)
flag.IntVar(&x, "x", 1920, "размер картинки по горизонтали")
flag.IntVar(&y, "y", 1080, "размер картинки по вертикали")
flag.IntVar(&dots, "d", 100000, "сколько точек рисовать")
flag.IntVar(&cmode, "color", 0, "color mode: 0 - default green, 1 - random, 2 - timed, 3 - rainbow")
flag.IntVar(&output, "o", 0, "режим вывода: 0 - изображение png, 1 - видео")
flag.StringVar(&fname, "out", "barnsley-fern.png", "полный путь файла для записи изображения")
flag.Parse()
s := &settings{
X: x,
Y: y,
Dots: dots,
BG: color.White,
CM: colormode(cmode),
X: x,
Y: y,
Dots: dots,
BG: color.White,
CM: colormode(cmode),
OM: outputmode(output),
Fname: fname,
}
return fname, s
return s
}

38
video.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"bytes"
"errors"
"io"
vid "github.com/AlexEidt/Vidio"
)
var erreEncodeVideo = errors.New("не удалось создать видео")
func createBarnsLeyFernVideo(s *settings, w io.Writer) error {
opts := &vid.Options{
FPS: 1,
Quality: 0,
Delay: 1000,
}
vw, err := vid.NewVideoWriter(s.Fname, s.X, s.Y, opts)
if err != nil {
return errors.Join(erreEncodeVideo, err)
}
frames := 100
for range frames {
imgbuf := new(bytes.Buffer)
if err := createBarnsleyFernPng(s, imgbuf); err != nil {
return errors.Join(erreEncodeVideo, err)
}
if err := vw.Write(imgbuf.Bytes()); err != nil {
return errors.Join(erreEncodeVideo, err)
}
}
return nil
}