首页 > 解决方案 > YouTube 视频下载

问题描述

如何通过输入 URL 下载不同格式的 YouTube 视频?我想将此功能放在我的网站中供我的用户使用。他们可以从 YouTube 下载视频或 MP3。我正在寻找一个 Wordpress 插件。

标签: phpwordpress

解决方案


想想 YouTube dl。这是链接:https ://github.com/ytdl-org/youtube-dl这里有很多机会将此框架与 Java、Python 等一起使用。代码将如下所示:

import java.io.File;
import java.io.IOException;

import com.sapher.youtubedl.YoutubeDL;
import com.sapher.youtubedl.YoutubeDLException;
import com.sapher.youtubedl.YoutubeDLRequest;
import com.sapher.youtubedl.YoutubeDLResponse;

public class TestingFunctions {

    public static void main(String[] args) throws InterruptedException, YoutubeDLException {

            YoutubeDL.setExecutablePath("/usr/local/Cellar/youtube-dl/2017.09.15/bin/youtube-dl");
        try {

            functionTotest("https://www.youtube.com/watch?v=DECxluN8OZM");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void functionTotest(String url) throws IOException, InterruptedException, YoutubeDLException {

        File a = new File("a.mp3");
        if (a.exists()) {
            a.delete();
        }

        // This is the command to run

        String videoUrl = url;
        String directory = System.getProperty("user.dir");

        YoutubeDLRequest request = new YoutubeDLRequest(videoUrl, directory);

        request.setOption("no-mark-watched");
        request.setOption("ignore-errors");
        request.setOption("no-playlist");
        request.setOption("extract-audio");
        request.setOption("audio-format \"mp3\"");
        request.setOption("output \"a.%(ext)s\"");

        YoutubeDLResponse response = YoutubeDL.execute(request);

        String stdOut = response.getOut();

        System.out.println(stdOut);
        System.out.println("File downloaded!");

    }

}

注意:这只是 YouTube dl 的代码示例,我不保证它有效


推荐阅读