kewlbear / YoutubeDL-iOS

Swift package of youtube_dl python module for iOS
MIT License
112 stars 35 forks source link

Download Speed and Repeated Download Issue #18

Open Asherbinyy opened 1 month ago

Asherbinyy commented 1 month ago

Description:

I encountered issues using the repo for downloading TikTok videos in my project. The problems are as follows:

  1. Download Speed: The download speed is almost fixed, taking around 20 seconds regardless of the video length or quality.
  2. First-Time Download Only: The download completes only the first time the app is installed. After 20 seconds, it asks for gallery access permission, and once granted, the video is saved. However, subsequent download attempts fail silently, requiring the app to be killed and re-run to download another video.
  3. No Exception Handling: After the initial successful download, subsequent downloads do not throw any exception, and the process stops without any feedback.
  4. Lack of Documentation: The repository lacks documentation, making troubleshooting or understanding the underlying issues difficult.

What I Tried:

• Attempted various download options, all leading to the same results. • Suspected caching in the YoutubeDL module and attempted to clear the directory before calling the API, but this did not resolve the issue.

Here's my code:


import Foundation
import YoutubeDL

@objc(YoutubeDLWrapper)
class YoutubeDLWrapper: NSObject {
    @objc func downloadVideo(url: String , completion: @escaping (NSString?, NSError?) -> Void) {
        print("Swift: downloadVideo called with URL: \(url)")
        Task {
            guard let url = URL(string: url) else {
                print("Swift: Invalid URL")
                let error = NSError(domain: "Invalid URL", code: 400, userInfo: nil)
                completion(nil, error)
                return
            }
            let directoryContents = try FileManager.default.contentsOfDirectory( at: url, includingPropertiesForKeys: nil, options: [])
            for file in directoryContents {
                do {
                    try FileManager.default.removeItem(at: file)
                } catch let error as NSError {
                    debugPrint("Ooops! Something went wrong: \(error)")
                }
            }
            do {
                let youtubeDL = YoutubeDL()
                print("Swift: Starting download with youtube-dl for URL: \(url)")
                let data = try await youtubeDL.download(url: url, options: [])
                print("Swift: Data is -> \(data)")
                print("Swift: Video downloaded successfully")
                completion(url.absoluteString as NSString, nil)
            } catch {
                print("Swift: Error downloading video: \(error.localizedDescription)")
                let nsError = error as NSError
                completion(nil, nsError)
            }
        }
    }

private func clearCache(){
  // let cacheURL =  FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
  // let fileManager = FileManager.default
  // do {
  // let directoryContents = try FileManager.default.contentsOfDirectory(at: cacheURL, includingPropertiesForKeys: nil, options: [])
  // for file in directoryContents {
  // do {
  // try fileManager.removeItem(at: file)
  // } catch let error as NSError {
  // debugPrint("Ooops! Something went wrong: \(error)")
  // }
  // }
  // } catch let error as NSError {
  // print(error.localizedDescription)
  // }
}
}