Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
148 lines (125 loc) · 4.75 KB
/
Copy pathmain.go
File metadata and controls
148 lines (125 loc) · 4.75 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"errors"
"flag"
"fmt"
"log"
"strings"
"github.com/asticode/go-astiav"
)
var (
input = flag.String("i", "", "the input path")
)
func main() {
// Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(c astiav.Classer, l astiav.LogLevel, fmt, msg string) {
var cs string
if c != nil {
if cl := c.Class(); cl != nil {
cs = " - class: " + cl.String()
}
}
log.Printf("ffmpeg log: %s%s - level: %d\n", strings.TrimSpace(msg), cs, l)
})
// Parse flags
flag.Parse()
// Usage
if *input == "" {
log.Println("Usage: <binary path> -i <input path>")
return
}
// Allocate input format context
inputFormatContext := astiav.AllocFormatContext()
if inputFormatContext == nil {
log.Println(errors.New("main: input format context is nil"))
return
}
defer inputFormatContext.Free()
// Open input
if err := inputFormatContext.OpenInput(*input, nil, nil); err != nil {
log.Println(fmt.Errorf("main: opening input failed: %w", err))
return
}
defer inputFormatContext.CloseInput()
// Find stream info
if err := inputFormatContext.FindStreamInfo(nil); err != nil {
log.Println(fmt.Errorf("main: finding stream info failed: %w", err))
return
}
// Iterate over all streams in the container.
for _, stream := range inputFormatContext.Streams() {
params := stream.CodecParameters()
// Look up the decoder to get human-readable codec name and description.
var name, description string
if codec := astiav.FindDecoder(params.CodecID()); codec != nil {
name, description = codec.Name(), codec.LongName()
}
fmt.Printf("stream index: %d\n", stream.Index())
fmt.Printf("\ttype: %s\n", params.MediaType())
fmt.Printf("\tname: %s\n", name)
fmt.Printf("\tdescription: %s\n", description)
fmt.Printf("\tprofile: %s\n", params.ProfileName())
fmt.Printf("\ttime base: %s\n", stream.TimeBase())
fmt.Printf("\tstart time: %f\n", float64(stream.StartTime())*stream.TimeBase().Float64())
fmt.Printf("\tduration (stream timebase): %d\n", stream.Duration())
fmt.Printf("\tduration (seconds): %f\n", float64(stream.Duration())*stream.TimeBase().Float64())
fmt.Printf("\tcodec id: %d\n", params.CodecID())
flags := stream.DispositionFlags()
fmt.Printf("\tdefault: %t\n", flags.Has(astiav.DispositionFlagDefault))
fmt.Printf("\tforced: %t\n", flags.Has(astiav.DispositionFlagForced))
// Dispatch based on media type and build the appropriate stream struct.
switch params.MediaType() {
case astiav.MediaTypeVideo:
sar := params.SampleAspectRatio()
dar := sar.Mul(astiav.NewRational(params.Width(), params.Height()))
fmt.Printf("\theight: %d\n", params.Height())
fmt.Printf("\twidth: %d\n", params.Width())
fmt.Printf("\tsample aspect ratio: %d:%d\n", sar.Num(), sar.Den())
fmt.Printf("\tdisplay aspect ratio: %d:%d\n", dar.Num(), dar.Den())
fmt.Printf("\treal frame rate: %f\n", stream.RFrameRate().Float64())
fmt.Printf("\tavg frame rate: %f\n", stream.AvgFrameRate().Float64())
case astiav.MediaTypeAudio:
fmt.Printf("\tchannels: %d\n", params.ChannelLayout().Channels())
fmt.Printf("\tchannel layout: %s\n", params.ChannelLayout().String())
fmt.Printf("\tsample rate: %d\n", params.SampleRate())
fmt.Printf("\tbit rate: %d\n", params.BitRate())
case astiav.MediaTypeSubtitle:
}
printDictionary(stream.Metadata())
fmt.Printf("\n")
}
// Iterate over all chapters in the container.
for i, chapter := range inputFormatContext.Chapters() {
fmt.Printf("chapter index: %d\n", i)
fmt.Printf("\tid: %d\n", chapter.ID())
fmt.Printf("\tstart time: %f\n", float64(chapter.Start())*chapter.TimeBase().Float64())
fmt.Printf("\tend time: %f\n", float64(chapter.End())*chapter.TimeBase().Float64())
printDictionary(chapter.Metadata())
fmt.Printf("\n")
}
// Print the container format information.
fmt.Printf("format:\n")
fmt.Printf("\tname: %s\n", inputFormatContext.InputFormat().Name())
fmt.Printf("\tdescription: %s\n", inputFormatContext.InputFormat().LongName())
fmt.Printf("\tbit rate: %d\n", inputFormatContext.BitRate())
fmt.Printf("\tstart time: %f\n", float64(inputFormatContext.StartTime())/float64(astiav.TimeBaseQ.Den()))
fmt.Printf("\tduration (format timebase): %d\n", inputFormatContext.Duration())
fmt.Printf("\tduration (seconds): %f\n", float64(inputFormatContext.Duration())/float64(astiav.TimeBaseQ.Den()))
printDictionary(inputFormatContext.Metadata())
}
func printDictionary(dictionary *astiav.Dictionary) {
if dictionary == nil {
return
}
fmt.Printf("\tmetadata:\n")
var entry *astiav.DictionaryEntry
flags := astiav.NewDictionaryFlags(astiav.DictionaryFlagIgnoreSuffix)
for {
entry = dictionary.Get("", entry, flags) // flags = 0 is standard
if entry == nil {
return
}
fmt.Printf("\t\t%s=%s\n", entry.Key(), entry.Value())
}
}