Skip to content

Commit 5b92ba9

Browse files
committed
add better ytdlp error logging
1 parent 8763588 commit 5b92ba9

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

main/src/main/java/com/github/topi314/lavasrc/ytdlp/YtdlpAudioSourceManager.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
1010
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterfaceManager;
1111
import com.sedmelluq.discord.lavaplayer.track.*;
12+
import org.apache.commons.io.IOUtils;
1213
import org.apache.http.client.config.RequestConfig;
1314
import org.apache.http.impl.client.HttpClientBuilder;
1415
import org.jetbrains.annotations.NotNull;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
1718

19+
import java.io.BufferedInputStream;
1820
import java.io.DataInput;
1921
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
2023
import java.util.ArrayList;
2124
import java.util.List;
2225
import java.util.function.Consumer;
@@ -169,7 +172,7 @@ public AudioItem getItem(String identifier) throws IOException {
169172
var args = new ArrayList<>(List.of(this.customLoadArgs));
170173
args.add(identifier);
171174
var process = getProcess(args);
172-
var json = JsonBrowser.parse(process.getInputStream());
175+
var json = getProcessJsonOutput(process);
173176

174177
var type = json.get("_type").text();
175178
switch (type) {
@@ -187,6 +190,7 @@ Process getProcess(List<String> args) {
187190
argList.add(this.path);
188191
argList.addAll(args);
189192

193+
log.debug("Starting yt-dlp with args: {}", argList);
190194
var processBuilder = new ProcessBuilder(argList);
191195
processBuilder.redirectErrorStream(true);
192196

@@ -198,6 +202,28 @@ Process getProcess(List<String> args) {
198202
}
199203
}
200204

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+
201227
@Override
202228
public void shutdown() {
203229
try {

main/src/main/java/com/github/topi314/lavasrc/ytdlp/YtdlpAudioTrack.java

+1-19
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
1111
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
1212
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor;
13-
import org.apache.commons.io.IOUtils;
1413
import org.slf4j.Logger;
1514
import org.slf4j.LoggerFactory;
1615

17-
import java.io.BufferedInputStream;
1816
import java.io.IOException;
1917
import java.net.URI;
20-
import java.nio.charset.StandardCharsets;
2118
import java.util.ArrayList;
2219
import java.util.List;
2320

@@ -40,22 +37,7 @@ private JsonBrowser getStreamUrl(String url) throws IOException {
4037
var args = new ArrayList<>(List.of(this.sourceManager.getCustomPlaybackArgs()));
4138
args.add(url);
4239
var process = this.sourceManager.getProcess(args);
43-
try (var stream = new BufferedInputStream(process.getInputStream())) {
44-
var data = IOUtils.toString(stream, StandardCharsets.UTF_8);
45-
46-
int exitCode;
47-
try {
48-
exitCode = process.waitFor();
49-
} catch (InterruptedException e) {
50-
Thread.currentThread().interrupt();
51-
throw new IOException("yt-dlp process was interrupted", e);
52-
}
53-
if (exitCode == 0) {
54-
return JsonBrowser.parse(data);
55-
} else {
56-
throw new RuntimeException("Failed to retrieve stream URL, error: " + data);
57-
}
58-
}
40+
return this.sourceManager.getProcessJsonOutput(process);
5941
}
6042

6143
@Override

0 commit comments

Comments
 (0)