Cleanup error messages
This commit is contained in:
parent
217b406617
commit
663a405ca6
5 changed files with 24 additions and 43 deletions
12
camera.go
12
camera.go
|
@ -1,7 +1,7 @@
|
||||||
package vidio
|
package vidio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -70,11 +70,9 @@ func getDevicesWindows() ([]string, error) {
|
||||||
)
|
)
|
||||||
pipe, err := cmd.StderrPipe()
|
pipe, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Read list devices from Stdout.
|
// Read list devices from Stdout.
|
||||||
|
@ -110,12 +108,10 @@ func getCameraData(device string, camera *Camera) error {
|
||||||
// it will write the meta data to Stderr.
|
// it will write the meta data to Stderr.
|
||||||
pipe, err := cmd.StderrPipe()
|
pipe, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Start the command.
|
// Start the command.
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Read ffmpeg output from Stdout.
|
// Read ffmpeg output from Stdout.
|
||||||
|
@ -156,11 +152,11 @@ func NewCamera(stream int) (*Camera, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if stream >= len(devices) {
|
if stream >= len(devices) {
|
||||||
return nil, errors.New("Could not find device with index: " + strconv.Itoa(stream))
|
return nil, fmt.Errorf("could not find device with index: %d", stream)
|
||||||
}
|
}
|
||||||
device = "video=" + devices[stream]
|
device = "video=" + devices[stream]
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Unsupported OS: " + runtime.GOOS)
|
return nil, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
camera := Camera{name: device, depth: 3}
|
camera := Camera{name: device, depth: 3}
|
||||||
|
@ -196,13 +192,11 @@ func initCamera(camera *Camera) error {
|
||||||
camera.cmd = cmd
|
camera.cmd = cmd
|
||||||
pipe, err := cmd.StdoutPipe()
|
pipe, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
camera.pipe = &pipe
|
camera.pipe = &pipe
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
imageio.go
20
imageio.go
|
@ -1,7 +1,6 @@
|
||||||
package vidio
|
package vidio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
|
@ -9,9 +8,7 @@ import (
|
||||||
|
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
_ "image/jpeg"
|
|
||||||
"image/png"
|
"image/png"
|
||||||
_ "image/png"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reads an image from a file. Currently only supports png and jpeg.
|
// Reads an image from a file. Currently only supports png and jpeg.
|
||||||
|
@ -33,12 +30,11 @@ func Read(filename string, buffer ...[]byte) (int, int, []byte, error) {
|
||||||
var data []byte
|
var data []byte
|
||||||
if len(buffer) > 0 {
|
if len(buffer) > 0 {
|
||||||
if len(buffer[0]) < size {
|
if len(buffer[0]) < size {
|
||||||
errmsg := fmt.Sprintf("Buffer size (%d) is smaller than image size (%d)", len(buffer[0]), size)
|
return 0, 0, nil, fmt.Errorf("buffer size (%d) is smaller than image size (%d)", len(buffer[0]), size)
|
||||||
return 0, 0, nil, errors.New(errmsg)
|
|
||||||
}
|
}
|
||||||
data = buffer[0]
|
data = buffer[0]
|
||||||
} else {
|
} else {
|
||||||
data = make([]byte, size, size)
|
data = make([]byte, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
index := 0
|
index := 0
|
||||||
|
@ -46,12 +42,10 @@ func Read(filename string, buffer ...[]byte) (int, int, []byte, error) {
|
||||||
for w := 0; w < bounds.X; w++ {
|
for w := 0; w < bounds.X; w++ {
|
||||||
r, g, b, _ := image.At(w, h).RGBA()
|
r, g, b, _ := image.At(w, h).RGBA()
|
||||||
r, g, b = r/256, g/256, b/256
|
r, g, b = r/256, g/256, b/256
|
||||||
data[index] = byte(r)
|
data[index+0] = byte(r)
|
||||||
index++
|
data[index+1] = byte(g)
|
||||||
data[index] = byte(g)
|
data[index+2] = byte(b)
|
||||||
index++
|
index += 3
|
||||||
data[index] = byte(b)
|
|
||||||
index++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bounds.X, bounds.Y, data, nil
|
return bounds.X, bounds.Y, data, nil
|
||||||
|
@ -84,6 +78,8 @@ func Write(filename string, width, height int, buffer []byte) error {
|
||||||
if err := jpeg.Encode(f, image, nil); err != nil {
|
if err := jpeg.Encode(f, image, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
22
utils.go
22
utils.go
|
@ -2,6 +2,7 @@ package vidio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -27,13 +28,12 @@ func exists(filename string) bool {
|
||||||
// Checks if the given program is installed.
|
// Checks if the given program is installed.
|
||||||
func checkExists(program string) error {
|
func checkExists(program string) error {
|
||||||
cmd := exec.Command(program, "-version")
|
cmd := exec.Command(program, "-version")
|
||||||
|
errmsg := fmt.Errorf("%s is not installed", program)
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
return errmsg
|
||||||
return errors.New(program + " is not installed.")
|
|
||||||
}
|
}
|
||||||
if err := cmd.Wait(); err != nil {
|
if err := cmd.Wait(); err != nil {
|
||||||
cmd.Process.Kill()
|
return errmsg
|
||||||
return errors.New(program + " is not installed.")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -53,12 +53,10 @@ func ffprobe(filename, stype string) (map[string]string, error) {
|
||||||
|
|
||||||
pipe, err := cmd.StdoutPipe()
|
pipe, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Read ffprobe output from Stdout.
|
// Read ffprobe output from Stdout.
|
||||||
|
@ -73,7 +71,6 @@ func ffprobe(filename, stype string) (map[string]string, error) {
|
||||||
}
|
}
|
||||||
// Wait for ffprobe command to complete.
|
// Wait for ffprobe command to complete.
|
||||||
if err := cmd.Wait(); err != nil {
|
if err := cmd.Wait(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,8 +135,7 @@ func parse(data string) float64 {
|
||||||
|
|
||||||
// Returns the webcam name used for the -f option with ffmpeg.
|
// Returns the webcam name used for the -f option with ffmpeg.
|
||||||
func webcam() (string, error) {
|
func webcam() (string, error) {
|
||||||
os := runtime.GOOS
|
switch runtime.GOOS {
|
||||||
switch os {
|
|
||||||
case "linux":
|
case "linux":
|
||||||
return "v4l2", nil
|
return "v4l2", nil
|
||||||
case "darwin":
|
case "darwin":
|
||||||
|
@ -147,7 +143,7 @@ func webcam() (string, error) {
|
||||||
case "windows":
|
case "windows":
|
||||||
return "dshow", nil // vfwcap
|
return "dshow", nil // vfwcap
|
||||||
default:
|
default:
|
||||||
return "", errors.New("Unsupported OS: " + os)
|
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +217,7 @@ func parseWebcamData(buffer []byte, camera *Camera) {
|
||||||
}
|
}
|
||||||
bufferstr = bufferstr[index:]
|
bufferstr = bufferstr[index:]
|
||||||
// Dimensions. widthxheight.
|
// Dimensions. widthxheight.
|
||||||
regex := regexp.MustCompile("\\d{2,}x\\d{2,}")
|
regex := regexp.MustCompile(`\d{2,}x\d{2,}`)
|
||||||
match := regex.FindString(bufferstr)
|
match := regex.FindString(bufferstr)
|
||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
split := strings.Split(match, "x")
|
split := strings.Split(match, "x")
|
||||||
|
@ -229,7 +225,7 @@ func parseWebcamData(buffer []byte, camera *Camera) {
|
||||||
camera.height = int(parse(split[1]))
|
camera.height = int(parse(split[1]))
|
||||||
}
|
}
|
||||||
// FPS.
|
// FPS.
|
||||||
regex = regexp.MustCompile("\\d+(.\\d+)? fps")
|
regex = regexp.MustCompile(`\d+(.\d+)? fps`)
|
||||||
match = regex.FindString(bufferstr)
|
match = regex.FindString(bufferstr)
|
||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
index = strings.Index(match, " fps")
|
index = strings.Index(match, " fps")
|
||||||
|
@ -242,7 +238,7 @@ func parseWebcamData(buffer []byte, camera *Camera) {
|
||||||
regex = regexp.MustCompile("Video: .+,")
|
regex = regexp.MustCompile("Video: .+,")
|
||||||
match = regex.FindString(bufferstr)
|
match = regex.FindString(bufferstr)
|
||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
match = match[7:]
|
match = match[len("Video: "):]
|
||||||
index = strings.Index(match, "(")
|
index = strings.Index(match, "(")
|
||||||
if index != -1 {
|
if index != -1 {
|
||||||
match = match[:index]
|
match = match[:index]
|
||||||
|
|
6
video.go
6
video.go
|
@ -1,7 +1,7 @@
|
||||||
package vidio
|
package vidio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -87,7 +87,7 @@ func (video *Video) SetFrameBuffer(buffer []byte) {
|
||||||
// Uses ffprobe to get video information and fills in the Video struct with this data.
|
// Uses ffprobe to get video information and fills in the Video struct with this data.
|
||||||
func NewVideo(filename string) (*Video, error) {
|
func NewVideo(filename string) (*Video, error) {
|
||||||
if !exists(filename) {
|
if !exists(filename) {
|
||||||
return nil, errors.New("Video file " + filename + " does not exist")
|
return nil, fmt.Errorf("video file %s does not exist", filename)
|
||||||
}
|
}
|
||||||
// Check if ffmpeg and ffprobe are installed on the users machine.
|
// Check if ffmpeg and ffprobe are installed on the users machine.
|
||||||
if err := checkExists("ffmpeg"); err != nil {
|
if err := checkExists("ffmpeg"); err != nil {
|
||||||
|
@ -134,12 +134,10 @@ func initVideo(video *Video) error {
|
||||||
video.cmd = cmd
|
video.cmd = cmd
|
||||||
pipe, err := cmd.StdoutPipe()
|
pipe, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
video.pipe = &pipe
|
video.pipe = &pipe
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package vidio
|
package vidio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
@ -142,14 +141,14 @@ func NewVideoWriter(filename string, width, height int, options *Options) (*Vide
|
||||||
|
|
||||||
if options.Audio != "" {
|
if options.Audio != "" {
|
||||||
if !exists(options.Audio) {
|
if !exists(options.Audio) {
|
||||||
return nil, errors.New("Audio file " + options.Audio + " does not exist.")
|
return nil, fmt.Errorf("audio file %s does not exist", options.Audio)
|
||||||
}
|
}
|
||||||
|
|
||||||
audioData, err := ffprobe(options.Audio, "a")
|
audioData, err := ffprobe(options.Audio, "a")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if len(audioData) == 0 {
|
} else if len(audioData) == 0 {
|
||||||
return nil, errors.New("Given \"audio\" file " + options.Audio + " has no audio.")
|
return nil, fmt.Errorf("given audio file %s has no audio", options.Audio)
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.audio = options.Audio
|
writer.audio = options.Audio
|
||||||
|
@ -258,12 +257,10 @@ func initVideoWriter(writer *VideoWriter) error {
|
||||||
|
|
||||||
pipe, err := cmd.StdinPipe()
|
pipe, err := cmd.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipe.Close()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
writer.pipe = &pipe
|
writer.pipe = &pipe
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
cmd.Process.Kill()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue