Debian Codegraph @

tags.go

raw symbols SCIP
1 package ffprobe
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 )
8
9 // ErrTagNotFound is a sentinel error used when a queried tag does not exist
10 var ErrTagNotFound = errors.New("tag not found")
11
12 // Tags is the map of tag names to values
13 type Tags map[string]interface{}
14
15 // GetInt returns a tag value as int64 and an error if one occurred.
16 // ErrTagNotFound will be returned if the key can't be found, ParseError if
17 // a parsing error occurs.
18 func (t Tags) GetInt(tag string) (int64, error) {
19 v, found := t[tag]
20 if !found || v == nil {
21 return 0, ErrTagNotFound
22 }
23
24 switch v := v.(type) {
25 case string:
26 return valToInt64(v)
27 case float64:
28 return int64(v), nil
29 case int64:
30 return v, nil
31 }
32
33 str := fmt.Sprintf("%v", v)
34 return valToInt64(str)
35 }
36
37 func valToInt64(str string) (int64, error) {
38 val, err := strconv.ParseInt(str, 10, 64)
39 if err != nil {
40 return 0, fmt.Errorf("int64 parsing error (%v): %w", str, err)
41 }
42 return val, nil
43 }
44
45 // GetString returns a tag value as string and an error if one occurred.
46 // ErrTagNotFound will be returned if the key can't be found
47 func (t Tags) GetString(tag string) (string, error) {
48 v, found := t[tag]
49 if !found || v == nil {
50 return "", ErrTagNotFound
51 }
52 return valToString(v), nil
53 }
54
55 func valToString(v interface{}) string {
56 switch v := v.(type) {
57 case string:
58 return v
59 case float64:
60 return strconv.FormatFloat(v, 'f', -1, 64)
61 case int64:
62 return strconv.FormatInt(v, 10)
63 }
64
65 return fmt.Sprintf("%v", v)
66 }
67
68 // GetFloat returns a tag value as float64 and an error if one occurred.
69 // ErrTagNotFound will be returned if the key can't be found.
70 func (t Tags) GetFloat(tag string) (float64, error) {
71 v, found := t[tag]
72 if !found || v == nil {
73 return 0, ErrTagNotFound
74 }
75
76 switch v := v.(type) {
77 case string:
78 return valToFloat64(v)
79 case float64:
80 return v, nil
81 case int64:
82 return float64(v), nil
83 }
84
85 str := fmt.Sprintf("%v", v)
86 return valToFloat64(str)
87 }
88
89 func valToFloat64(str string) (float64, error) {
90 val, err := strconv.ParseFloat(str, 64)
91 if err != nil {
92 return 0, fmt.Errorf("float64 parsing error (%v): %w", str, err)
93 }
94 return val, nil
95 }
96
97 // FormatTags is a json data structure to represent format tags
98 // Deprecated, use the Tags of TagList instead
99 type FormatTags struct {
100 MajorBrand string `json:"major_brand"`
101 MinorVersion string `json:"minor_version"`
102 CompatibleBrands string `json:"compatible_brands"`
103 CreationTime string `json:"creation_time"`
104 }
105
106 func (f *FormatTags) setFrom(tags Tags) {
107 f.MajorBrand, _ = tags.GetString("major_brand")
108 f.MinorVersion, _ = tags.GetString("minor_version")
109 f.CompatibleBrands, _ = tags.GetString("compatible_brands")
110 f.CreationTime, _ = tags.GetString("creation_time")
111 }
112
113 // StreamTags is a json data structure to represent stream tags
114 // Deprecated, use the Tags of TagList instead
115 type StreamTags struct {
116 Rotate int `json:"rotate,string,omitempty"`
117 CreationTime string `json:"creation_time,omitempty"`
118 Language string `json:"language,omitempty"`
119 Title string `json:"title,omitempty"`
120 Encoder string `json:"encoder,omitempty"`
121 Location string `json:"location,omitempty"`
122 }
123
124 func (s *StreamTags) setFrom(tags Tags) {
125 rotate, _ := tags.GetInt("rotate")
126 s.Rotate = int(rotate)
127
128 s.CreationTime, _ = tags.GetString("creation_time")
129 s.Language, _ = tags.GetString("language")
130 s.Title, _ = tags.GetString("title")
131 s.Encoder, _ = tags.GetString("encoder")
132 s.Location, _ = tags.GetString("location")
133 }