| 1 | package ffprobe |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // StreamType represents a media stream type like video, audio, subtitles, etc |
| 8 | type StreamType string |
| 9 | |
| 10 | const ( |
| 11 | // StreamAny means any type of stream |
| 12 | StreamAny StreamType = "" |
| 13 | // StreamVideo is a video stream |
| 14 | StreamVideo StreamType = "video" |
| 15 | // StreamAudio is an audio stream |
| 16 | StreamAudio StreamType = "audio" |
| 17 | // StreamSubtitle is a subtitle stream |
| 18 | StreamSubtitle StreamType = "subtitle" |
| 19 | // StreamData is a data stream |
| 20 | StreamData StreamType = "data" |
| 21 | // StreamAttachment is an attachment stream |
| 22 | StreamAttachment StreamType = "attachment" |
| 23 | ) |
| 24 | |
| 25 | // ProbeData is the root json data structure returned by an ffprobe. |
| 26 | type ProbeData struct { |
| 27 | Streams []*Stream `json:"streams"` |
| 28 | Format *Format `json:"format"` |
| 29 | Chapters []*Chapter `json:"chapters"` |
| 30 | } |
| 31 | |
| 32 | // Format is a json data structure to represent formats |
| 33 | type Format struct { |
| 34 | Filename string `json:"filename"` |
| 35 | NBStreams int `json:"nb_streams"` |
| 36 | NBPrograms int `json:"nb_programs"` |
| 37 | FormatName string `json:"format_name"` |
| 38 | FormatLongName string `json:"format_long_name"` |
| 39 | StartTimeSeconds float64 `json:"start_time,string"` |
| 40 | DurationSeconds float64 `json:"duration,string"` |
| 41 | Size string `json:"size"` |
| 42 | BitRate string `json:"bit_rate"` |
| 43 | ProbeScore int `json:"probe_score"` |
| 44 | TagList Tags `json:"tags"` |
| 45 | Tags *FormatTags `json:"-"` // Deprecated: Use TagList instead |
| 46 | } |
| 47 | |
| 48 | // Stream is a json data structure to represent streams. |
| 49 | // A stream can be a video, audio, subtitle, etc type of stream. |
| 50 | type Stream struct { |
| 51 | Index int `json:"index"` |
| 52 | ID string `json:"id"` |
| 53 | CodecName string `json:"codec_name"` |
| 54 | CodecLongName string `json:"codec_long_name"` |
| 55 | CodecType string `json:"codec_type"` |
| 56 | CodecTimeBase string `json:"codec_time_base"` |
| 57 | CodecTagString string `json:"codec_tag_string"` |
| 58 | CodecTag string `json:"codec_tag"` |
| 59 | RFrameRate string `json:"r_frame_rate"` |
| 60 | AvgFrameRate string `json:"avg_frame_rate"` |
| 61 | TimeBase string `json:"time_base"` |
| 62 | StartPts int `json:"start_pts"` |
| 63 | StartTime string `json:"start_time"` |
| 64 | DurationTs uint64 `json:"duration_ts"` |
| 65 | Duration string `json:"duration"` |
| 66 | BitRate string `json:"bit_rate"` |
| 67 | BitsPerRawSample string `json:"bits_per_raw_sample"` |
| 68 | NbFrames string `json:"nb_frames"` |
| 69 | Disposition StreamDisposition `json:"disposition,omitempty"` |
| 70 | TagList Tags `json:"tags"` |
| 71 | Tags StreamTags `json:"-"` // Deprecated: Use TagList instead |
| 72 | FieldOrder string `json:"field_order,omitempty"` |
| 73 | Profile string `json:"profile,omitempty"` |
| 74 | Width int `json:"width"` |
| 75 | Height int `json:"height"` |
| 76 | HasBFrames int `json:"has_b_frames,omitempty"` |
| 77 | SampleAspectRatio string `json:"sample_aspect_ratio,omitempty"` |
| 78 | DisplayAspectRatio string `json:"display_aspect_ratio,omitempty"` |
| 79 | PixFmt string `json:"pix_fmt,omitempty"` |
| 80 | Level int `json:"level,omitempty"` |
| 81 | ColorRange string `json:"color_range,omitempty"` |
| 82 | ColorSpace string `json:"color_space,omitempty"` |
| 83 | ColorTransfer string `json:"color_transfer,omitempty"` |
| 84 | ColorPrimaries string `json:"color_primaries,omitempty"` |
| 85 | SampleFmt string `json:"sample_fmt,omitempty"` |
| 86 | SampleRate string `json:"sample_rate,omitempty"` |
| 87 | Channels int `json:"channels,omitempty"` |
| 88 | ChannelLayout string `json:"channel_layout,omitempty"` |
| 89 | BitsPerSample int `json:"bits_per_sample,omitempty"` |
| 90 | SideDataList SideDataList `json:"side_data_list,omitempty"` |
| 91 | } |
| 92 | |
| 93 | // StreamDisposition is a json data structure to represent stream dispositions |
| 94 | type StreamDisposition struct { |
| 95 | Default int `json:"default"` |
| 96 | Dub int `json:"dub"` |
| 97 | Original int `json:"original"` |
| 98 | Comment int `json:"comment"` |
| 99 | Lyrics int `json:"lyrics"` |
| 100 | Karaoke int `json:"karaoke"` |
| 101 | Forced int `json:"forced"` |
| 102 | HearingImpaired int `json:"hearing_impaired"` |
| 103 | VisualImpaired int `json:"visual_impaired"` |
| 104 | CleanEffects int `json:"clean_effects"` |
| 105 | AttachedPic int `json:"attached_pic"` |
| 106 | } |
| 107 | |
| 108 | // Chapters is a json data structure to represent chapters. |
| 109 | type Chapter struct { |
| 110 | ID int `json:"id"` |
| 111 | TimeBase string `json:"time_base"` |
| 112 | StartTimeSeconds float64 `json:"start_time,string"` |
| 113 | EndTimeSeconds float64 `json:"end_time,string"` |
| 114 | TagList Tags `json:"tags"` |
| 115 | } |
| 116 | |
| 117 | // StartTime returns the start time of the chapter as a time.Duration |
| 118 | func (c *Chapter) StartTime() time.Duration { |
| 119 | return time.Duration(c.StartTimeSeconds * float64(time.Second)) |
| 120 | } |
| 121 | |
| 122 | // EndTime returns the end timestamp of the chapter as a time.Duration |
| 123 | func (c *Chapter) EndTime() time.Duration { |
| 124 | return time.Duration(c.EndTimeSeconds * float64(time.Second)) |
| 125 | } |
| 126 | |
| 127 | // Title returns the value of the "title" tag of the chapter |
| 128 | func (c *Chapter) Title() string { |
| 129 | title, _ := c.TagList.GetString("title") |
| 130 | return title |
| 131 | } |
| 132 | |
| 133 | // StartTime returns the start time of the media file as a time.Duration |
| 134 | func (f *Format) StartTime() (duration time.Duration) { |
| 135 | return time.Duration(f.StartTimeSeconds * float64(time.Second)) |
| 136 | } |
| 137 | |
| 138 | // Duration returns the duration of the media file as a time.Duration |
| 139 | func (f *Format) Duration() (duration time.Duration) { |
| 140 | return time.Duration(f.DurationSeconds * float64(time.Second)) |
| 141 | } |
| 142 | |
| 143 | // StreamType returns all streams which are of the given type |
| 144 | func (p *ProbeData) StreamType(streamType StreamType) (streams []Stream) { |
| 145 | for _, s := range p.Streams { |
| 146 | if s == nil { |
| 147 | continue |
| 148 | } |
| 149 | switch streamType { |
| 150 | case StreamAny: |
| 151 | streams = append(streams, *s) |
| 152 | default: |
| 153 | if s.CodecType == string(streamType) { |
| 154 | streams = append(streams, *s) |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return streams |
| 159 | } |
| 160 | |
| 161 | // FirstVideoStream returns the first video stream found |
| 162 | func (p *ProbeData) FirstVideoStream() *Stream { |
| 163 | return p.firstStream(StreamVideo) |
| 164 | } |
| 165 | |
| 166 | // FirstAudioStream returns the first audio stream found |
| 167 | func (p *ProbeData) FirstAudioStream() *Stream { |
| 168 | return p.firstStream(StreamAudio) |
| 169 | } |
| 170 | |
| 171 | // FirstSubtitleStream returns the first subtitle stream found |
| 172 | func (p *ProbeData) FirstSubtitleStream() *Stream { |
| 173 | return p.firstStream(StreamSubtitle) |
| 174 | } |
| 175 | |
| 176 | // FirstDataStream returns the first data stream found |
| 177 | func (p *ProbeData) FirstDataStream() *Stream { |
| 178 | return p.firstStream(StreamData) |
| 179 | } |
| 180 | |
| 181 | // FirstAttachmentStream returns the first attachment stream found |
| 182 | func (p *ProbeData) FirstAttachmentStream() *Stream { |
| 183 | return p.firstStream(StreamAttachment) |
| 184 | } |
| 185 | |
| 186 | func (p *ProbeData) firstStream(streamType StreamType) *Stream { |
| 187 | for _, s := range p.Streams { |
| 188 | if s == nil { |
| 189 | continue |
| 190 | } |
| 191 | if s.CodecType == string(streamType) { |
| 192 | return s |
| 193 | } |
| 194 | } |
| 195 | return nil |
| 196 | } |