Add example to README, copy buffer in Write
This commit is contained in:
parent
f1c7d31a29
commit
68fe8618c6
2 changed files with 20 additions and 17 deletions
17
README.md
17
README.md
|
@ -167,6 +167,23 @@ for i := 1; i <= 10; i++ {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Write all frames of `video.mp4` as `jpg` images.
|
||||||
|
|
||||||
|
```go
|
||||||
|
video, _ := vidio.NewVideo("video.mp4")
|
||||||
|
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
|
||||||
|
video.SetFrameBuffer(img.Pix)
|
||||||
|
|
||||||
|
frame := 0
|
||||||
|
for video.Read() {
|
||||||
|
f, _ := os.Create(fmt.Sprintf("%d.jpg", frame))
|
||||||
|
jpeg.Encode(f, img, nil)
|
||||||
|
f.Close()
|
||||||
|
frame++
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# Acknowledgements
|
# Acknowledgements
|
||||||
|
|
||||||
* 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.
|
* 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.
|
||||||
|
|
20
imageio.go
20
imageio.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"image/color"
|
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
)
|
)
|
||||||
|
@ -61,27 +60,14 @@ func Write(filename string, width, height int, buffer []byte) error {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
image := image.NewRGBA(image.Rect(0, 0, width, height))
|
image := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||||
index := 0
|
copy(image.Pix, buffer)
|
||||||
for h := 0; h < height; h++ {
|
|
||||||
for w := 0; w < width; w++ {
|
|
||||||
r, g, b := buffer[index+0], buffer[index+1], buffer[index+2]
|
|
||||||
image.Set(w, h, color.RGBA{r, g, b, 255})
|
|
||||||
index += 4
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch filepath.Ext(filename) {
|
switch filepath.Ext(filename) {
|
||||||
case ".png":
|
case ".png":
|
||||||
if err := png.Encode(f, image); err != nil {
|
return png.Encode(f, image)
|
||||||
return err
|
|
||||||
}
|
|
||||||
case ".jpg", ".jpeg":
|
case ".jpg", ".jpeg":
|
||||||
if err := jpeg.Encode(f, image, nil); err != nil {
|
return jpeg.Encode(f, image, nil)
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(filename))
|
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue