-
Notifications
You must be signed in to change notification settings - Fork 45
/
probedata.go
194 lines (174 loc) · 7.01 KB
/
probedata.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package ffprobe
import (
"time"
)
// StreamType represents a media stream type like video, audio, subtitles, etc
type StreamType string
const (
// StreamAny means any type of stream
StreamAny StreamType = ""
// StreamVideo is a video stream
StreamVideo StreamType = "video"
// StreamAudio is an audio stream
StreamAudio StreamType = "audio"
// StreamSubtitle is a subtitle stream
StreamSubtitle StreamType = "subtitle"
// StreamData is a data stream
StreamData StreamType = "data"
// StreamAttachment is an attachment stream
StreamAttachment StreamType = "attachment"
)
// ProbeData is the root json data structure returned by an ffprobe.
type ProbeData struct {
Streams []*Stream `json:"streams"`
Format *Format `json:"format"`
Chapters []*Chapter `json:"chapters"`
}
// Format is a json data structure to represent formats
type Format struct {
Filename string `json:"filename"`
NBStreams int `json:"nb_streams"`
NBPrograms int `json:"nb_programs"`
FormatName string `json:"format_name"`
FormatLongName string `json:"format_long_name"`
StartTimeSeconds float64 `json:"start_time,string"`
DurationSeconds float64 `json:"duration,string"`
Size string `json:"size"`
BitRate string `json:"bit_rate"`
ProbeScore int `json:"probe_score"`
TagList Tags `json:"tags"`
Tags *FormatTags `json:"-"` // Deprecated: Use TagList instead
}
// Stream is a json data structure to represent streams.
// A stream can be a video, audio, subtitle, etc type of stream.
type Stream struct {
Index int `json:"index"`
ID string `json:"id"`
CodecName string `json:"codec_name"`
CodecLongName string `json:"codec_long_name"`
CodecType string `json:"codec_type"`
CodecTimeBase string `json:"codec_time_base"`
CodecTagString string `json:"codec_tag_string"`
CodecTag string `json:"codec_tag"`
RFrameRate string `json:"r_frame_rate"`
AvgFrameRate string `json:"avg_frame_rate"`
TimeBase string `json:"time_base"`
StartPts int `json:"start_pts"`
StartTime string `json:"start_time"`
DurationTs uint64 `json:"duration_ts"`
Duration string `json:"duration"`
BitRate string `json:"bit_rate"`
BitsPerRawSample string `json:"bits_per_raw_sample"`
NbFrames string `json:"nb_frames"`
Disposition StreamDisposition `json:"disposition,omitempty"`
TagList Tags `json:"tags"`
Tags StreamTags `json:"-"` // Deprecated: Use TagList instead
FieldOrder string `json:"field_order,omitempty"`
Profile string `json:"profile,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
HasBFrames int `json:"has_b_frames,omitempty"`
SampleAspectRatio string `json:"sample_aspect_ratio,omitempty"`
DisplayAspectRatio string `json:"display_aspect_ratio,omitempty"`
PixFmt string `json:"pix_fmt,omitempty"`
Level int `json:"level,omitempty"`
ColorRange string `json:"color_range,omitempty"`
ColorSpace string `json:"color_space,omitempty"`
SampleFmt string `json:"sample_fmt,omitempty"`
SampleRate string `json:"sample_rate,omitempty"`
Channels int `json:"channels,omitempty"`
ChannelLayout string `json:"channel_layout,omitempty"`
BitsPerSample int `json:"bits_per_sample,omitempty"`
SideDataList SideDataList `json:"side_data_list,omitempty"`
}
// StreamDisposition is a json data structure to represent stream dispositions
type StreamDisposition struct {
Default int `json:"default"`
Dub int `json:"dub"`
Original int `json:"original"`
Comment int `json:"comment"`
Lyrics int `json:"lyrics"`
Karaoke int `json:"karaoke"`
Forced int `json:"forced"`
HearingImpaired int `json:"hearing_impaired"`
VisualImpaired int `json:"visual_impaired"`
CleanEffects int `json:"clean_effects"`
AttachedPic int `json:"attached_pic"`
}
// Chapters is a json data structure to represent chapters.
type Chapter struct {
ID int `json:"id"`
TimeBase string `json:"time_base"`
StartTimeSeconds float64 `json:"start_time,string"`
EndTimeSeconds float64 `json:"end_time,string"`
TagList Tags `json:"tags"`
}
// StartTime returns the start time of the chapter as a time.Duration
func (c *Chapter) StartTime() time.Duration {
return time.Duration(c.StartTimeSeconds * float64(time.Second))
}
// EndTime returns the end timestamp of the chapter as a time.Duration
func (c *Chapter) EndTime() time.Duration {
return time.Duration(c.EndTimeSeconds * float64(time.Second))
}
// Name returns the value of the "title" tag of the chapter
func (c *Chapter) Title() string {
title, _ := c.TagList.GetString("title")
return title
}
// StartTime returns the start time of the media file as a time.Duration
func (f *Format) StartTime() (duration time.Duration) {
return time.Duration(f.StartTimeSeconds * float64(time.Second))
}
// Duration returns the duration of the media file as a time.Duration
func (f *Format) Duration() (duration time.Duration) {
return time.Duration(f.DurationSeconds * float64(time.Second))
}
// StreamType returns all streams which are of the given type
func (p *ProbeData) StreamType(streamType StreamType) (streams []Stream) {
for _, s := range p.Streams {
if s == nil {
continue
}
switch streamType {
case StreamAny:
streams = append(streams, *s)
default:
if s.CodecType == string(streamType) {
streams = append(streams, *s)
}
}
}
return streams
}
// FirstVideoStream returns the first video stream found
func (p *ProbeData) FirstVideoStream() *Stream {
return p.firstStream(StreamVideo)
}
// FirstAudioStream returns the first audio stream found
func (p *ProbeData) FirstAudioStream() *Stream {
return p.firstStream(StreamAudio)
}
// FirstSubtitleStream returns the first subtitle stream found
func (p *ProbeData) FirstSubtitleStream() *Stream {
return p.firstStream(StreamSubtitle)
}
// FirstDataStream returns the first data stream found
func (p *ProbeData) FirstDataStream() *Stream {
return p.firstStream(StreamData)
}
// FirstAttachmentStream returns the first attachment stream found
func (p *ProbeData) FirstAttachmentStream() *Stream {
return p.firstStream(StreamAttachment)
}
func (p *ProbeData) firstStream(streamType StreamType) *Stream {
for _, s := range p.Streams {
if s == nil {
continue
}
if s.CodecType == string(streamType) {
return s
}
}
return nil
}