Skip to content

Commit 8d92da9

Browse files
committed
Better documentations
1 parent 8278dbd commit 8d92da9

6 files changed

Lines changed: 82 additions & 107 deletions

File tree

README.md

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,34 @@
11
# youtubedl-java [![Build Status](https://travis-ci.org/sapher/youtubedl-java.svg?branch=master)](https://travis-ci.org/sapher/youtubedl-java)
22

3-
4-
Just a simple WIP java wrapper for youtubedl
5-
6-
#Table of contents
7-
8-
- [Prerequisite](#prerequisite)
9-
- [Usage](#usage)
10-
- [Links](#links)
3+
A simple java wrapper for [youtube-dl](https://github.com/rg3/youtube-dl) executable
114

125
# Prerequisite
13-
You need `youtube-dl` to be installed and available. You can follow [those instructions](https://github.com/rg3/youtube-dl/blob/master/README.md#installation) to install `youtube-dl`
14-
15-
# Usage
16-
17-
YoutubeDL wrapper is compose of 3 main class : `YoutubeDL`, `YoutubeDLRequest`, `YoutubeDLResponse`.
18-
19-
## YoutubeDL
20-
Static class that execute a request and return a response.
21-
22-
### Execute a command
23-
24-
```
25-
YoutubeDL.execute('--version');
26-
```
27-
28-
### Execute a request
296

30-
```
31-
YoutubeDLRequest request = new YoutubeDLRequest();
32-
YoutubeDL.execute(request);
33-
```
34-
35-
## YoutubeDLRequest
36-
Represent a command for youtube-dl to execute.
37-
You can **set** and **get** all options handle by youtube-dl : [YoutubeDL Options](https://github.com/rg3/youtube-dl/blob/master/README.md#options).
38-
Don't use those deprecated (you can but.. don't).
7+
Youtube-dl should be installed and available.
398

40-
```
41-
// Example to set / get rate limit
42-
request.setRateLimit(1024);
43-
request.getRateLimit();
44-
```
9+
# Usage
4510

46-
## YoutubeDLResponse
47-
Represent the result of a request.
4811

49-
This object is composed of:
12+
## Make request
5013

51-
* **exitCode** Exit code of youtube-dl programm
52-
* **err** String from stderr
53-
* **out** String from stdout
54-
* **directory** Directory where the programme has been launched
55-
* *more useful to come...*
14+
```java
15+
// Video url to download
16+
String videoUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
5617

57-
## Custom request
18+
// Destination directory
19+
String directory = System.getProperty("user.home");
5820

59-
You can use `YoutubeDLRequest` object to build any command you want.
21+
// Build request
22+
YoutubeDLRequest request = new YoutubeDLRequest(videoUrl, directory);
23+
request.setOption("ignore-errors"); // --ignore-errors
24+
request.setOption("output", "%(id)s"); // --output "%(id)s"
25+
request.setOption("retries", 10); // --retries 10
6026

61-
```java
62-
// Download a video
63-
YoutubeDLRequest request = new YoutubeDLRequest();
64-
request.setUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
65-
request.setDirectory("/Users/johndoe/Desktop");
27+
// Make request and return response
6628
YoutubeDLResponse response = YoutubeDL.execute(request);
6729

68-
// will result to youtube-dl https://www.youtube.com/watch?v=dQw4w9WgXcQ
30+
// Response
31+
String stdOut = response.getOut(); // Executable output
6932
```
70-
71-
Be care, you can build a nonsensical command.
72-
7333
# Links
74-
75-
[YoutubeDL Documentation](https://github.com/rg3/youtube-dl/blob/master/README.md#installation)
76-
77-
**[Back to top](#table-of-contents)**
34+
* [Youtube-dl documentation](https://github.com/sapher/youtubedl-java)

src/main/java/com/sapher/youtubedl/YoutubeDL.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212
import java.util.List;
1313
import java.util.Map;
1414

15+
/**
16+
* <p>Provide an interface for youtube-dl executable</p>
17+
*
18+
* <p>
19+
* For more information on youtube-dl, please see
20+
* <a href="https://github.com/rg3/youtube-dl/blob/master/README.md">YoutubeDL Documentation</a>
21+
* </p>
22+
*/
1523
public class YoutubeDL {
1624

1725
/**
1826
* Youtube-dl executable name
1927
*/
20-
public static final String executableName = "youtube-dl";
28+
protected static final String executableName = "youtube-dl";
2129

2230
/**
2331
* Append executable name to command
2432
* @param command Command string
2533
* @return Command string
2634
*/
27-
private static String buildCommand(String command) {
35+
protected static String buildCommand(String command) {
2836
return String.format("%s %s", executableName, command);
2937
}
3038

3139
/**
32-
* Execute youtube-dl command
40+
* Execute youtube-dl request
3341
* @param request request object
3442
* @return response object
3543
* @throws YoutubeDLException
@@ -113,7 +121,7 @@ public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
113121
// Build request
114122
YoutubeDLRequest request = new YoutubeDLRequest(url);
115123
request.setOption("dump-json");
116-
//request.setOption("no-playlist");
124+
request.setOption("no-playlist");
117125
YoutubeDLResponse response = YoutubeDL.execute(request);
118126

119127
// Parse result
@@ -172,31 +180,4 @@ public static List<String> getTags(String url) throws YoutubeDLException {
172180
VideoInfo info = getVideoInfo(url);
173181
return info.tags;
174182
}
175-
176-
/**public static List<VideoSubtitle> getSubtitles(String url) throws YoutubeDLException {
177-
VideoInfo info = getVideoInfo(url);
178-
return info.subtitles;
179-
}**/
180-
181-
/**
182-
* Download audio of a video
183-
* @param url Video Url
184-
* @param dir Destination directory
185-
* @param format Audio format
186-
* @param quality Audio quality
187-
* @param output Output filename template
188-
* @throws YoutubeDLException
189-
*/
190-
public static YoutubeDLResponse downloadAudio(String url, String dir, String format, int quality, String output) throws YoutubeDLException {
191-
192-
YoutubeDLRequest request = new YoutubeDLRequest(url, dir);
193-
//request.setDirectory(dir);
194-
request.setOption("no-playlist");
195-
//request.setOption("extract-audio");
196-
request.setOption("format", format);
197-
request.setOption("audio-quality", quality);
198-
request.setOption("output", output);
199-
200-
return YoutubeDL.execute(request);
201-
}
202183
}

src/main/java/com/sapher/youtubedl/YoutubeDLException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
package com.sapher.youtubedl;
22

3+
/**
4+
* YoutubeDL Exception
5+
*/
36
public class YoutubeDLException extends Exception {
47

8+
/**
9+
* Exception message
10+
*/
511
private String message;
612

13+
/**
14+
* Construct YoutubeDLException with a message
15+
* @param message
16+
*/
717
public YoutubeDLException(String message) {
818
this.message = message;
919
}
1020

21+
/**
22+
* Construct YoutubeDLException from another exception
23+
* @param e Any exception
24+
*/
1125
public YoutubeDLException(Exception e) {
1226
message = e.getMessage();
1327
}
1428

29+
/**
30+
* Get exception message
31+
* @return exception message
32+
*/
1533
@Override
1634
public String getMessage() {
1735
return message;

src/main/java/com/sapher/youtubedl/YoutubeDLRequest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
import java.util.*;
44

5+
/**
6+
* YoutubeDL request
7+
*/
58
public class YoutubeDLRequest {
69

10+
/**
11+
* Executable working directory
12+
*/
713
private String directory;
14+
15+
/**
16+
* Video Url
17+
*/
818
private String url;
919

20+
/**
21+
* List of executable options
22+
*/
1023
private Map<String, String> options = new HashMap<String, String>();
1124

1225
public String getDirectory() {
@@ -41,19 +54,35 @@ public void setOption(String key, int value) {
4154
options.put(key, String.valueOf(value));
4255
}
4356

57+
/**
58+
* Constructor
59+
*/
4460
public YoutubeDLRequest() {
4561

4662
}
4763

64+
/**
65+
* Construct a request with a videoUrl
66+
* @param url
67+
*/
4868
public YoutubeDLRequest(String url) {
4969
this.url = url;
5070
}
5171

72+
/**
73+
* Construct a request with a videoUrl and working directory
74+
* @param url
75+
* @param directory
76+
*/
5277
public YoutubeDLRequest(String url, String directory) {
5378
this.url = url;
5479
this.directory = directory;
5580
}
5681

82+
/**
83+
* Transform options to a string that the executable will execute
84+
* @return Command string
85+
*/
5786
protected String buildOptions() {
5887

5988
StringBuilder builder = new StringBuilder();

src/main/java/com/sapher/youtubedl/YoutubeDLResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.sapher.youtubedl;
22

3-
43
import java.util.Map;
54

5+
/**
6+
* YoutubeDL response
7+
*/
68
public class YoutubeDLResponse {
79

810
private Map<String, String> options;

src/test/java/com/sapher/youtubedl/YoutubeDLTest.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
public class YoutubeDLTest {
1212

13-
private final String directory = System.getProperty("user.home");
14-
//private final String directory = System.getProperty("java.io.tmpdir");
13+
//private final String directory = System.getProperty("user.home");
14+
private final String directory = System.getProperty("java.io.tmpdir");
1515
private final String videoUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
1616

1717
@Test
@@ -90,16 +90,4 @@ public void testGetCategories() throws YoutubeDLException {
9090
Assert.assertNotNull(categories);
9191
Assert.assertTrue(categories.size() > 0);
9292
}
93-
94-
/**@Test
95-
public void testDownloadAudio() throws YoutubeDLException {
96-
YoutubeDLResponse response = YoutubeDL.downloadAudio(videoUrl, directory, "17", 0, "%(id)s");
97-
}**/
98-
99-
/**@Test
100-
public void testGetSubtitles() throws YoutubeDLException {
101-
List<VideoSubtitle> subtitles = YoutubeDL.getSubtitles(videoUrl);
102-
Assert.assertNotNull(subtitles);
103-
Assert.assertTrue(subtitles.size() > 0);
104-
}**/
10593
}

0 commit comments

Comments
 (0)