pytube——下载YouTube视频的python库

pytube的翻译文档


描述


Youtube是世界上非常流行的视频分享平台,并且作为一个极客,你可能会遇到这样一种情形,就是你想去写点东西下载视频。鉴于此情况,我推荐pytube给你。
pytube是用python写的轻量级库,它没有第三方的依赖并且目的是为了变得高度可靠。
pytube没有假定,意思是没有内建的方法获得‘最’高质量的视频。pytube仅仅给出了所有可以获得的形式和方法,给开发者自己定义所有的’最高‘是什么。
pytube使管程简单,允许你能给清楚的知道不同下载事件的堆栈函数调用,比如on progress 或者or complete
最后,pytube也包括一种命令行的实现,允许你快速从终端下载视频。

安装


下载使用pip通过pypi
pip install pytube

库的用法


from pytube import YouTube
# not necessary, just for demo purposes.//不是很必要,只是为了演示的目的
from pprint import pprint
yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")

# Once set, you can see all the codec and quality options YouTube has made
# available for the perticular video by printing videos.
# 一旦选定,你可以看见youtube提供的所有编码和质量选项通过打印视频的方法
print(yt.get_videos())

# [<Video: MPEG-4 Visual (.3gp) - 144p>,
#  <Video: MPEG-4 Visual (.3gp) - 240p>,
#  <Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>,
#  <Video: H.264 (.mp4) - 360p>,
#  <Video: H.264 (.mp4) - 720p>,
#  <Video: VP8 (.webm) - 360p>,
#  <Video: VP8 (.webm) - 480p>]

# The filename is automatically generated based on the video title.  You
# can override this by manually setting the filename.
# 文件名自动根据视频名生成,你可以覆盖它通过手动设置文件名

# view the auto generated filename:   查看自动生成的文件名
print(yt.filename)

# Pulp Fiction - Dancing Scene [HD]

# set the filename: 设置文件名的方法
yt.set_filename('Dancing Scene from Pulp Fiction')

# You can also filter the criteria by filetype.  你可以通过过滤器过滤指定的类型
print(yt.filter('flv'))

# [<Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>]

# Notice that the list is ordered by lowest resolution to highest. If you
# wanted the highest resolution available for a specific file type, you
# can simply do:(注意:列表是从最低到最高的质量排序,如果你想要更高的特定文件类型的分辨率,你可以仅通过:
print(yt.filter('mp4')[-1])
# <Video: H.264 (.mp4) - 720p>

# You can also get all videos for a given resolution  (你也可以得到所有的视频通过给一个分辨率)
print(yt.filter(resolution='480p'))

# [<Video: H.264 (.flv) - 480p>,
#  <Video: VP8 (.webm) - 480p>]

# To select a video by a specific resolution and filetype you can use the get
# method.(为了挑选一个特定分辨率和文件类型,你可以使用get方法)

video = yt.get('mp4', '720p')

# NOTE: get() can only be used if and only if one object matches your criteria.(get()能够被使用的前提必须是一个对象符合标准)
# for example:(例如:)

print(yt.videos)

#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]

# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...(既然我们有2个H.264(.mp4)可获得,我们可以尝试使用get)

video = yt.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.  (多个对象符合标准)

# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).(在这种情况下,我们需要明确编码和分辨率)

# Okay, let's download it! (a destination directory is required) //行,那么我们下载它(需要一个目的目录)
video.download('/tmp/')

命令行使用方法

你可以下载一个视频通过简单的传递-e参数或者--extension=转换并且设置它为你希望的文件类型
$ pytube -e mp4 http://www.youtube.com/watch?v=Ik-RsDGPI5Y
同样的:
$ pytube -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y
当运行没有分辨率或者扩展,它会给出一个所有可以下载的列表

$ pytube http://www.youtube.com/watch?v=Ik-RsDGPI5Y
  Resolution      Extension
 ----------------------------
 0  3gp             144p
 1  3gp             240p
 2  mp4             360p
 3  mp4             720p
 4  webm            360p
 Enter choice:

你可以看见可得到的一系列的可获得的格式通过传递-s或者--show-available标志
$ pytube -s http://www.youtube.com/watch?v=Ik-RsDGPI5Y
你也可以明确下载的文件路径通过(-p或者--path=)
$ pytube -e mp4 -p ~/Downloads/ http://www.youtube.com/watch?v=Ik-RsDGPI5Y
或者可以选择文件名(-f或者--filename=):
$ pytube -e mp4 -f "Dancing Scene from Pulp Fiction" http://www.youtube.com/watch?v=Ik-RsDGPI5Y
你可以可以通过明确分辨率或者期望的类型
$ pytube -e mp4 -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y


注意:

本文是本人翻译github上的Readme文档,时间是2017年5月22日,可能以后作者会有修改。原文地址如下:https://github.com/nficano/pytube.

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值