php - YouTube 视频下载
问题描述
如何通过输入 URL 下载不同格式的 YouTube 视频?我想将此功能放在我的网站中供我的用户使用。他们可以从 YouTube 下载视频或 MP3。我正在寻找一个 Wordpress 插件。
解决方案
想想 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 的代码示例,我不保证它有效
推荐阅读
- reactjs - 反应选择特定的参考
- python - 如何摆脱被空白空间包围的线条?
- python - 是否可以将 django 模板标签值(如 lorem)存储在变量中?
- reactjs - 将整个应用程序包装在单个 ErrorBoundary (react-error-boundary) 中
- python - python mysql连接器将不必要的精度值添加到具有整数值的浮点数据类型单元格
- node.js - 节点应用程序构建成功但未显示在 Heroku 上(MEAN Stack 应用程序)
- r - R. 删除分隔符之间的所有内容
- java - 通过 Java 套接字发送多个文件
- ios - 将 UIImageView 旋转 90 度 - Swift
- jsx - Vue 3 中的无渲染组件