can't seem to exec properly

1,582 views
Skip to first unread message

jeremy.r...@gmail.com

unread,
Nov 18, 2013, 7:08:21 PM11/18/13
to golan...@googlegroups.com
I'm trying to call ffmpeg from inside a Go application.

I am able to execute the following command from the command prompt (inside of ~/vidconv):
ffmpeg -i sample.mov sample.mp4

However, when trying to accomplish the same task using the exec package, I'm hitting a roadblock.

cmd := exec.Command("ffmpeg", "-i", job.Src, job.Dest)
err := cmd.Start()
if err != nil {
log.Fatalf("Unable to execute ffmpeg command: %s\n", err)
return nil, err
}
go func() {
log.Printf("Starting job @ %s\n", job.Start)
err := cmd.Wait()
if err != nil {
log.Fatalf("The ffmpeg command failed: %s\nCommand: %s\n", err, cmd.Args)
}
job.ch <- err
}()

job.Src and job.Dest are strings containing the source and destination filenames.

My output is:
2013/11/18 15:57:45 Starting job @ 2013-11-18 15:57:45.789101712 -0800 PST
2013/11/18 15:57:45 The ffmpeg command failed: exit status 1
Command: [ffmpeg -i sample.mov sample.mp4]

The stdout and stderr of the command contain nothing--I have checked both.  "exit status 1" is fairly cryptic, and gives little insight into what I happen to be doing incorrectly.

Any help would be greatly appreciated.

Jan Mercl

unread,
Nov 18, 2013, 9:45:38 PM11/18/13
to jeremy.r...@gmail.com, golang-nuts

Your shell uses the PATH environment variable to find the binary. exec.Command does not, IIRC.

-j

brainman

unread,
Nov 18, 2013, 10:42:09 PM11/18/13
to golan...@googlegroups.com, jeremy.r...@gmail.com
> On Tuesday, 19 November 2013 11:08:21 UTC+11, jeremy.r...@gmail.com wrote:

> ... The stdout and stderr of the command contain nothing--I have checked both. ...

That is strange. This

package main

import (
"fmt"
"log"
"os/exec"
)

func main() {
out, err := exec.Command("date", "uuuu").CombinedOutput()
if err != nil {
log.Fatalf("date failed: %v %v", err, string(out))
}
fmt.Printf("The date is %s\n", out)
}

fails with

... date failed: exit status 1 date: invalid date `uuuu'
exit status 1
...

Perhaps ffmpeg does not report errors, but perhaps yo are mistaken.

Alex

Kevin Gillette

unread,
Nov 19, 2013, 12:58:32 AM11/19/13
to golan...@googlegroups.com, jeremy.r...@gmail.com
On Monday, November 18, 2013 7:45:38 PM UTC-7, Jan Mercl wrote:

Your shell uses the PATH environment variable to find the binary. exec.Command does not, IIRC.

Exec does use PATH. 

capnm

unread,
Nov 19, 2013, 3:40:14 AM11/19/13
to golan...@googlegroups.com, jeremy.r...@gmail.com
On Tuesday, 19 November 2013 01:08:21 UTC+1, jeremy.r...@gmail.com wrote:
I'm trying to call ffmpeg from inside a Go application.

I am able to execute the following command from the command prompt (inside of ~/vidconv):
ffmpeg -i sample.mov sample.mp4

However, when trying to accomplish the same task using the exec package, I'm hitting a roadblock.

cmd := exec.Command("ffmpeg", "-i", job.Src, job.Dest)
err := cmd.Start()

The executing ffmpeg works here as intended, you should capture CombinedOutput()
like Alex did (for what ever reason ubuntu deprecated ffmpeg.)

cat /tmp/x.go 
package main

import (
"fmt"
"log"
"os/exec"
)

func main() {
out, err := exec.Command("ffmpeg", "-i", "sample.mov", "sample.mp4").CombinedOutput()
if err != nil {
log.Fatalf("date failed: %v %v", err, string(out))
}
fmt.Printf("The date is %s\n", out)
}


go run x.go
2013/11/19 08:36:48 date failed: exec: "ffmpeg": executable file not found in $PATH


sudo apt-get install ffmpeg
go run x.go
2013/11/19 08:38:43 date failed: exit status 1 ffmpeg version 0.8.9-6:0.8.9-0ubuntu0.13.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:09:48 with gcc 4.7.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
sample.mov: No such file or directory
exit status 1


However in the past I tripped over, that passing "pass:notasecret"
to openssl didn't work, e.g. something like:
exec.Command("openssl", "pkcs12", "-in", p12FileName, "-passin", "pass:notasecret", "-out", pemFileName)

The work-around was to pipe all through bash:
cmd := fmt.Sprintln("openssl pkcs12 -in", p12FileName,
                "-nocerts -passin pass:notasecret -nodes -out", pemFileName)
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()

~martin

Carlos Castillo

unread,
Nov 19, 2013, 4:39:27 AM11/19/13
to golan...@googlegroups.com, jeremy.r...@gmail.com
From what I can tell, the error is probably that the input file doesn't exist. I had no problems getting the code to work (with some slight modifications because the job type isn't presented), and found that when the command couldn't be found it printed:

2013/11/19 01:23:48 Unable to execute ffmpeg command: exec: "ffmpeg": executable file not found in $PATH

But I did get the error code 1 when I put a non-existant input file.

Some suggestions:

  1. At least while debugging, show either go's current working directory (os.Getwd), or use the full path to the files (you can use path/filepath.Abs to turn your paths into absolute paths), this will show you if you made the wrong assumptions about where go and ffmpeg are looking for the files
  2. To easily see ffmpeg's output, which complains about the missing input file, without using CombinedOutput() or the like to awkwardly read it in your program, simply map your program's stdout/stderr to the commands via "cmd.Stderr, cmd.Stdout = os.Stderr, os.Stdout"
  3. If you don't map or control the command's stdin, you can't respond to the overwrite request made by ffmpeg when the destination file exists, ffmpeg assumes "no", use the "-y" flag to force yes if you don't want to have to write a "y" yourself to the stdin stream and have your program embody the replace logic instead.

jeremy.r...@gmail.com

unread,
Nov 19, 2013, 9:36:17 AM11/19/13
to golan...@googlegroups.com, jeremy.r...@gmail.com
Thank you all for your help.  I have been able to get this working properly thanks to you all.

Here's the result:
  • The system $PATH was being consulted when running the ffmpeg command--that much was correct.  The command was being located properly.
  • I was able to verify that I was in the proper working directory using os.Getwd().
  • There was indeed output in Stdout and Stderr, though I was only able to see it after following Carlos' advice to set cmd.Stderr/out to os.Stderr/out.  I'm not sure why this is the case, but I will investigate further (I'm on OS X).
  • The input file could not be located.  This was one of those PEBCAK errors where I had moved/renamed the file and failed to realize it.
I want to thank you all again for your help.

Carlos:  The advice about passing the -y flag was especially helpful, as I would likely not have caught that until much later on.

I'd also like to mention to capnm that Ubuntu might not be using the true ffmpeg library if you apt-get install.  It uses a fork called libav, and (at least in versions of Ubuntu that I have run) you have to go through alternate means to get ffmpeg.  The deprecation warning is falsy; hopefully they will make that clearer in the future.
Reply all
Reply to author
Forward
0 new messages