diff --git a/transcoder/command.go b/transcoder/command.go new file mode 100644 index 0000000..37cc4cf --- /dev/null +++ b/transcoder/command.go @@ -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) + } +} \ No newline at end of file diff --git a/transcoder/transcoder.go b/transcoder/transcoder.go index 174ce85..4b1d53e 100644 --- a/transcoder/transcoder.go +++ b/transcoder/transcoder.go @@ -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