Skip to content

Commit 063750a

Browse files
committed
Added progress bar
1 parent 24fce9f commit 063750a

2 files changed

Lines changed: 62 additions & 30 deletions

File tree

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,22 @@ You must only get one stream to be able to download it. You can use the methods:
4848

4949
You can also manually select the stream using `.get("index")`.
5050

51-
The download() method must receive the path that the stream will be downloaded.
52-
5351
```java
5452
public static void main(String[] args) throws Exception {
5553
Youtube yt = new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo");
56-
yt.streams().getHighestResolution().download("./");
54+
yt.streams().getHighestResolution().download();
5755
}
5856
```
5957

6058
or
6159

6260
```java
6361
public static void main(String[] args) throws Exception {
64-
new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo").streams().get(1).download("./");
62+
new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo").streams().get(1).download();
6563
}
6664
}
6765
```
66+
You can define the path where the file will be saved with the `.download("YOUR/PATH")` method
6867

6968
### Downloading videos with multiple audio tracks
7069
Videos with multiple progressive audio tracks come with the original audio, which is why we must choose the adaptive types.
@@ -102,22 +101,27 @@ public static void main(String[] args) throws Exception {
102101
filters.put("progressive", "true");
103102
filters.put("subType", "mp4");
104103

105-
yt.streams().filter(filters).getFirst().download("./");
104+
yt.streams().filter(filters).getFirst().download();
106105

107106
}
108107
```
109108

110109
### Download with callback function
111110

112-
If no parameter is passed, a download percentage string will be printed to the terminal
111+
If no parameters are passed, a progress bar will be printed to the terminal.
112+
113+
```
114+
-|############################################| 100,0%
115+
```
116+
113117
```java
114-
public static void progress(Long value){
115-
System.out.println(value);
118+
public static void progress(long bytesReceived, long fileSize){
119+
System.out.println((bytesReceived * 100) / fileSize);
116120
}
117121

118122
public static void main(String[] args) throws Exception {
119123
Youtube yt = new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo");
120-
yt.streams().getHighestResolution().download("./", Download::progress);
124+
yt.streams().getHighestResolution().download(Download::progress);
121125
}
122126
```
123127

@@ -128,7 +132,7 @@ The `getVideos()` method will return an ArrayList with the links extracted from
128132
```java
129133
public static void main(String[] args) throws Exception {
130134
for(String pl : new Playlist("https://www.youtube.com/playlist?list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al").getVideos()){
131-
new Youtube(pl).streams().getHighestResolution().download("./");
135+
new Youtube(pl).streams().getHighestResolution().download();
132136
}
133137
}
134138
```
@@ -239,7 +243,7 @@ Download it in .srt format (if the .srt format is not informed, the xml will be
239243

240244
```java
241245
public static void main(String[] args) throws Exception {
242-
new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo&t=1s").getCaptions().getByCode("en").download("caption.srt", "./")
246+
new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo&t=1s").getCaptions().getByCode("en").download("caption.srt", "./");
243247
}
244248
```
245249

@@ -266,7 +270,7 @@ This process involves manually obtaining a PO token generated from YouTube in a
266270
```java
267271
public static void main(String[] args) throws Exception {
268272
Youtube yt = new Youtube("https://www.youtube.com/watch?v=2lAe1cqCOXo", true);
269-
yt.streams().getHighestResolution().download("./");
273+
yt.streams().getHighestResolution().download();
270274
}
271275
```
272276
The terminal will ask you to insert the tokens.

src/main/java/com/github/felipeucelli/javatube/Stream.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.*;
88
import java.net.*;
99
import java.util.*;
10-
import java.util.function.Consumer;
10+
import java.util.function.BiConsumer;
1111
import java.util.regex.Matcher;
1212
import java.util.regex.Pattern;
1313
import static java.lang.Math.min;
@@ -171,29 +171,61 @@ private void checkFile(String filePath) throws IOException {
171171
}
172172
}
173173

174-
public static void onProgress(long value){
175-
System.out.println(value + "%");
174+
private static void displayProgressBar(long bytesReceived, long fileSize, char ch, double scale) {
175+
int columns = getTerminalWidth();
176+
int maxWidth = (int) (columns * scale);
177+
178+
int filled = (int) Math.round(maxWidth * ((double) bytesReceived / fileSize));
179+
int remaining = maxWidth - filled;
180+
181+
String bar = String.valueOf(ch).repeat(Math.max(0, filled)) +
182+
" ".repeat(Math.max(0, remaining));
183+
184+
double percent = 100.0 * bytesReceived / fileSize;
185+
186+
PrintStream out = System.out;
187+
out.printf("-|%s| %.1f%%\r", bar, percent);
188+
out.flush();
189+
}
190+
191+
private static void displayProgressBar(long bytesReceived, long fileSize) {
192+
displayProgressBar(bytesReceived, fileSize, '#', 0.55);
193+
}
194+
195+
private static int getTerminalWidth() {
196+
return 80;
197+
}
198+
199+
public void download() throws Exception {
200+
download("./", title, Stream::displayProgressBar);
176201
}
202+
177203
public void download(String path) throws Exception {
178-
startDownload(path, title, Stream::onProgress);
204+
download(path, title, Stream::displayProgressBar);
179205
}
180-
public void download(String path, Consumer<Long> progress) throws Exception {
181-
startDownload(path, title, progress);
206+
207+
public void download(BiConsumer<Long, Long> progress) throws Exception {
208+
download("./", title, progress);
209+
}
210+
211+
public void download(String path, BiConsumer<Long, Long> progress) throws Exception {
212+
download(path, title, progress);
182213
}
214+
183215
public void download(String path, String fileName) throws Exception {
184-
startDownload(path, fileName, Stream::onProgress);
216+
download(path, fileName, Stream::displayProgressBar);
185217
}
186-
public void download(String path, String fileName, Consumer<Long> progress) throws Exception {
218+
219+
public void download(String path, String fileName, BiConsumer<Long, Long> progress) throws Exception {
187220
startDownload(path, fileName, progress);
188221
}
189-
private void startDownload(String path, String fileName, Consumer<Long> progress) throws Exception {
222+
223+
private void startDownload(String path, String fileName, BiConsumer<Long, Long> progress) throws Exception {
190224
String savePath = path + safeFileName(fileName) + "." + subType;
191225
if(!isOtf){
192226
long startSize = 0;
193227
long stopPos;
194228
int defaultRange = 1048576;
195-
long progressPercentage;
196-
long lastPrintedProgress = 0;
197229
byte[] chunkReceived;
198230

199231
checkFile(savePath);
@@ -205,12 +237,8 @@ private void startDownload(String path, String fileName, Consumer<Long> progress
205237
String chunk = url + "&range=" + startSize + "-" + stopPos;
206238
chunkReceived = Request.get(chunk).toByteArray();
207239

208-
progressPercentage = (stopPos * 100L) / (fileSize);
240+
progress.accept(stopPos, fileSize);
209241

210-
if (progressPercentage != lastPrintedProgress) {
211-
lastPrintedProgress = progressPercentage;
212-
progress.accept(progressPercentage);
213-
}
214242
startSize = startSize + chunkReceived.length;
215243
try (FileOutputStream fos = new FileOutputStream(savePath, true)) {
216244
fos.write(chunkReceived);
@@ -221,7 +249,7 @@ private void startDownload(String path, String fileName, Consumer<Long> progress
221249
}
222250
}
223251

224-
private void downloadOtf(String savePath, Consumer<Long> progress) throws Exception {
252+
private void downloadOtf(String savePath, BiConsumer<Long, Long> progress) throws Exception {
225253
int countChunk = 0;
226254
byte[] chunkReceived;
227255
int lastChunk = 0;
@@ -241,7 +269,7 @@ private void downloadOtf(String savePath, Consumer<Long> progress) throws Except
241269
throw new RegexMatchError("downloadOtf: " + pattern);
242270
}
243271
}
244-
progress.accept((countChunk * 100L) / (lastChunk));
272+
progress.accept(Long.parseLong(String.valueOf(countChunk)), Long.parseLong(String.valueOf(lastChunk)));
245273
countChunk = countChunk + 1;
246274
try (FileOutputStream fos = new FileOutputStream(savePath, true)) {
247275
fos.write(chunkReceived);

0 commit comments

Comments
 (0)