在iOS Swift中下载mp3格式的youtube视频

在iOS Swift中下载mp3格式的youtube视频,ios,swift,video,youtube,mp3,Ios,Swift,Video,Youtube,Mp3,有没有办法获得youtube视频的.mp3链接?我尝试了多个在线youtube-to-mp3转换器站点,但都只是在系统中下载文件,没有提供任何mp3链接 或 有什么方法可以从链接下载文件,比如说有一些链接像www.somesongdownloader.com,在浏览器中加载这个链接时,mp3文件被下载。但是如果我试着从我的ios代码中下载相同的内容,那只是下载php文件而不是mp3文件。以下是我的密码- 以下代码适用于我无法从youtube视频中获取的mp3链接,但此代码不适用于任何可在浏览器上

有没有办法获得youtube视频的.mp3链接?我尝试了多个在线youtube-to-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()
    }
}

正如我在评论中所建议的,使用你可以做到这一点

    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()

}

我还没有做错误处理,所以您需要进一步完善这段代码。但这肯定会奏效。我已经测试过了。

您需要获得转换后mp3的直接下载URL。为此,你必须在网页的URL中播放HTML(将视频转换为mp3)。检查这个。。这不在iOS中,但您可能需要以类似的方式工作。是否有其他方法获取转换后的mp3的直接下载链接?不确定,请检查一下。我已经尝试过此方法,但没有,它不起作用:(,它下载的是get文件而不是mp3文件。另外,我们从中获取的url不是mp3文件。请将您的文件的扩展名保存为.mp3。它会工作的。您可以通过将名为get的文件的扩展名重命名为.mp3来测试它。很抱歉延迟..我正忙于其他工作。没有保存扩展名为.mp3的文件对我有效。保存的文件不可用播放。如果你也尝试过。它对你有用吗?我的意思是你能播放它吗?是的,我刚刚将“get”命名为“get.mp3”。并在iTunes中播放了该文件。它对我有效。很抱歉,响应太晚,您的解决方案有效,但只有几次。有时失败是因为youtubeinmp3链接没有返回json格式的数据。我已经对其进行了多次测试,该链接有时返回json响应,有时会重定向到不同的页面。