Skip to content

Commit 68fe861

Browse files
committed
Add example to README, copy buffer in Write
1 parent f1c7d31 commit 68fe861

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ for i := 1; i <= 10; i++ {
167167
}
168168
```
169169

170+
Write all frames of `video.mp4` as `jpg` images.
171+
172+
```go
173+
video, _ := vidio.NewVideo("video.mp4")
174+
175+
img := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
176+
video.SetFrameBuffer(img.Pix)
177+
178+
frame := 0
179+
for video.Read() {
180+
f, _ := os.Create(fmt.Sprintf("%d.jpg", frame))
181+
jpeg.Encode(f, img, nil)
182+
f.Close()
183+
frame++
184+
}
185+
```
186+
170187
# Acknowledgements
171188

172189
* Special thanks to [Zulko](http://zulko.github.io/) and his [blog post](http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/) about using FFmpeg to process video.

imageio.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"path/filepath"
88

9-
"image/color"
109
"image/jpeg"
1110
"image/png"
1211
)
@@ -61,27 +60,14 @@ func Write(filename string, width, height int, buffer []byte) error {
6160
defer f.Close()
6261

6362
image := image.NewRGBA(image.Rect(0, 0, width, height))
64-
index := 0
65-
for h := 0; h < height; h++ {
66-
for w := 0; w < width; w++ {
67-
r, g, b := buffer[index+0], buffer[index+1], buffer[index+2]
68-
image.Set(w, h, color.RGBA{r, g, b, 255})
69-
index += 4
70-
}
71-
}
63+
copy(image.Pix, buffer)
7264

7365
switch filepath.Ext(filename) {
7466
case ".png":
75-
if err := png.Encode(f, image); err != nil {
76-
return err
77-
}
67+
return png.Encode(f, image)
7868
case ".jpg", ".jpeg":
79-
if err := jpeg.Encode(f, image, nil); err != nil {
80-
return err
81-
}
69+
return jpeg.Encode(f, image, nil)
8270
default:
8371
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(filename))
8472
}
85-
86-
return nil
8773
}

0 commit comments

Comments
 (0)