Created
January 25, 2015 18:58
-
-
Save davinkevin/d89c166de465b813bc8d to your computer and use it in GitHub Desktop.
Test to download youtube video with vget 1.1.22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lan.dk.podcastserver.worker.downloader; | |
import com.github.axet.vget.VGet; | |
import com.github.axet.vget.info.VGetParser; | |
import com.github.axet.vget.info.VideoInfo; | |
import com.github.axet.vget.vhs.VimeoInfo; | |
import com.github.axet.vget.vhs.YoutubeInfo; | |
import com.github.axet.wget.info.DownloadInfo; | |
import org.junit.Test; | |
import java.io.File; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.List; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
/** | |
* Created by kevin on 25/01/15. | |
*/ | |
public class vGetTest { | |
// http://www.youtube.com/watch?v=d9bhgNmuzFg&feature=youtube_gdata | |
@Test | |
public void should_download_video () throws MalformedURLException { | |
download("http://www.youtube.com/watch?v=d9bhgNmuzFg&feature=youtube_gdata", new File("/tmp/test.mp4")); | |
} | |
VideoInfo info; | |
long last; | |
public void download(String url, File path) { | |
try { | |
AtomicBoolean stop = new AtomicBoolean(false); | |
Runnable notify = () -> { | |
VideoInfo i1 = info; | |
DownloadInfo i2 = i1.getInfo(); | |
// notify app or save download state | |
// you can extract information from DownloadInfo info; | |
switch (i1.getState()) { | |
case EXTRACTING: | |
case EXTRACTING_DONE: | |
case DONE: | |
if (i1 instanceof YoutubeInfo) { | |
YoutubeInfo i = (YoutubeInfo) i1; | |
System.out.println(i1.getState() + " " + i.getVideoQuality()); | |
} else if (i1 instanceof VimeoInfo) { | |
VimeoInfo i = (VimeoInfo) i1; | |
System.out.println(i1.getState() + " " + i.getVideoQuality()); | |
} else { | |
System.out.println("downloading unknown quality"); | |
} | |
break; | |
case RETRYING: | |
System.out.println(i1.getState() + " " + i1.getDelay()); | |
break; | |
case DOWNLOADING: | |
long now = System.currentTimeMillis(); | |
if (now - 1000 > last) { | |
last = now; | |
String parts = ""; | |
List<DownloadInfo.Part> pp = i2.getParts(); | |
if (pp != null) { | |
// multipart download | |
for (DownloadInfo.Part p : pp) { | |
if (p.getState().equals(DownloadInfo.Part.States.DOWNLOADING)) { | |
parts += String.format("Part#%d(%.2f) ", p.getNumber(), p.getCount() | |
/ (float) p.getLength()); | |
} | |
} | |
} | |
System.out.println(String.format("%s %.2f %s", i1.getState(), | |
i2.getCount() / (float) i2.getLength(), parts)); | |
} | |
break; | |
default: | |
break; | |
} | |
}; | |
URL web = new URL(url); | |
// [OPTIONAL] limit maximum quality, or do not call this function if | |
// you wish maximum quality available. | |
// | |
// if youtube does not have video with requested quality, program | |
// will raise en exception. | |
VGetParser user = null; | |
// create proper html parser depends on url | |
user = VGet.parser(web); | |
// download maximum video quality from youtube | |
//user = new YouTubeQParser(YoutubeQuality.p480); | |
// download mp4 format only, fail if non exist | |
// user = new YouTubeMPGParser(); | |
// create proper videoinfo to keep specific video information | |
info = user.info(web); | |
VGet v = new VGet(info, path); | |
// [OPTIONAL] call v.extract() only if you d like to get video title | |
// or download url link | |
// before start download. or just skip it. | |
v.extract(user, stop, notify); | |
System.out.println("Title: " + info.getTitle()); | |
System.out.println("Download URL: " + info.getInfo().getSource()); | |
v.download(user, stop, notify); | |
} catch (RuntimeException e) { | |
throw e; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment