在iOS Swift中以mp3格式下载YouTubevideo

有没有办法让youtubevideo的.mp3链接? 我尝试了多个在线youtube到MP3转换器的网站,但它只是在系统中下载文件,并没有给任何MP3链接。

要么

有什么办法可以从链接下载文件,所以可以说有一些链接,如www.somesongdownloader.com,在浏览器中加载此链接mp3文件正在下载。 但如果我尝试从我的ios代码下载相同的只是下载的PHP文件不是MP3文件。 下面是我的代码 –

下面的代码工作正常的MP3链接,我无法获得youtubevideo,但这段代码是不工作的任何url,让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() } } 

正如我的意见所build议的,使用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() } 

我还没有做error handling,所以你需要进一步优化这个代码。 但是这肯定会奏效。 我已经testing过了。