Skip to content

Commit b0059cf

Browse files
committed
Add parsing of video informations
1 parent d5f4a4b commit b0059cf

9 files changed

Lines changed: 415 additions & 7 deletions

File tree

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.sapher.youtubedl;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.sapher.youtubedl.mapper.VideoFormat;
5+
import com.sapher.youtubedl.mapper.VideoInfo;
6+
import com.sapher.youtubedl.mapper.VideoSubtitle;
7+
import com.sapher.youtubedl.mapper.VideoThumbnail;
38
import com.sapher.youtubedl.utils.StreamGobbler;
49

510
import java.io.File;
611
import java.io.IOException;
712
import java.io.InputStream;
13+
import java.util.List;
814

915
public class YoutubeDL {
1016
public static final String executableName = "youtube-dl";
1117

12-
public static String buildCommand(String command) {
18+
private static String buildCommand(String command) {
1319
return String.format("%s %s", executableName, command);
1420
}
1521

@@ -20,6 +26,7 @@ public static YoutubeDLResponse execute(String command, String directory) throws
2026
int exitCode;
2127
StringBuffer outBuffer = new StringBuffer();
2228
StringBuffer errBuffer = new StringBuffer();
29+
long startTime = System.nanoTime();
2330

2431
// TODO A nice place to break everything
2532
String[] split = command.split(" ");
@@ -50,7 +57,9 @@ public static YoutubeDLResponse execute(String command, String directory) throws
5057
throw new YoutubeDLException(e);
5158
}
5259

53-
youtubeDLResponse = new YoutubeDLResponse(command, directory, exitCode, outBuffer.toString(), errBuffer.toString());
60+
int elapsedTime = (int) ((System.nanoTime() - startTime) / 1000000);
61+
62+
youtubeDLResponse = new YoutubeDLResponse(command, directory, exitCode , elapsedTime, outBuffer.toString(), errBuffer.toString());
5463

5564
return youtubeDLResponse;
5665
}
@@ -59,4 +68,66 @@ public static YoutubeDLResponse execute(YoutubeDLRequest request) throws Youtube
5968
String command = request.buildOptions();
6069
return execute(buildCommand(command), request.getDirectory());
6170
}
71+
72+
/**
73+
* Retrieve all information available on a video
74+
* @param url Video url
75+
* @return Video info
76+
* @throws YoutubeDLException
77+
*/
78+
public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
79+
80+
// Build request
81+
YoutubeDLRequest request = new YoutubeDLRequest(url);
82+
request.setDumpJson(true);
83+
YoutubeDLResponse response = YoutubeDL.execute(request);
84+
85+
// Parse result
86+
ObjectMapper objectMapper = new ObjectMapper();
87+
VideoInfo videoInfo = null;
88+
89+
try {
90+
videoInfo = objectMapper.readValue(response.getOut(), VideoInfo.class);
91+
} catch (IOException e) {
92+
throw new YoutubeDLException("Unable to parse video information: " + e.getMessage());
93+
}
94+
95+
return videoInfo;
96+
}
97+
98+
public static List<VideoFormat> getFormats(String url) throws YoutubeDLException {
99+
VideoInfo info = getVideoInfo(url);
100+
return info.formats;
101+
}
102+
103+
public static List<VideoThumbnail> getThumbnails(String url) throws YoutubeDLException {
104+
VideoInfo info = getVideoInfo(url);
105+
return info.thumbnails;
106+
}
107+
108+
public static List<String> getCategories(String url) throws YoutubeDLException {
109+
VideoInfo info = getVideoInfo(url);
110+
return info.categories;
111+
}
112+
113+
public static List<String> getTags(String url) throws YoutubeDLException {
114+
VideoInfo info = getVideoInfo(url);
115+
return info.tags;
116+
}
117+
118+
/**public static List<VideoSubtitle> getSubtitles(String url) throws YoutubeDLException {
119+
VideoInfo info = getVideoInfo(url);
120+
return info.subtitles;
121+
}**/
122+
123+
/**public static void d(String url, String dir, String format, int quality, String output) throws YoutubeDLException {
124+
YoutubeDLRequest request = new YoutubeDLRequest(dir, url);
125+
request.setDirectory(dir);
126+
request.setExtractAudio(true);
127+
request.setFormat(format);
128+
request.setAudioQuality(quality);
129+
request.setOutput(output);
130+
131+
YoutubeDL.execute(request);
132+
}**/
62133
}

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

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class YoutubeDLRequest {
9090
private Boolean noCacheDir;
9191
private Boolean rmCacheDir;
9292

93-
// Thumbnail Options
93+
// VideoThumbnail Options
9494
private Boolean writeThumbnail;
9595
private Boolean writeAllThumbnails;
9696
private Boolean listThumbnails;
@@ -154,9 +154,33 @@ public class YoutubeDLRequest {
154154
private Boolean netRc;
155155
private String videoPassword;
156156

157+
// Postprocessing Options
158+
private Boolean extractAudio;
159+
private Integer audioFormat;
160+
private Integer audioQuality;
161+
private String recodeVideo;
162+
private String postprocessorArgs;
163+
private Boolean keepAudio;
164+
private Boolean noPostOverwrites;
165+
private Boolean embedSubs;
166+
private Boolean embedThumbnail;
167+
private Boolean addMetadata;
168+
private Boolean metadataFromTitle;
169+
private Boolean xattrs;
170+
private String fixup;
171+
private Boolean preferAvconv;
172+
private Boolean preferFfmpeg;
173+
private String ffmpegLocation;
174+
private String exec;
175+
private String convertSubs;
176+
157177
public YoutubeDLRequest() {
158178
}
159179

180+
public YoutubeDLRequest(String url) {
181+
this.url = url;
182+
}
183+
160184
public YoutubeDLRequest(String directory, String url) {
161185
this.directory = directory;
162186
this.url = url;
@@ -1170,6 +1194,150 @@ public void setVideoPassword(String videoPassword) {
11701194
this.videoPassword = videoPassword;
11711195
}
11721196

1197+
public Boolean getExtractAudio() {
1198+
return extractAudio;
1199+
}
1200+
1201+
public void setExtractAudio(Boolean extractAudio) {
1202+
this.extractAudio = extractAudio;
1203+
}
1204+
1205+
public Integer getAudioFormat() {
1206+
return audioFormat;
1207+
}
1208+
1209+
public void setAudioFormat(Integer audioFormat) {
1210+
this.audioFormat = audioFormat;
1211+
}
1212+
1213+
public Integer getAudioQuality() {
1214+
return audioQuality;
1215+
}
1216+
1217+
public void setAudioQuality(Integer audioQuality) {
1218+
this.audioQuality = audioQuality;
1219+
}
1220+
1221+
public String getRecodeVideo() {
1222+
return recodeVideo;
1223+
}
1224+
1225+
public void setRecodeVideo(String recodeVideo) {
1226+
this.recodeVideo = recodeVideo;
1227+
}
1228+
1229+
public String getPostprocessorArgs() {
1230+
return postprocessorArgs;
1231+
}
1232+
1233+
public void setPostprocessorArgs(String postprocessorArgs) {
1234+
this.postprocessorArgs = postprocessorArgs;
1235+
}
1236+
1237+
public Boolean getKeepAudio() {
1238+
return keepAudio;
1239+
}
1240+
1241+
public void setKeepAudio(Boolean keepAudio) {
1242+
this.keepAudio = keepAudio;
1243+
}
1244+
1245+
public Boolean getNoPostOverwrites() {
1246+
return noPostOverwrites;
1247+
}
1248+
1249+
public void setNoPostOverwrites(Boolean noPostOverwrites) {
1250+
this.noPostOverwrites = noPostOverwrites;
1251+
}
1252+
1253+
public Boolean getEmbedSubs() {
1254+
return embedSubs;
1255+
}
1256+
1257+
public void setEmbedSubs(Boolean embedSubs) {
1258+
this.embedSubs = embedSubs;
1259+
}
1260+
1261+
public Boolean getEmbedThumbnail() {
1262+
return embedThumbnail;
1263+
}
1264+
1265+
public void setEmbedThumbnail(Boolean embedThumbnail) {
1266+
this.embedThumbnail = embedThumbnail;
1267+
}
1268+
1269+
public Boolean getAddMetadata() {
1270+
return addMetadata;
1271+
}
1272+
1273+
public void setAddMetadata(Boolean addMetadata) {
1274+
this.addMetadata = addMetadata;
1275+
}
1276+
1277+
public Boolean getMetadataFromTitle() {
1278+
return metadataFromTitle;
1279+
}
1280+
1281+
public void setMetadataFromTitle(Boolean metadataFromTitle) {
1282+
this.metadataFromTitle = metadataFromTitle;
1283+
}
1284+
1285+
public Boolean getXattrs() {
1286+
return xattrs;
1287+
}
1288+
1289+
public void setXattrs(Boolean xattrs) {
1290+
this.xattrs = xattrs;
1291+
}
1292+
1293+
public String getFixup() {
1294+
return fixup;
1295+
}
1296+
1297+
public void setFixup(String fixup) {
1298+
this.fixup = fixup;
1299+
}
1300+
1301+
public Boolean getPreferAvconv() {
1302+
return preferAvconv;
1303+
}
1304+
1305+
public void setPreferAvconv(Boolean preferAvconv) {
1306+
this.preferAvconv = preferAvconv;
1307+
}
1308+
1309+
public Boolean getPreferFfmpeg() {
1310+
return preferFfmpeg;
1311+
}
1312+
1313+
public void setPreferFfmpeg(Boolean preferFfmpeg) {
1314+
this.preferFfmpeg = preferFfmpeg;
1315+
}
1316+
1317+
public String getFfmpegLocation() {
1318+
return ffmpegLocation;
1319+
}
1320+
1321+
public void setFfmpegLocation(String ffmpegLocation) {
1322+
this.ffmpegLocation = ffmpegLocation;
1323+
}
1324+
1325+
public String getExec() {
1326+
return exec;
1327+
}
1328+
1329+
public void setExec(String exec) {
1330+
this.exec = exec;
1331+
}
1332+
1333+
public String getConvertSubs() {
1334+
return convertSubs;
1335+
}
1336+
1337+
public void setConvertSubs(String convertSubs) {
1338+
this.convertSubs = convertSubs;
1339+
}
1340+
11731341
protected String buildOptions() {
11741342

11751343
Map<String, String> options = new HashMap<String, String>();

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ public class YoutubeDLResponse {
88
private int exitCode;
99
private String out;
1010
private String err;
11-
private List<String> filePaths;
11+
/**private List<String> filePaths;
12+
private List<String> thumbnailPaths;
13+
private List<String> subtitlePaths;**/
1214
private String directory;
15+
private int elapsedTime;
1316

14-
public YoutubeDLResponse(String command, String directory, int exitCode, String out, String err) {
17+
public YoutubeDLResponse(String command, String directory, int exitCode, int elapsedTime, String out, String err) {
1518
this.command = command;
1619
this.directory = directory;
20+
this.elapsedTime = elapsedTime;
1721
this.exitCode = exitCode;
1822
this.out = out;
1923
this.err = err;
@@ -35,11 +39,15 @@ public String getErr() {
3539
return err;
3640
}
3741

38-
public List<String> getFilePaths() {
42+
/*public List<String> getFilePaths() {
3943
return filePaths;
40-
}
44+
}*/
4145

4246
public String getDirectory() {
4347
return directory;
4448
}
49+
50+
public int getElapsedTime() {
51+
return elapsedTime;
52+
}
4553
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.sapher.youtubedl.mapper;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
public class HttpHeader {
8+
9+
@JsonProperty("Accept-Charset")
10+
public String acceptCharset;
11+
@JsonProperty("Accept-Language")
12+
public String acceptLanguage;
13+
@JsonProperty("Accept-Encoding")
14+
public String acceptEncoding;
15+
@JsonProperty("Accept")
16+
public String accept;
17+
@JsonProperty("User-Agent")
18+
public String userAgent;
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sapher.youtubedl.mapper;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
public class VideoFormat {
8+
9+
public int asr;
10+
public int tbr;
11+
public int abr;
12+
public String format;
13+
@JsonProperty("format_id")
14+
public String formatId;
15+
@JsonProperty("format_note")
16+
public String formatNote;
17+
public String ext;
18+
public int preference;
19+
public String vcodec;
20+
public String acodec;
21+
public int width;
22+
public int height;
23+
public int filesize;
24+
public int fps;
25+
public String url;
26+
}

0 commit comments

Comments
 (0)