1313import java .util .Map ;
1414
1515public 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}
0 commit comments