This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffprobe.go
157 lines (143 loc) · 5.09 KB
/
ffprobe.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
package main
import (
"encoding/json"
"fmt"
"strings"
)
var ffprobe_cmdline string = "ffprobe -v quiet -print_format json -show_format -show_streams -show_chapters "
func GetFFprobeInfo(filename string) (*VideoProbeInfo, error) {
output, err := OutputBytesForCommand(ffprobe_cmdline + filename)
if err != nil {
return nil, err
}
var result VideoProbeInfo
err = json.Unmarshal(output, &result)
if err != nil {
return nil, fmt.Errorf("could not unmarshal ffprobe output: %w", err)
}
return &result, nil
}
type VideoProbeInfo struct {
Streams []Stream `json:"streams"`
Format Format `json:"format"`
Chapters []Chapter `json:"chapters"`
}
type Subtitle struct {
Index int
Title string
Language string
}
func (vpi VideoProbeInfo) ShowSubtitles() {
for _, stream := range vpi.Streams {
if stream.IsSubtitle() {
fmt.Printf("CodecName: %s, CodecTagString: %s, CodecTag: %s\n", stream.CodecName, stream.CodecTagString, stream.CodecTag)
if lang, ok := stream.Tags["language"]; ok {
fmt.Println(" Language:", lang)
}
if title, ok := stream.Tags["title"]; ok {
fmt.Println("Subs title:", title)
}
}
}
}
func (vpi VideoProbeInfo) ShowChapters() {
fmt.Printf("Chapter count: %d\n", len(vpi.Chapters))
for _, chapter := range vpi.Chapters {
fmt.Println(chapter)
}
}
type Stream struct {
Index int `json:"index"`
CodecName string `json:"codec_name"`
CodecLongName string `json:"codec_long_name"`
Profile string `json:"profile"`
CodecType string `json:"codec_type"`
CodecTagString string `json:"codec_tag_string"`
CodecTag string `json:"codec_tag"`
Width int `json:"width"`
Height int `json:"height"`
CodedWidth int `json:"coded_width"`
CodedHeight int `json:"coded_height"`
ClosedCaptions int `json:"closed_captions"`
FilmGrain int `json:"film_grain"`
HasBFrames int `json:"has_b_frames"`
SampleAspectRatio string `json:"sample_aspect_ratio"`
DisplayAspectRatio string `json:"display_aspect_ratio"`
PixFmt string `json:"pix_fmt"`
Level int `json:"level"`
ColorRange string `json:"color_range"`
ColorSpace string `json:"color_space"`
ColorTransfer string `json:"color_transfer"`
ColorPrimaries string `json:"color_primaries"`
ChromaLocation string `json:"chroma_location"`
FieldOrder string `json:"field_order"`
Refs int `json:"refs"`
IsAvc string `json:"is_avc"`
NalLengthSize string `json:"nal_length_size"`
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"`
BitsPerRawSample string `json:"bits_per_raw_sample"`
ExtradataSize int `json:"extradata_size"`
Disposition 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"`
TimedThumbnails int `json:"timed_thumbnails"`
Captions int `json:"captions"`
Descriptions int `json:"descriptions"`
Metadata int `json:"metadata"`
Dependent int `json:"dependent"`
StillImage int `json:"still_image"`
} `json:"disposition"`
Tags map[string]string `json:"tags"`
}
func (stream Stream) GetLanguage() (string, error) {
for k, v := range stream.Tags {
if strings.TrimSpace(strings.ToLower(k)) == "language" {
return v, nil
}
}
return "", fmt.Errorf("no language tag found in this stream")
}
func (stream Stream) IsSubtitle() bool {
return stream.CodecType == "subtitle"
}
func (stream Stream) IsVideo() bool {
return stream.CodecType == "video"
}
func (stream Stream) IsAudio() bool {
return stream.CodecType == "video"
}
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"`
StartTime string `json:"start_time"`
Duration string `json:"duration"`
Size string `json:"size"`
BitRate string `json:"bit_rate"`
ProbeScore int `json:"probe_score"`
Tags map[string]string `json:"tags"`
}
type Chapter struct {
ID int `json:"id"`
TimeBase string `json:"time_base"`
Start int `json:"start"`
StartTime string `json:"start_time"`
End int64 `json:"end"`
EndTime string `json:"end_time"`
Tags map[string]string `json:"tags"`
}