【发布时间】:2016-03-25 09:02:53
【问题描述】:
有没有办法获取 youtube 视频的 .mp3 链接?我尝试了多个在线 youtube 到 mp3 转换器站点,但它们都只是将文件下载到系统中并且没有提供任何 mp3 链接。
或者
有什么方法可以从链接下载文件,所以可以说有一些链接,如 www.somesongdownloader.com,在加载浏览器 mp3 文件中的此链接时正在下载。但是如果我尝试从我的 ios 代码中下载相同的内容,它只是下载 php 文件而不是 mp3 文件。下面是我的代码 -
以下代码适用于我无法为 youtube 视频获取的 mp3 链接,但此代码不适用于任何提供 mp3 文件以在浏览器上下载的 url -
class func loadFileAsync(url: NSURL, completion:(path:String, error:NSError!) -> Void) {
print("Inside loadFileAsync")
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!)
if NSFileManager().fileExistsAtPath(destinationUrl.path!) {
print("file already exists [\(destinationUrl.path!)]")
completion(path: destinationUrl.path!, error:nil)
} else {
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
if (error == nil) {
if let response = response as? NSHTTPURLResponse {
print("response=\(response)")
if response.statusCode == 200 {
if data!.writeToURL(destinationUrl, atomically: true) {
print("file saved [\(destinationUrl.path!)]")
completion(path: destinationUrl.path!, error:error)
} else {
print("error saving file")
let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
completion(path: destinationUrl.path!, error:error)
}
}
}
}
else {
print("Failure: \(error!.localizedDescription)");
completion(path: destinationUrl.path!, error:error)
}
})
task.resume()
}
}
【问题讨论】:
-
您需要获取转换后的mp3的直接下载地址。为此,您必须使用所用 URL 网页中的 HTML(将视频转换为 mp3)。检查此codeproject.com/Tips/587931/YouTube-to-mp .. 这不在 iOS 中,但您可能必须以类似的方式工作。
-
有没有其他方法可以得到转换后的mp3的直接下载链接?
-
不确定,检查一次youtubeinmp3.com/api。
标签: ios swift video youtube mp3