首页 > 解决方案 > Youtube dl 不是用 python 2 执行的

问题描述

我想执行 youtube dl 来下载视频。

这是我的代码:

struct YoutubeVideoInfo: Codable {
  let fulltitle: String
  let url: String
  let thumbnail: String
  let uploader: String
  
  static func getVideoInfo(from url: String) -> YoutubeVideoInfo? {
    guard let path = Bundle.main.path(forResource: "youtube-dl", ofType: ""),
          let json = executeCommand(command: path, args: ["-f mp4/best", "--dump-json", url]),
          let data = json.data(using: .utf8),
          let info = try? JSONDecoder().decode(YoutubeVideoInfo.self, from: data) else {
      return nil
    }
    
    return info
  }
}


func executeCommand(command: String, args: [String]) -> String? {
  let task = Process()
  
  task.launchPath = command
  task.arguments = args
  
  let pipe = Pipe()
  task.standardOutput = pipe
  task.launch()
  
  let data = pipe.fileHandleForReading.readDataToEndOfFile()
  guard let output = String(data: data, encoding: .utf8) else {
    return nil
  }
  
  return output
}

但它显示错误:

回溯(最后一次调用):文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py”,第 174 行,在 _run_module_as_main “ main ”,fname,loader,pkg_name )文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py”,第72行,在run_globals文件中的_run_code执行代码“/Users/khuongpham/Library/Developer/Xcode /DerivedData/VideoDownloader-agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/ main .py”,第 16 行,在文件“/Users/khuongpham/Library/Developer/Xcode/DerivedData/VideoDownloader -agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/youtube_dl/ init.py”,第 15 行,在文件“/Users/khuongpham/Library/Developer/Xcode/DerivedData/VideoDownloader-agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/youtube_dl/options.py ”,第 8,在文件“/Users/khuongpham/Library/Developer/Xcode/DerivedData/VideoDownloader-agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/youtube_dl/downloader/init.py”,第 3 行,在文件“/Users/khuongpham/Library/Developer/Xcode/DerivedData/VideoDownloader-agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/youtube_dl/downloader/common .py”,第 9 行,在文件“/Users/khuongpham/Library/Developer/Xcode/DerivedData/VideoDownloader-agkzwgefyogltjaxgreulgpacwbv/Build/Products/Debug/VideoDownloader.app/Contents/Resources/youtube-dl/youtube_dl/compat.py ",第 2359 行,在文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/http/server.py",第 7 行,从 CGIHTTPServer 导入 * 文件 "/System/Library /Frameworks/Python.framework/Versions/2.7/lib/python2.7/CGIHTTPServer.py”,第 30 行,在导入 SimpleHTTPServer 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py”,第 28 行,在 SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler) 类中:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 219, in SimpleHTTPRequestHandler mimetypes.init() # try read system mime.types File "/System /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetypes.py”,第 358 行,在 init db.read(file) 文件中“/System/Library/Frameworks/Python.framework/Versions/ 2.7/lib/python2.7/mimetypes.py",第 202 行,使用 open(filename) 作为 fp 读取:IOError: [Errno 1] Operation not allowed: '/etc/apache2/mime.types'

标签: iosmacosyoutube-dl

解决方案


推荐阅读