mirror of
https://github.com/xfrr/goffmpeg.git
synced 2025-11-03 02:03:21 +08:00
command wrapper
making transcoder
This commit is contained in:
29
transcoder/command.go
Normal file
29
transcoder/command.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Worker struct {
|
||||
Command string
|
||||
Args string
|
||||
Output chan string
|
||||
}
|
||||
|
||||
func (cmd *Worker) Run() {
|
||||
out, err := exec.Command(cmd.Command, cmd.Args).Output()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cmd.Output <- string(out)
|
||||
}
|
||||
|
||||
func Collect(c chan string) {
|
||||
for {
|
||||
msg := <-c
|
||||
fmt.Printf("The command result is %s\n", msg)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,10 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"goffmpeg/models"
|
||||
"os/exec"
|
||||
"fmt"
|
||||
"goffmpeg/ffmpeg"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Transcoder struct {
|
||||
@@ -15,14 +19,45 @@ type Transcoder struct {
|
||||
MediaFile *models.Mediafile
|
||||
}
|
||||
|
||||
func New(inputPath *string) (*Transcoder, error) {
|
||||
func New(inputPath *string, configuration *ffmpeg.Configuration) (*Transcoder, error) {
|
||||
transcoding := new(Transcoder)
|
||||
|
||||
if inputPath == nil {
|
||||
return nil, errors.New("error: transcoder.Initialize -> inputPath missing")
|
||||
}
|
||||
|
||||
_, err := os.Stat(*inputPath)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, errors.New("error: transcoder.Initialize -> input file not found")
|
||||
}
|
||||
|
||||
// Set input path
|
||||
transcoding.InputPath = *inputPath
|
||||
|
||||
// TODO: Get file metadata from ffprobe and set MediaFile
|
||||
command := fmt.Sprintf("%s -i %s -print_format json -show_format -show_streams -show_error", configuration.FfprobeBin, *inputPath)
|
||||
|
||||
cmd := exec.Command("/bin/sh", "-c", command)
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
cmd.Stdout = &out
|
||||
|
||||
cmdErr := cmd.Start()
|
||||
|
||||
if cmdErr != nil {
|
||||
return nil, cmdErr
|
||||
}
|
||||
|
||||
_, errProc := cmd.Process.Wait()
|
||||
if errProc != nil {
|
||||
return nil, errProc
|
||||
}
|
||||
|
||||
stdout := out.String()
|
||||
|
||||
fmt.Println(stdout)
|
||||
|
||||
transcoding.MediaFile = new(models.Mediafile)
|
||||
|
||||
return transcoding, nil
|
||||
|
||||
Reference in New Issue
Block a user