ath*_*r21 1 youtube ios swift swift4
我正在尝试在手机上下载 YouTube 视频,以便用户稍后可以离线播放。我在用户可以下载的地方添加了一个按钮。我当前如何下载视频的代码如下。
@objc func downloadSelectedVideo() {
if let audioUrl = URL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") {
// create your document folder url
let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
// your destination file url
let destination = documentsUrl.appendingPathComponent(audioUrl.lastPathComponent)
print(destination)
// check if it exists before downloading it
if FileManager().fileExists(atPath: destination.path) {
print("The file already exists at path")
} else {
// if the file doesn't exist
// just download the data from your url
URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) in
// after downloading your data you need to save it to your destination url
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("audio"),
let location = location, error == nil
else { return }
do {
try FileManager.default.moveItem(at: location, to: destination)
print("file saved")
} catch {
print(error)
}
}).resume()
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我已经将 URL 连接到了 mp3 格式的免费音乐,并且工作正常我可以下载音乐,一切都工作得很好,但是当我尝试连接 YouTube 视频时,它永远不会打印出来声明文件已保存 我尝试将此作为 URLif let audioUrl = URL(string: "https://www.youtube.com/watch?v=3WSgJCYIewM")
但保存的打印语句文件从未运行,但是当我尝试使用我之前提到的其他文件时,它保存了打印文件。
应该使用什么 URL 来下载 YouTube 视频,我是否必须使用 mp3 或 mp4 源来下载视频。我尽量不使用任何第三方网站,如果您能提出任何解决方案,那将是伟大且有帮助的。谢谢
YouTube 非常明确地说明了这一点,你不能这样做。
在E. 处理 YouTube 数据和内容下找到此内容
您和您的 API 客户端不得且不得鼓励、允许或要求他人:
未经 YouTube 事先书面批准,下载、导入、备份、缓存或存储 YouTube 视听内容的副本,
IANAL,但我不想与他们的法律团队对抗。
| 归档时间: |
|
| 查看次数: |
3134 次 |
| 最近记录: |