Skip to content

Commit 8278dbd

Browse files
committed
Solve issue where multiple options command failed to execute
1 parent 5bca24e commit 8278dbd

5 files changed

Lines changed: 115 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ YoutubeDL.execute(request);
3535
## YoutubeDLRequest
3636
Represent a command for youtube-dl to execute.
3737
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).
3839

3940
```
4041
// Example to set / get rate limit

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

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@
1313
import java.util.Map;
1414

1515
public class YoutubeDL {
16+
17+
/**
18+
* Youtube-dl executable name
19+
*/
1620
public static final String executableName = "youtube-dl";
1721

22+
/**
23+
* Append executable name to command
24+
* @param command Command string
25+
* @return Command string
26+
*/
1827
private static String buildCommand(String command) {
1928
return String.format("%s %s", executableName, command);
2029
}
2130

31+
/**
32+
* Execute youtube-dl command
33+
* @param request request object
34+
* @return response object
35+
* @throws YoutubeDLException
36+
*/
2237
public static YoutubeDLResponse execute(YoutubeDLRequest request) throws YoutubeDLException {
2338

2439
String command = buildCommand(request.buildOptions());
@@ -61,13 +76,32 @@ public static YoutubeDLResponse execute(YoutubeDLRequest request) throws Youtube
6176
throw new YoutubeDLException(e);
6277
}
6378

79+
String out = outBuffer.toString();
80+
String err = errBuffer.toString();
81+
82+
if(exitCode > 0) {
83+
throw new YoutubeDLException(err);
84+
}
85+
6486
int elapsedTime = (int) ((System.nanoTime() - startTime) / 1000000);
6587

66-
youtubeDLResponse = new YoutubeDLResponse(command, options, directory, exitCode , elapsedTime, outBuffer.toString(), errBuffer.toString());
88+
youtubeDLResponse = new YoutubeDLResponse(command, options, directory, exitCode , elapsedTime, out, err);
6789

6890
return youtubeDLResponse;
6991
}
7092

93+
94+
/**
95+
* Get youtube-dl executable version
96+
* @return version string
97+
* @throws YoutubeDLException
98+
*/
99+
public static String getVersion() throws YoutubeDLException {
100+
YoutubeDLRequest request = new YoutubeDLRequest();
101+
request.setOption("version");
102+
return YoutubeDL.execute(request).getOut();
103+
}
104+
71105
/**
72106
* Retrieve all information available on a video
73107
* @param url Video url
@@ -78,8 +112,8 @@ public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
78112

79113
// Build request
80114
YoutubeDLRequest request = new YoutubeDLRequest(url);
81-
//request.setDumpJson(true);
82115
request.setOption("dump-json");
116+
//request.setOption("no-playlist");
83117
YoutubeDLResponse response = YoutubeDL.execute(request);
84118

85119
// Parse result
@@ -95,21 +129,45 @@ public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
95129
return videoInfo;
96130
}
97131

132+
/**
133+
* List formats
134+
* @param url Video url
135+
* @return list of formats
136+
* @throws YoutubeDLException
137+
*/
98138
public static List<VideoFormat> getFormats(String url) throws YoutubeDLException {
99139
VideoInfo info = getVideoInfo(url);
100140
return info.formats;
101141
}
102142

143+
/**
144+
* List thumbnails
145+
* @param url Video url
146+
* @return list of thumbnail
147+
* @throws YoutubeDLException
148+
*/
103149
public static List<VideoThumbnail> getThumbnails(String url) throws YoutubeDLException {
104150
VideoInfo info = getVideoInfo(url);
105151
return info.thumbnails;
106152
}
107153

154+
/**
155+
* List categories
156+
* @param url Video url
157+
* @return list of category
158+
* @throws YoutubeDLException
159+
*/
108160
public static List<String> getCategories(String url) throws YoutubeDLException {
109161
VideoInfo info = getVideoInfo(url);
110162
return info.categories;
111163
}
112164

165+
/**
166+
* List tags
167+
* @param url Video url
168+
* @return list of tag
169+
* @throws YoutubeDLException
170+
*/
113171
public static List<String> getTags(String url) throws YoutubeDLException {
114172
VideoInfo info = getVideoInfo(url);
115173
return info.tags;
@@ -120,14 +178,25 @@ public static List<String> getTags(String url) throws YoutubeDLException {
120178
return info.subtitles;
121179
}**/
122180

123-
/**public static void d(String url, String dir, String format, int quality, String output) throws YoutubeDLException {
124-
YoutubeDLRequest request = new YoutubeDLRequest(url, dir);
125-
request.setDirectory(dir);
126-
request.setExtractAudio(true);
127-
request.setFormat(format);
128-
request.setAudioQuality(quality);
129-
request.setOutput(output);
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 {
130191

131-
YoutubeDL.execute(request);
132-
}**/
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+
}
133202
}

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

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

3-
import java.lang.reflect.Field;
43
import java.util.*;
54

65
public class YoutubeDLRequest {
@@ -38,6 +37,10 @@ public void setOption(String key, String value) {
3837
options.put(key, value);
3938
}
4039

40+
public void setOption(String key, int value) {
41+
options.put(key, String.valueOf(value));
42+
}
43+
4144
public YoutubeDLRequest() {
4245

4346
}
@@ -70,11 +73,11 @@ protected String buildOptions() {
7073
if(value == null) value = "";
7174

7275
String optionFormatted = String.format("--%s %s", name, value).trim();
73-
builder.append(optionFormatted);
76+
builder.append(optionFormatted + " ");
7477

7578
it.remove();
7679
}
7780

78-
return builder.toString();
81+
return builder.toString().trim();
7982
}
8083
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public void testBuildOptionWithValue() {
2222

2323
Assert.assertEquals("--password 1234", request.buildOptions());
2424
}
25+
26+
@Test
27+
public void testBuildChainOptionWithValue() {
28+
29+
YoutubeDLRequest request = new YoutubeDLRequest();
30+
request.setOption("password", "1234");
31+
request.setOption("username", "1234");
32+
33+
Assert.assertEquals("--username 1234 --password 1234", request.buildOptions());
34+
}
2535
}

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.sapher.youtubedl.mapper.VideoFormat;
44
import com.sapher.youtubedl.mapper.VideoInfo;
5-
import com.sapher.youtubedl.mapper.VideoSubtitle;
65
import com.sapher.youtubedl.mapper.VideoThumbnail;
76
import org.junit.Test;
87
import org.junit.Assert;
@@ -12,17 +11,29 @@
1211
public class YoutubeDLTest {
1312

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

1717
@Test
1818
public void testGetVersion() throws YoutubeDLException {
19+
Assert.assertNotNull(YoutubeDL.getVersion());
20+
}
21+
22+
@Test
23+
public void testElapsedTime() throws YoutubeDLException {
24+
25+
long startTime = System.nanoTime();
1926

2027
YoutubeDLRequest request = new YoutubeDLRequest();
21-
request.setOption("help");
28+
request.setOption("version");
29+
YoutubeDLResponse response = YoutubeDL.execute(request);
30+
31+
int elapsedTime = (int) (System.nanoTime() - startTime);
2232

23-
Assert.assertNotNull(YoutubeDL.execute(request));
33+
Assert.assertTrue(elapsedTime > response.getElapsedTime());
2434
}
2535

36+
2637
@Test
2738
public void testSimulateDownload() throws YoutubeDLException {
2839

@@ -32,7 +43,7 @@ public void testSimulateDownload() throws YoutubeDLException {
3243

3344
YoutubeDLResponse response = YoutubeDL.execute(request);
3445

35-
Assert.assertEquals("youtube-dl https://www.youtube.com/watch?v=9ka5bgHnHyg --simulate", response.getCommand());
46+
Assert.assertEquals("youtube-dl " + videoUrl + " --simulate", response.getCommand());
3647
}
3748

3849
@Test
@@ -81,8 +92,8 @@ public void testGetCategories() throws YoutubeDLException {
8192
}
8293

8394
/**@Test
84-
public void testD() throws YoutubeDLException {
85-
YoutubeDLResponse response = YoutubeDL.d(videoUrl, directory, 151);
95+
public void testDownloadAudio() throws YoutubeDLException {
96+
YoutubeDLResponse response = YoutubeDL.downloadAudio(videoUrl, directory, "17", 0, "%(id)s");
8697
}**/
8798

8899
/**@Test

0 commit comments

Comments
 (0)