9
9
import com .sedmelluq .discord .lavaplayer .tools .io .HttpInterface ;
10
10
import com .sedmelluq .discord .lavaplayer .tools .io .HttpInterfaceManager ;
11
11
import com .sedmelluq .discord .lavaplayer .track .*;
12
+ import org .apache .commons .io .IOUtils ;
12
13
import org .apache .http .client .config .RequestConfig ;
13
14
import org .apache .http .impl .client .HttpClientBuilder ;
14
15
import org .jetbrains .annotations .NotNull ;
15
16
import org .slf4j .Logger ;
16
17
import org .slf4j .LoggerFactory ;
17
18
19
+ import java .io .BufferedInputStream ;
18
20
import java .io .DataInput ;
19
21
import java .io .IOException ;
22
+ import java .nio .charset .StandardCharsets ;
20
23
import java .util .ArrayList ;
21
24
import java .util .List ;
22
25
import java .util .function .Consumer ;
@@ -169,7 +172,7 @@ public AudioItem getItem(String identifier) throws IOException {
169
172
var args = new ArrayList <>(List .of (this .customLoadArgs ));
170
173
args .add (identifier );
171
174
var process = getProcess (args );
172
- var json = JsonBrowser . parse (process . getInputStream () );
175
+ var json = getProcessJsonOutput (process );
173
176
174
177
var type = json .get ("_type" ).text ();
175
178
switch (type ) {
@@ -187,6 +190,7 @@ Process getProcess(List<String> args) {
187
190
argList .add (this .path );
188
191
argList .addAll (args );
189
192
193
+ log .debug ("Starting yt-dlp with args: {}" , argList );
190
194
var processBuilder = new ProcessBuilder (argList );
191
195
processBuilder .redirectErrorStream (true );
192
196
@@ -198,6 +202,28 @@ Process getProcess(List<String> args) {
198
202
}
199
203
}
200
204
205
+ JsonBrowser getProcessJsonOutput (Process process ) throws IOException {
206
+ try (var stream = new BufferedInputStream (process .getInputStream ())) {
207
+ var data = IOUtils .toString (stream , StandardCharsets .UTF_8 );
208
+ log .debug ("yt-dlp process output: {}" , data );
209
+ int exitCode ;
210
+ try {
211
+ exitCode = process .waitFor ();
212
+ } catch (InterruptedException e ) {
213
+ Thread .currentThread ().interrupt ();
214
+ throw new IOException ("yt-dlp process was interrupted" , e );
215
+ }
216
+ if (exitCode != 0 ) {
217
+ throw new RuntimeException ("Failed to retrieve item, error: " + data );
218
+ }
219
+ try {
220
+ return JsonBrowser .parse (data );
221
+ } catch (IOException e ) {
222
+ throw new IOException ("Failed to parse yt-dlp output as JSON: " + data , e );
223
+ }
224
+ }
225
+ }
226
+
201
227
@ Override
202
228
public void shutdown () {
203
229
try {
0 commit comments