-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.go
87 lines (71 loc) · 1.7 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"github.com/Eyevinn/mp4ff/internal"
"github.com/Eyevinn/mp4ff/mp4"
)
const (
appName = "mp4ff-info"
)
var usg = `%s prints the box tree of input mp4 (ISOBMFF) file.
Usage of %s:
`
type options struct {
levels string
version bool
}
func parseOptions(fs *flag.FlagSet, args []string) (*options, error) {
fs.Usage = func() {
fmt.Fprintf(os.Stderr, usg, appName, appName)
fmt.Fprintf(os.Stderr, "\n%s [options] infile\n\noptions:\n", appName)
fs.PrintDefaults()
}
opts := options{}
fs.StringVar(&opts.levels, "l", "", "level of details, e.g. all:1 or trun:1,subs:1")
fs.BoolVar(&opts.version, "version", false, "Get mp4ff version")
err := fs.Parse(args[1:])
return &opts, err
}
func main() {
if err := run(os.Args, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(args []string, w io.Writer) error {
fs := flag.NewFlagSet(appName, flag.ContinueOnError)
opts, err := parseOptions(fs, args)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if opts.version {
fmt.Printf("%s %s\n", appName, internal.GetVersion())
return nil
}
if len(fs.Args()) != 1 {
fs.Usage()
return fmt.Errorf("need input file")
}
inFilePath := fs.Arg(0)
ifd, err := os.Open(inFilePath)
if err != nil {
return fmt.Errorf("could not open input file: %w", err)
}
defer ifd.Close()
parsedMp4, err := mp4.DecodeFile(ifd, mp4.WithDecodeMode(mp4.DecModeLazyMdat))
if err != nil {
return fmt.Errorf("could not parse input file: %w", err)
}
err = parsedMp4.Info(w, opts.levels, "", " ")
if err != nil {
return fmt.Errorf("could not print info: %w", err)
}
return nil
}