summaryrefslogtreecommitdiff
path: root/media
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-22 18:01:27 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-22 18:01:27 +1000
commita90739089110e9f92653792bb1f23888ad734a79 (patch)
treecf7b364140e560d4e062607e1ec71567cf843360 /media
parentce3008222e924902352d697b45626aebd8980af1 (diff)
Add support for transcoding
Diffstat (limited to 'media')
-rw-r--r--media/utils.go56
-rw-r--r--media/video.go6
2 files changed, 4 insertions, 58 deletions
diff --git a/media/utils.go b/media/utils.go
deleted file mode 100644
index b67f276..0000000
--- a/media/utils.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package media
-
-import (
- "bytes"
- "fmt"
- "os/exec"
- "time"
-)
-
-func cmdExists(cmd string) bool {
- _, err := exec.LookPath(cmd)
- return err == nil
-}
-
-func runCmd(timeout int, command string, args ...string) error {
- // instantiate new command
- cmd := exec.Command(command, args...)
-
- // get pipe to standard output
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- return fmt.Errorf("cmd.StdoutPipe() error: %w", err)
- }
-
- // start process via command
- if err := cmd.Start(); err != nil {
- return fmt.Errorf("cmd.Start() error: %w", err)
- }
-
- // setup a buffer to capture standard output
- var buf bytes.Buffer
-
- // create a channel to capture any errors from wait
- done := make(chan error)
- go func() {
- if _, err := buf.ReadFrom(stdout); err != nil {
- panic("buf.Read(stdout) error: " + err.Error())
- }
- done <- cmd.Wait()
- }()
-
- // block on select, and switch based on actions received
- select {
- case <-time.After(time.Duration(timeout) * time.Second):
- if err := cmd.Process.Kill(); err != nil {
- return fmt.Errorf("failed to kill: %w", err)
- }
- return fmt.Errorf("timeout reached, process killed")
- case err := <-done:
- if err != nil {
- close(done)
- return fmt.Errorf("process done, with error: %w", err)
- }
- return nil
- }
-}
diff --git a/media/video.go b/media/video.go
index 9c10b1e..6e88a8c 100644
--- a/media/video.go
+++ b/media/video.go
@@ -10,6 +10,8 @@ import (
"time"
"github.com/dhowden/tag"
+
+ "github.com/wybiral/tube/utils"
)
// Video represents metadata for a single video.
@@ -75,8 +77,8 @@ func ParseVideo(p *Path, name string) (*Video, error) {
if pic != nil {
v.Thumb = pic.Data
v.ThumbType = pic.MIMEType
- } else if cmdExists("mt") {
- if err := runCmd(3, "mt", "-s", "-n", "1", pth); err != nil {
+ } else if utils.CmdExists("mt") {
+ if err := utils.RunCmd(3, "mt", "-s", "-n", "1", pth); err != nil {
return nil, err
}
data, err := ioutil.ReadFile(fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth))))