Preface:
Many people believe we have seen the video on youtube site, there are a lot of high-quality video on the website, clarity is also very high, liked want to download to a local, although there are many ways, but certainly not to the quick python ,
Ado, on the code:
Install:
pip install pafy
pip install youtube-dl
First to study under pafy module, this module Baidu no information next to the python's official website https://pypi.org/project/pafy/ check the next, let's look at what are some features of this module
import pafy url = "https://www.youtube.com/watch?v=bMt47wvK6u0" video = pafy.new(url) print(video)
This is a video link to Jay, we'll take a look at what https://www.youtube.com/watch?v=zk4Olw9eRVo output
Print (video.title) # Cindy Yen Cindy Yen [I] believe in you I Believe Official Lyric MV - drama 'with nine Ganzi Dian "episode Print (video.author) # JVR Music Music JVR Print (video.viewcount) # 27563 Print (video.length) # 295 Print (video.duration) # 00:04:55 Print (video.likes) # 611 Print (video.dislikes) # 13 Print (video.description) # Introduction .... ..
Also you can view the video can be downloaded separately rate list
for s in streams: print(s)
normal:webm@640x360
normal:mp4@640x360
normal:mp4@1280x720
Of course, we then detailed view
for s in streams: print(s.resolution, s.extension, s.get_filesize(), s.url)
It is not a resolution, format, size, have a download link to what these were not enough, we then look
import pafy url = "https://www.youtube.com/watch?v=bMt47wvK6u0" video = pafy.new(url) best = video.getbest() print(best.resolution, best.extension)
1280x720 mp4
getbest () method does this video output is most clear that a piece of information
getbest (preftype = "webm") may also be used so that the output video format specified, then look
import pafy url = "https://www.youtube.com/watch?v=bMt47wvK6u0" video = pafy.new(url) Best = video.getbest (preftype = " webm " ) Print (best.url) # print out the specified format video link
We then look at the video download method, this module has been for us a good package, you can call this method directly
import pafy url = "https://www.youtube.com/watch?v=bMt47wvK6u0" video = pafy.new(url) best = video.getbest() best.download(quiet=False)
best.download (filepath = "/ tmp / ", quiet = False)
you can specify the download path, and whether to display the progress bar
below we look at audio download method:
import pafy url = "https://www.youtube.com/watch?v=bMt47wvK6u0" video = pafy.new(url) audiostreams = video.audiostreams for a in audiostreams: print(a.bitrate, a.extension, a.get_filesize())
audiostreams [ . 1 ] . downloads () # downloaded first pieces of audio information
= bestaudio video.getbestaudio () # is also the highest quality output that a Print (bestaudio.bitrate)
160K
bestaudio . download () # can directly download
Then look at:
allstreams = video.allstreams for s in allstreams: print(s.mediatype, s.extension, s.quality)
This method, may output all the data of this video
In the final, the complete code:
import requests import re import pafy class YoutubeVideoDownload(): def get_video_list(self,list_url): """解析视频列表方法""" headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' } html = requests.get(list_url, headers=headers).text videoIds = re.findall('"videoId":"([A-Za-z0-9_-]{11})","thumbnail"', html) for videoId in videoIds: download_link = "https://www.youtube.com/watch?v=" + videoId print(download_link) self.get_video_info(download_link) def get_video_info(self,detail_url): """下载视频""" video = pafy.new(detail_url) v_best = video.getbest() v_best.download() if __name__ == '__main__': yotubo = YoutubeVideoDownload() yotubo.get_video_list("https://www.youtube.com/watch?v=F7tCW0wiLk4&list=OLAK5uy_nx5m5O-enzVoclJmkLw7d9Qaaibp8xR4A")
I understand that friends do not need to say anything now, support for parsing a list of download, supports a single video downloads, you can call different methods