在iOS Swift中以mp3格式下载youtube视频
有什么方法可以获取YouTube视频的.mp3链接吗?我尝试了多个在线youtube到mp3转换器的站点,但它们都只是在系统中下载文件,并且没有提供任何mp3链接.
Is there any way to get .mp3 links for youtube video's? I tried multiple online youtube to mp3 converter sites but it all just downloads the file in system and doesn't give any mp3 link.
或
有什么方法可以从链接下载文件,所以可以说有一些链接,例如www.somesongdownloader.com,该链接在浏览器mp3文件中的下载正在下载.但是,如果我尝试从ios代码下载相同的内容,则只需下载php文件而不是mp3文件即可.下面是我的代码-
Is there any way i can download files from a link, so lets say there is some link like www.somesongdownloader.com, on load of this link in browser mp3 file is getting downloaded. but if i try to download the same from my ios code its just downloading the php file not mp3 file. below is my code -
下面的代码适用于mp3链接,但我无法将其用于youtube视频,但是此代码不适用于允许mp3文件在浏览器上下载的任何网址-
Below code works fine for mp3 links which i am not able to get for youtube videos but this code is not working for any url which gives mp3 file to download on browser -
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()
}
}
如我的评论所建议,使用 http: //www.youtubeinmp3.com/api/您可以执行此操作.
As suggested in my comments, using http://www.youtubeinmp3.com/api/ you can do this.
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()
}
我还没有完成错误处理,因此您需要进一步优化此代码.但这肯定会起作用.我已经测试过了.
I have not done error handling, so you need to further refine this code. But this will surely work. I have tested it.