python下载油管视频
import time from pytube import YouTube import requests # 需要自行创建三个文件夹 video audio cover def getId(str): vid = str.split("=")[-1] print(vid) return vid def down(vid): if not vid: print("地址有误,请重试") return start = time.time() yt = YouTube('https://youtu.be/%s' % vid) itags = [137, 399, 22] for i in itags: try: yt.streams.get_by_itag(i).download(output_path="./video", filename_prefix=vid + "_") if i != 22: yt.streams.get_by_itag(140).download(output_path="./audio", filename_prefix=vid + "_") break except: print("换清晰度..") end = time.time() downCover(vid) print("下载成功,用时%s秒" % (end - start)) def downCover(vid): try: # 1080p res = requests.get("https://img.youtube.com/vi/%s/maxresdefault.jpg" % vid) with open("./cover/%s.jpg" % vid, 'wb') as f: f.write(res.content) except: # 720p res = requests.get("https://img.youtube.com/vi/%s/hqdefault.jpg" % vid) with open("./cover/%s.jpg" % vid, 'wb') as f: f.write(res.content) if __name__ == '__main__': URL = "https://www.youtube.com/watch?v=J4Ws9A-K14I" vid = getId(URL) down(vid)
人生苦短,慢慢潇洒。
nsyw.run