最近在想用爬虫写youtube网站下载学习视频,找了好多资料也没有有个有用的。

真不容易找到几行代码,代码实现很简单,基于youtube_dl

来之不易,仅参考

1.只是下载 YouTube 视频



from __future__ import unicode_literals


import youtube_dl


ydl_opts = {}


with youtube_dl.YoutubeDL(ydl_opts) as ydl:


    ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])


 


实现截图如下


YouTube爬虫下载_3c

 

 2.格式转码

#ydl2.py
from __future__ import unicode_literals
import youtube_dl

def my_hook(d):
if d['status'] == 'finished':
print('Done downloading, now converting ...')

ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(id)s',
'noplaylist' : True,
'progress_hooks': [my_hook],
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=pwp1CH5R-w4'])
运行

YouTube爬虫下载_代码实现_02

 

 

YouTube爬虫下载_代码实现_03