Cleanup error messages

This commit is contained in:
Alex Eidt 2022-07-20 13:43:02 -07:00
parent 217b406617
commit 663a405ca6
5 changed files with 24 additions and 43 deletions

View file

@ -1,7 +1,7 @@
package vidio
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
@ -70,11 +70,9 @@ func getDevicesWindows() ([]string, error) {
)
pipe, err := cmd.StderrPipe()
if err != nil {
pipe.Close()
return nil, err
}
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return nil, err
}
// Read list devices from Stdout.
@ -110,12 +108,10 @@ func getCameraData(device string, camera *Camera) error {
// it will write the meta data to Stderr.
pipe, err := cmd.StderrPipe()
if err != nil {
pipe.Close()
return err
}
// Start the command.
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return err
}
// Read ffmpeg output from Stdout.
@ -156,11 +152,11 @@ func NewCamera(stream int) (*Camera, error) {
return nil, err
}
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]
default:
return nil, errors.New("Unsupported OS: " + runtime.GOOS)
return nil, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
camera := Camera{name: device, depth: 3}
@ -196,13 +192,11 @@ func initCamera(camera *Camera) error {
camera.cmd = cmd
pipe, err := cmd.StdoutPipe()
if err != nil {
pipe.Close()
return err
}
camera.pipe = &pipe
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return err
}

View file

@ -1,7 +1,6 @@
package vidio
import (
"errors"
"fmt"
"image"
"os"
@ -9,9 +8,7 @@ import (
"image/color"
"image/jpeg"
_ "image/jpeg"
"image/png"
_ "image/png"
)
// 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
if len(buffer) > 0 {
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, errors.New(errmsg)
return 0, 0, nil, fmt.Errorf("buffer size (%d) is smaller than image size (%d)", len(buffer[0]), size)
}
data = buffer[0]
} else {
data = make([]byte, size, size)
data = make([]byte, size)
}
index := 0
@ -46,12 +42,10 @@ func Read(filename string, buffer ...[]byte) (int, int, []byte, error) {
for w := 0; w < bounds.X; w++ {
r, g, b, _ := image.At(w, h).RGBA()
r, g, b = r/256, g/256, b/256
data[index] = byte(r)
index++
data[index] = byte(g)
index++
data[index] = byte(b)
index++
data[index+0] = byte(r)
data[index+1] = byte(g)
data[index+2] = byte(b)
index += 3
}
}
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 {
return err
}
default:
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(filename))
}
return nil

View file

@ -2,6 +2,7 @@ package vidio
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
@ -27,13 +28,12 @@ func exists(filename string) bool {
// Checks if the given program is installed.
func checkExists(program string) error {
cmd := exec.Command(program, "-version")
errmsg := fmt.Errorf("%s is not installed", program)
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return errors.New(program + " is not installed.")
return errmsg
}
if err := cmd.Wait(); err != nil {
cmd.Process.Kill()
return errors.New(program + " is not installed.")
return errmsg
}
return nil
}
@ -53,12 +53,10 @@ func ffprobe(filename, stype string) (map[string]string, error) {
pipe, err := cmd.StdoutPipe()
if err != nil {
pipe.Close()
return nil, err
}
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return nil, err
}
// Read ffprobe output from Stdout.
@ -73,7 +71,6 @@ func ffprobe(filename, stype string) (map[string]string, error) {
}
// Wait for ffprobe command to complete.
if err := cmd.Wait(); err != nil {
cmd.Process.Kill()
return nil, err
}
@ -138,8 +135,7 @@ func parse(data string) float64 {
// Returns the webcam name used for the -f option with ffmpeg.
func webcam() (string, error) {
os := runtime.GOOS
switch os {
switch runtime.GOOS {
case "linux":
return "v4l2", nil
case "darwin":
@ -147,7 +143,7 @@ func webcam() (string, error) {
case "windows":
return "dshow", nil // vfwcap
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:]
// Dimensions. widthxheight.
regex := regexp.MustCompile("\\d{2,}x\\d{2,}")
regex := regexp.MustCompile(`\d{2,}x\d{2,}`)
match := regex.FindString(bufferstr)
if len(match) > 0 {
split := strings.Split(match, "x")
@ -229,7 +225,7 @@ func parseWebcamData(buffer []byte, camera *Camera) {
camera.height = int(parse(split[1]))
}
// FPS.
regex = regexp.MustCompile("\\d+(.\\d+)? fps")
regex = regexp.MustCompile(`\d+(.\d+)? fps`)
match = regex.FindString(bufferstr)
if len(match) > 0 {
index = strings.Index(match, " fps")
@ -242,7 +238,7 @@ func parseWebcamData(buffer []byte, camera *Camera) {
regex = regexp.MustCompile("Video: .+,")
match = regex.FindString(bufferstr)
if len(match) > 0 {
match = match[7:]
match = match[len("Video: "):]
index = strings.Index(match, "(")
if index != -1 {
match = match[:index]

View file

@ -1,7 +1,7 @@
package vidio
import (
"errors"
"fmt"
"io"
"os"
"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.
func NewVideo(filename string) (*Video, error) {
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.
if err := checkExists("ffmpeg"); err != nil {
@ -134,12 +134,10 @@ func initVideo(video *Video) error {
video.cmd = cmd
pipe, err := cmd.StdoutPipe()
if err != nil {
pipe.Close()
return err
}
video.pipe = &pipe
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return err
}

View file

@ -1,7 +1,6 @@
package vidio
import (
"errors"
"fmt"
"io"
"math"
@ -142,14 +141,14 @@ func NewVideoWriter(filename string, width, height int, options *Options) (*Vide
if 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")
if err != nil {
return nil, err
} 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
@ -258,12 +257,10 @@ func initVideoWriter(writer *VideoWriter) error {
pipe, err := cmd.StdinPipe()
if err != nil {
pipe.Close()
return err
}
writer.pipe = &pipe
if err := cmd.Start(); err != nil {
cmd.Process.Kill()
return err
}