Search in sources :

Example 1 with YoutubeDLRequest

use of com.yausername.youtubedl_android.YoutubeDLRequest in project MPlayer by abh80.

the class Function2 method download.

public static void download(String directory, String id, Function2<Float, Boolean, Void> callback) {
    System.out.println(id);
    Thread t = new Thread(() -> {
        YoutubeDLRequest request = new YoutubeDLRequest("https://youtube.com/watch?v=" + id);
        request.addOption("-o", directory + "/" + id + ".mp3");
        request.addOption("--extract-audio");
        request.addOption("--audio-format", "mp3");
        try {
            YoutubeDL.getInstance().execute(request, (progress, etaInSeconds, line) -> {
                System.out.println(line);
                callback.apply(progress, false);
            });
        } catch (YoutubeDLException | InterruptedException e) {
            e.printStackTrace();
        }
    });
    t.start();
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (!t.isAlive()) {
                callback.apply(100f, true);
                timer.cancel();
            }
        }
    }, 0, 2000);
}
Also used : YoutubeDLException(com.yausername.youtubedl_android.YoutubeDLException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) YoutubeDLRequest(com.yausername.youtubedl_android.YoutubeDLRequest)

Example 2 with YoutubeDLRequest

use of com.yausername.youtubedl_android.YoutubeDLRequest in project androidtv-news by anenasa.

the class Channel method parse.

void parse() throws JSONException, IOException, YoutubeDLException, InterruptedException {
    String url = getUrl();
    YoutubeDLRequest request;
    if (url.startsWith("https://hamivideo.hinet.net/channel/") && url.endsWith(".do")) {
        String id = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
        OkHttpClient okHttpClient = new OkHttpClient();
        Request okHttpRequest = new Request.Builder().url("https://hamivideo.hinet.net/api/play.do?freeProduct=1&id=" + id).build();
        Response response = okHttpClient.newCall(okHttpRequest).execute();
        JSONObject object = new JSONObject(response.body().string());
        request = new YoutubeDLRequest(object.getString("url"));
    } else {
        request = new YoutubeDLRequest(url);
    }
    request.addOption("-f", getFormat());
    if (!getHeader().isEmpty()) {
        String[] headers = getHeader().split("\\\\r\\\\n");
        for (String header : headers) {
            request.addOption("--add-header", header);
        }
    }
    VideoInfo streamInfo = YoutubeDL.getInstance().getInfo(request);
    setVideo(streamInfo.getUrl());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Request(okhttp3.Request) YoutubeDLRequest(com.yausername.youtubedl_android.YoutubeDLRequest) VideoInfo(com.yausername.youtubedl_android.mapper.VideoInfo) YoutubeDLRequest(com.yausername.youtubedl_android.YoutubeDLRequest)

Aggregations

YoutubeDLRequest (com.yausername.youtubedl_android.YoutubeDLRequest)2 YoutubeDLException (com.yausername.youtubedl_android.YoutubeDLException)1 VideoInfo (com.yausername.youtubedl_android.mapper.VideoInfo)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 JSONObject (org.json.JSONObject)1