有没有办法获得youtube视频的.mp3链接?我尝试了多个在线youtube到mp3转换器网站,但它只是在系统中下载文件,并没有提供任何MP3链接。
或
有什么方法可以从链接下载文件,所以我们可以说有一些像www.somesongdownloader.com这样的链接,在浏览器mp3文件中加载此链接后会被下载。但如果我尝试从我的ios代码下载相同的它只是下载php文件而不是mp3文件。下面是我的代码 -
下面的代码适用于mp3链接,我无法获取youtube视频,但此代码不适用于任何在浏览器上下载mp3文件的网址 -
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()
}
}
答案 0 :(得分:1)
根据我的评论中的建议,使用http://www.youtubeinmp3.com/api/可以执行此操作。
let videoURL = "http://www.youtube.com/watch?v=KMU0tzLwhbE"
let url = NSURL(string: "http://www.youtubeinmp3.com/fetch/?format=JSON&video=\(videoURL)")
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 != nil {
do {
let responseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary;
let urlString = responseJSON["link"] as! String
let directDownloadURL = NSURL(string: urlString)
// Call your method loadFileAsync
YourClass.loadFileAsync(directDownloadURL!, completion: { (path, error) -> Void in
print(path)
})
}
catch let JSONError as NSError {
print("\(JSONError)")
}
catch {
print("unknown error in JSON Parsing");
}
}
}
}
}
else {
print("Failure: \(error!.localizedDescription)");
}
})
task.resume()
}
我还没有完成错误处理,因此您需要进一步优化此代码。但这肯定会奏效。我测试了它。