-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_plan.go
114 lines (95 loc) · 2.48 KB
/
new_plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright ©2022 Evolution. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// ease tool's new-plan subcommand implementation.
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/evolution-gaming/ease/internal/encoding"
)
// inputFiles implements flag.Value interface.
type inputFiles []string
func (i *inputFiles) String() string {
return strings.Join(*i, ", ")
}
func (i *inputFiles) Set(value string) error {
*i = append(*i, value)
return nil
}
func CreateNewPlanCommand() *NewPlanApp {
longHelp := `Subcommand "new-plan" helps create a new plan configuration file template.
Examples:
ease new-plan -i path/to/input/video.mp4 -o plan.json
ease new-plan -i video1.mp4 -i video2.mp4 -o plan.json`
app := &NewPlanApp{
fs: flag.NewFlagSet("new-plan", flag.ContinueOnError),
}
app.fs.StringVar(&app.flOutFile, "o", "", "Output file (stdout by default).")
app.fs.Var(&app.flInputFiles, "i", "Source video files. Use multiple times for multiple files.")
app.fs.Usage = func() {
printSubCommandUsage(longHelp, app.fs)
}
return app
}
type NewPlanApp struct {
// FlagSet instance
fs *flag.FlagSet
// Output file to save plot to
flOutFile string
// Video input files
flInputFiles inputFiles
}
func (a *NewPlanApp) Run(args []string) error {
if err := a.fs.Parse(args); err != nil {
return &AppError{
msg: "usage error",
exitCode: 2,
}
}
// In case no input video provided we will use some placeholder string.
if len(a.flInputFiles) == 0 {
a.flInputFiles = []string{"path/to/source/video.mp4"}
}
// Create a PlanConfig instance and populate it with some data. From this we shall
// crate a JSON plan.
pc := encoding.PlanConfig{}
pc.Inputs = a.flInputFiles
pc.Schemes = []encoding.Scheme{
{
Name: "encode1",
CommandTpl: "ffmpeg -i %INPUT% -c:v libx264 -preset fast -crf 23 %OUTPUT%.mp4",
},
{
Name: "encode2",
CommandTpl: "ffmpeg -i %INPUT% -c:v libx264 -preset faster -crf 25 %OUTPUT%.mp4",
},
}
var out io.Writer
switch a.flOutFile {
case "":
out = os.Stdout
default:
fd, err := os.Create(a.flOutFile)
if err != nil {
return &AppError{
msg: fmt.Sprintf("output file error: %s", err),
exitCode: 1,
}
}
out = fd
}
e := json.NewEncoder(out)
e.SetIndent("", " ")
if err := e.Encode(pc); err != nil {
return &AppError{
msg: "JSON marshal error",
exitCode: 1,
}
}
return nil
}