4
4
"bufio"
5
5
"errors"
6
6
"fmt"
7
- "io"
8
7
"io/ioutil"
9
8
"log"
10
9
"net/http"
@@ -53,63 +52,24 @@ func fixExtension(str string) string {
53
52
if strings .Contains (str , "mp3" ) {
54
53
str = ".mp3"
55
54
} else {
56
- format = ".flv"
55
+ str = ".flv"
57
56
}
58
57
59
58
return str
60
59
}
61
60
62
- // decodeStream accept Values and decodes them individually
63
- // decodeStream returns the final RawVideoStream object
64
- func decodeStream (values url.Values , streams * RawVideoStream , rawstream []stream ) error {
65
- streams .Author = values ["author" ][0 ]
66
- streams .Title = values ["title" ][0 ]
67
- streamMap , ok := values ["url_encoded_fmt_stream_map" ]
68
- if ! ok {
69
- return errors .New ("Error reading encoded stream map" )
70
- }
71
-
72
- // read and decode streams
73
- streamsList := strings .Split (string (streamMap [0 ]), "," )
74
- for streamPos , streamRaw := range streamsList {
75
- streamQry , err := url .ParseQuery (streamRaw )
76
- if err != nil {
77
- logrus .Infof ("Error occured during stream decoding %d: %s\n " , streamPos , err )
78
- continue
79
- }
80
- var sig string
81
- if _ , exist := streamQry ["sig" ]; exist {
82
- sig = streamQry ["sig" ][0 ]
83
- }
84
-
85
- rawstream = append (rawstream , stream {
86
- "quality" : streamQry ["quality" ][0 ],
87
- "type" : streamQry ["type" ][0 ],
88
- "url" : streamQry ["url" ][0 ],
89
- "sig" : sig ,
90
- "title" : values ["title" ][0 ],
91
- "author" : values ["author" ][0 ],
92
- })
93
- logrus .Infof ("Stream found: quality '%s', format '%s'" , streamQry ["quality" ][0 ], streamQry ["type" ][0 ])
94
- }
95
-
96
- streams .URLEncodedFmtStreamMap = rawstream
97
- return nil
98
- }
99
-
100
61
// encodeAudioStream consumes a raw data stream and
101
62
// encodeAudioStream encodes the data stream in mp3
102
63
func encodeAudioStream (file , path , surl string , bitrate uint ) error {
103
- resp , err := downloadVideoStream (surl )
64
+ data , err := downloadVideoStream (surl )
104
65
if err != nil {
105
66
log .Printf ("Http.Get\n error: %s\n URL: %s\n " , err , surl )
106
67
return err
107
68
}
108
- defer resp .Body .Close ()
109
69
110
70
tmp , _ := os .OpenFile ("_temp_" , os .O_CREATE , 0755 )
111
71
defer tmp .Close ()
112
- if _ , err := io . Copy ( tmp , resp . Body ); err != nil {
72
+ if _ , err := tmp . Write ( data ); err != nil {
113
73
logrus .Errorf ("Failed to read response body: %v" , err )
114
74
return err
115
75
}
@@ -121,7 +81,7 @@ func encodeAudioStream(file, path, surl string, bitrate uint) error {
121
81
return err
122
82
}
123
83
124
- outputDirectory := currentDirectory .HomeDir + "Downloads" + path
84
+ outputDirectory := currentDirectory .HomeDir + "/ Downloads/ " + path
125
85
outputFile := filepath .Join (outputDirectory , file )
126
86
if err := os .MkdirAll (filepath .Dir (outputFile ), 0775 ); err != nil {
127
87
logrus .Errorf ("Unable to create output directory: %v" , err )
@@ -149,12 +109,11 @@ func encodeAudioStream(file, path, surl string, bitrate uint) error {
149
109
// encodeVideoStream consumes video data stream and
150
110
// encodeVideoStream encodes the video in flv
151
111
func encodeVideoStream (file , path , surl string ) error {
152
- resp , err := downloadVideoStream (surl )
112
+ data , err := downloadVideoStream (surl )
153
113
if err != nil {
154
114
log .Printf ("Http.Get\n error: %s\n URL: %s\n " , err , surl )
155
115
return err
156
116
}
157
- defer resp .Body .Close ()
158
117
159
118
// Create output file
160
119
currentDirectory , err := user .Current ()
@@ -163,7 +122,7 @@ func encodeVideoStream(file, path, surl string) error {
163
122
return err
164
123
}
165
124
166
- outputDirectory := currentDirectory .HomeDir + "Downloads" + path
125
+ outputDirectory := currentDirectory .HomeDir + "/ Downloads/ " + path
167
126
outputFile := filepath .Join (outputDirectory , file )
168
127
if err := os .MkdirAll (filepath .Dir (outputFile ), 0775 ); err != nil {
169
128
logrus .Errorf ("Unable to create output directory: %v" , err )
@@ -177,7 +136,7 @@ func encodeVideoStream(file, path, surl string) error {
177
136
defer fp .Close ()
178
137
179
138
//saving downloaded file.
180
- if _ , err = io . Copy ( fp , resp . Body ); err != nil {
139
+ if _ , err = fp . Write ( data ); err != nil {
181
140
logrus .Errorf ("Unable to encode video stream: %s `->` %v" , surl , err )
182
141
return err
183
142
}
@@ -186,7 +145,7 @@ func encodeVideoStream(file, path, surl string) error {
186
145
187
146
// downloadVideoStream downloads video streams from youtube
188
147
// downloadVideoStream returns the *http.Reponse body
189
- func downloadVideoStream (url string ) (* http. Response , error ) {
148
+ func downloadVideoStream (url string ) ([] byte , error ) {
190
149
resp , err := http .Get (url )
191
150
if err != nil {
192
151
logrus .Errorf ("Unable to fetch Data stream from URL(%s)\n : %v" , url , err )
@@ -199,7 +158,9 @@ func downloadVideoStream(url string) (*http.Response, error) {
199
158
return nil , errors .New ("Non 200 status code received" )
200
159
}
201
160
202
- return resp , nil
161
+ output , _ := ioutil .ReadAll (resp .Body )
162
+
163
+ return output , nil
203
164
}
204
165
205
166
// getVideoId extracts the video id string from youtube url
@@ -221,6 +182,38 @@ func getVideoId(url string) (string, error) {
221
182
}
222
183
}
223
184
185
+ // decodeStream accept Values and decodes them individually
186
+ // decodeStream returns the final RawVideoStream object
187
+ func decodeStream (values url.Values , streams * RawVideoStream , rawstream []stream ) error {
188
+ streams .Author = values .Get ("author" )
189
+ streams .Title = values .Get ("title" )
190
+ streamMap := values .Get ("url_encoded_fmt_stream_map" )
191
+
192
+ // read and decode streams
193
+ streamsList := strings .Split (string (streamMap ), "," )
194
+ for streamPos , streamRaw := range streamsList {
195
+ streamQry , err := url .ParseQuery (streamRaw )
196
+ if err != nil {
197
+ logrus .Infof ("Error occured during stream decoding %d: %s\n " , streamPos , err )
198
+ continue
199
+ }
200
+ var sig string
201
+ sig = streamQry .Get ("sig" )
202
+ rawstream = append (rawstream , stream {
203
+ "quality" : streamQry .Get ("quality" ),
204
+ "type" : streamQry .Get ("type" ),
205
+ "url" : streamQry .Get ("url" ),
206
+ "sig" : sig ,
207
+ "title" : values .Get ("title" ),
208
+ "author" : values .Get ("author" ),
209
+ })
210
+ logrus .Infof ("Stream found: quality '%s', format '%s'" , streamQry .Get ("quality" ), streamQry .Get ("type" ))
211
+ }
212
+
213
+ streams .URLEncodedFmtStreamMap = rawstream
214
+ return nil
215
+ }
216
+
224
217
// decodeVideoStream processes downloaded video stream and
225
218
// decodeVideoStream calls helper functions and writes the
226
219
// output in the required format
@@ -232,13 +225,12 @@ func decodeVideoStream(videoId, path, format string, bitrate uint) error {
232
225
rawVideo .VideoId = videoId
233
226
rawVideo .VideoInfo = streamApiUrl + videoId
234
227
235
- videoStream , err := downloadVideoStream (rawVideo .VideoInfo )
228
+ data , err := downloadVideoStream (rawVideo .VideoInfo )
236
229
if err != nil {
237
230
logrus .Errorf ("Unable to get video stream: %v" , err )
238
231
return err
239
232
}
240
233
241
- data , _ := ioutil .ReadAll (videoStream .Body )
242
234
parsedResp , err := url .ParseQuery (string (data ))
243
235
if err != nil {
244
236
logrus .Errorf ("Error parsing video byte stream: %v" , err )
@@ -261,10 +253,10 @@ func decodeVideoStream(videoId, path, format string, bitrate uint) error {
261
253
return errors .New ("Unable to decode raw video streams" )
262
254
}
263
255
264
- file := removeWhiteSpace (rawVideo .Title + fixExtension (format ) )
256
+ file := removeWhiteSpace (rawVideo .Title ) + fixExtension (format )
265
257
surl := decStreams [0 ]["url" ] + "&signature" + decStreams [0 ]["sig" ]
266
- logrus .Infof ("Downloading data to file: %s" , file )
267
258
259
+ logrus .Infof ("Downloading data to file: %s" , file )
268
260
if strings .Contains (file , "mp3" ) {
269
261
if err := encodeAudioStream (file , path , surl , bitrate ); err != nil {
270
262
logrus .Errorf ("Unable to encode %s: %v" , format , err )
0 commit comments