Report download progress while downloading.

master
Nekojimi 2 years ago
parent 9fa5e36cdc
commit 3ff544d699
  1. 12
      src/main/java/moe/nekojimi/chords/Chords.java
  2. 37
      src/main/java/moe/nekojimi/chords/Downloader.java
  3. 22
      src/main/java/moe/nekojimi/chords/SongRequest.java

@ -421,6 +421,9 @@ public final class Chords extends ListenerAdapter
private class DownloaderMessageHandler implements BiConsumer<SongRequest, Exception>
{
private static final String PROGRESS_SYMBOLS = " ▏▎▍▌▋▊▉█";
// private static final String PROGRESS_SYMBOLS = " ⠀⡀⣀⣠⣤⣦⣶⣾⣿";
public DownloaderMessageHandler()
{
}
@ -432,7 +435,7 @@ public final class Chords extends ListenerAdapter
// TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
// String bracketNo = "[" + song.getNumber() + "] ";
if (ex == null)
if (song.getLocation() != null)
if (song.getLocation() != null && request.getProgress() >= 100)
{
request.getInvocation().respond("Finished downloading " + song + ", added to queue!");
log("DOWN", "Downloaded " + song);
@ -448,7 +451,12 @@ public final class Chords extends ListenerAdapter
String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k";
formatDetails = " (" + bitFmt + ", " + sizeFmt + ")";
}
request.getInvocation().respond("Now downloading " + song + formatDetails + " ...");
String progressDetails = "";
if (request.getProgress() >= 0)
{
progressDetails = " [" + String.format("%.1f", request.getProgress()) + "%]";
}
request.getInvocation().respond("Now downloading " + song + formatDetails + progressDetails + " ...");
log("DOWN", "Downloading " + song + "...");
}
else

@ -13,6 +13,7 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -38,12 +39,14 @@ public class Downloader implements Consumer<DownloadTask>
{
private static final int DOWNLOAD_TIMEOUT = 300;
private static final int INFO_TIMEOUT = 10;
private static final int INFO_TIMEOUT = 30;
private static final int FORMAT_TIMEOUT = 5;
private static final int BITRATE_TARGET = (int) AudioSendHandler.INPUT_FORMAT.getSampleRate();
private static final Pattern FORMAT_PATTERN = Pattern.compile("^([\\w]+)\\s+([\\w]+)\\s+(\\w+ ?\\w*)\\s+(.*)$");
public static final Pattern DESTINATION_PATTERN = Pattern.compile("Destination: (.*\\.wav)");
public static final Pattern PROGRESS_PATTERN = Pattern.compile("\\[download\\].*?([\\d\\.]+)%");
private static final Pattern ETA_PATTERN = Pattern.compile("\\[download\\].*?ETA\\s+(\\d{1,2}:\\d{2})");
private final List<DownloadTask> downloadQueue = new LinkedList<>();
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
@ -258,19 +261,39 @@ public class Downloader implements Consumer<DownloadTask>
+ " -f " + formatCodes + "worstaudio/bestaudio/worst/best"
+ " --audio-format=wav"
+ " --no-playlist"
+ " --extractor-args youtube:player_client=android"
+ " -o " + getDownloadDir().getAbsolutePath() + "/%(title)s.%(ext)s "
+ song.getUrl().toString();
Process exec = runCommand(cmd, DOWNLOAD_TIMEOUT);
InputStream in = exec.getInputStream();
String output = new String(in.readAllBytes(), Charset.defaultCharset());
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println(line);
Matcher progMatcher = PROGRESS_PATTERN.matcher(line);
if (progMatcher.find())
{
task.request.setProgress(Double.parseDouble(progMatcher.group(1)));
messageHandler.accept(task.request, null);
}
Matcher destMatcher = DESTINATION_PATTERN.matcher(line);
if (destMatcher.find())
song.setLocation(new File(destMatcher.group(1)));
}
// String output = new String(in.readAllBytes(), Charset.defaultCharset());
String error = new String(exec.getErrorStream().readAllBytes(), Charset.defaultCharset());
System.out.println(output);
Matcher matcher = DESTINATION_PATTERN.matcher(output);
if (matcher.find())
song.setLocation(new File(matcher.group(1)));
else if (exec.exitValue() != 0)
// System.out.println(output);
if (exec.exitValue() != 0)
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue() + ", output:\n" + error);
task.request.setProgress(100);
// return true;
if (task.getDestination() != null)

@ -38,6 +38,8 @@ public class SongRequest
private Song song;
private double progress = -1;
private double eta = -1;
public List<Result> getSearchResults()
@ -120,4 +122,24 @@ public class SongRequest
this.song = song;
}
public double getProgress()
{
return progress;
}
public void setProgress(double progress)
{
this.progress = progress;
}
public double getEta()
{
return eta;
}
public void setEta(double eta)
{
this.eta = eta;
}
}

Loading…
Cancel
Save