|
| 1 | +package fluentffmpeg |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os/exec" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/fatih/structs" |
| 10 | +) |
| 11 | + |
| 12 | +// Command is a struct that holds arguments and their values to run FFmpeg |
| 13 | +type Command struct { |
| 14 | + FFmpegPath string |
| 15 | + Args *Args |
| 16 | + input io.Reader |
| 17 | + output io.Writer |
| 18 | + logs io.Writer |
| 19 | +} |
| 20 | + |
| 21 | +// NewCommand returns a new Command |
| 22 | +func NewCommand(ffmpegPath string) *Command { |
| 23 | + if ffmpegPath == "" { |
| 24 | + ffmpegPath = "ffmpeg" |
| 25 | + } |
| 26 | + return &Command{ |
| 27 | + FFmpegPath: ffmpegPath, |
| 28 | + Args: &Args{}, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Run runs the FFmpeg command. It returns an error if the command fails with exit status code 1. This error message only signifies that |
| 33 | +// the command returned a non-zero status code, read from stderr to see more comprehensive FFmpeg errors. |
| 34 | +func (c *Command) Run() error { |
| 35 | + return c.Build().Run() |
| 36 | +} |
| 37 | + |
| 38 | +// Build returns an exec.Cmd struct ready to run the FFmpeg command with its arguments |
| 39 | +func (c *Command) Build() *exec.Cmd { |
| 40 | + cmd := exec.Command(c.FFmpegPath, c.GetArgs()...) |
| 41 | + |
| 42 | + if c.input != nil { |
| 43 | + cmd.Stdin = c.input |
| 44 | + } |
| 45 | + |
| 46 | + if c.output != nil { |
| 47 | + cmd.Stdout = c.output |
| 48 | + } |
| 49 | + |
| 50 | + if c.logs != nil { |
| 51 | + cmd.Stderr = c.logs |
| 52 | + } |
| 53 | + |
| 54 | + return cmd |
| 55 | +} |
| 56 | + |
| 57 | +// GetArgs returns the arguments for the FFmpeg command. |
| 58 | +func (c *Command) GetArgs() []string { |
| 59 | + var inputs []string |
| 60 | + var outputs []string |
| 61 | + |
| 62 | + inputs = c.getArgs(c.Args.input, "pipeInput", "inputPath") |
| 63 | + outputs = c.getArgs(c.Args.output, "pipeOutput", "outputPath") |
| 64 | + |
| 65 | + return append(inputs, outputs...) |
| 66 | +} |
| 67 | + |
| 68 | +func (c *Command) getArgs(argType interface{}, targetNames ...string) []string { |
| 69 | + var options []string |
| 70 | + var target []string |
| 71 | + |
| 72 | + fields := structs.Names(argType) |
| 73 | + |
| 74 | + // Iterates through the fields, |
| 75 | + // and calls its corresponding getter function. |
| 76 | + for _, v := range fields { |
| 77 | + option := true |
| 78 | + if containsString(targetNames, v) { |
| 79 | + option = false |
| 80 | + } |
| 81 | + value := reflect.ValueOf(c.Args).MethodByName("Get" + strings.Title(v)) |
| 82 | + if (value != reflect.Value{}) { |
| 83 | + result := value.Call([]reflect.Value{}) |
| 84 | + if v, ok := result[0].Interface().([]string); ok { |
| 85 | + if option { |
| 86 | + options = append(options, v...) |
| 87 | + } else { |
| 88 | + target = append(target, v...) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return append(options, target...) |
| 95 | +} |
| 96 | + |
| 97 | +// OutputLogs sets the destination to write the FFmpeg log output to |
| 98 | +func (c *Command) OutputLogs(writer io.Writer) *Command { |
| 99 | + c.logs = writer |
| 100 | + return c |
| 101 | +} |
0 commit comments