-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (120 loc) · 3.32 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/grafov/m3u8"
"gopkg.in/vansante/go-ffprobe.v2"
)
func getPodID(line string) string {
podIDRegexp := `.*/pod/([0-9]*)/profile/.*`
r := regexp.MustCompile(podIDRegexp)
match := r.FindStringSubmatch(line)
if len(match) > 1 {
return match[1]
}
return ""
}
func parseStartEndPTS(filePath string) {
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
data, err := ffprobe.ProbeURL(ctx, filePath)
if err != nil {
fmt.Printf("Error getting data: %v\n", err)
panic(err)
}
for _, stream := range data.Streams {
if stream.CodecType == string(ffprobe.StreamVideo) {
fmt.Printf("pts: %d durationTS %f", stream.StartPts, data.Format.DurationSeconds)
}
}
fmt.Printf(" startTime: %fs endTime: %fs [%s]\n", data.Format.StartTimeSeconds, data.Format.DurationSeconds+data.Format.StartTimeSeconds, filepath.Base(filePath))
}
func downloadSegment(fileName string, segmentURL *url.URL) {
resp, err := http.Get(segmentURL.String())
if err != nil || resp.StatusCode > 399 {
fmt.Printf("Error downloading %s\n", segmentURL)
panic(err)
}
defer resp.Body.Close()
out, err := os.Create(fileName)
if err != nil {
fmt.Printf("Error creating file %s: %v\n", fileName, err)
panic(err)
}
defer out.Close()
io.Copy(out, resp.Body)
}
func downloadPlaylist(playlistURL, playlistFilePath string) {
resp, err := http.Get(playlistURL)
if err != nil {
fmt.Printf("Error downloading %s\n", playlistURL)
panic(err)
}
defer resp.Body.Close()
out, err := os.Create(playlistFilePath)
if err != nil {
fmt.Printf("Error creating file %s: %v\n", "playlist.m3u8", err)
panic(err)
}
defer out.Close()
io.Copy(out, resp.Body)
}
func parseMediaPlaylist(playlistURL *url.URL, tmpDir, filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error opening file %s: %v\n", filePath, err)
panic(err)
}
playlistBufioReader := bufio.NewReader(file)
playlist, _, _ := m3u8.DecodeFrom(playlistBufioReader, true)
mediapl := playlist.(*m3u8.MediaPlaylist)
for _, segment := range mediapl.Segments {
if segment == nil {
break
}
if segment.Discontinuity {
fmt.Printf("DISCONTINUITY\n")
}
segmentURL, err := url.Parse(segment.URI)
if err != nil {
panic(err)
}
urlWithoutParams := strings.Split(segmentURL.String(), "?")[0]
urlPath := filepath.Base(urlWithoutParams)
podId := getPodID(urlWithoutParams)
segmentfileName := fmt.Sprintf("%s/%s-%s", tmpDir, podId, urlPath)
if segmentURL.Scheme == "" {
segmentURL = playlistURL.ResolveReference(segmentURL)
// will evaluate only Google segments
continue
}
downloadSegment(segmentfileName, segmentURL)
parseStartEndPTS(segmentfileName)
}
}
func main() {
if len(os.Args) <= 1 {
fmt.Println("Error: Missing playlist URL")
return
}
playlist := os.Args[1]
playlistURL, err := url.Parse(playlist)
if err != nil {
panic(err)
}
tmpDir := fmt.Sprintf("/tmp/%d", time.Now().Unix())
os.MkdirAll(tmpDir, os.ModePerm)
fmt.Printf("\nAnalyzing: %s\n\nSaving files at: %s\n\n", playlist, tmpDir)
playlistFilePath := fmt.Sprintf("%s/playlist.m3u8", tmpDir)
downloadPlaylist(playlistURL.String(), playlistFilePath)
parseMediaPlaylist(playlistURL, tmpDir, playlistFilePath)
}