不用太多的設定,一支 python執行檔 即可完成!
安裝 python && pip install pytube
建立 ytd.py
### ytd.py ### import os import sys import uuid import re # 儲存根目錄 rootDir = "./download/" if not os.path.isdir(rootDir): os.mkdir(rootDir) # 儲存子目錄 try: fileDir = sys.argv[2] except IndexError: fileDir = '' dirPath = rootDir + fileDir if not os.path.isdir(dirPath): os.mkdir(dirPath) os.chdir(dirPath) from pytube import YouTube yt = YouTube(sys.argv[1]) title = yt.title + ".mp3" title = re.sub(r'[\/:*?<>|"]*', '', title) # 移除 \/:*?<>|" hashName = str(uuid.uuid1()) print(hashName) print("download...") yt.streams.filter().get_audio_only().download(filename=hashName) os.rename(hashName, title) print('success!')
指令 python ytd.py https://www.youtube.com/watch?v=PJ6HHojdQu8 dir