且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 python 获取 *** 视频的持续时间?

更新时间:2023-02-09 12:25:31

获取 *** 视频时长的两种方法

第一种方式:

使用 python 和 V3 *** api,这是每个视频的方式.您需要 API 密钥,可以在此处获取:https://console.developers.google.com/一个>

With python and V3 *** api this is the way for every videos. You need the API key, you can get it here: https://console.developers.google.com/

# -*- coding: utf-8 -*-
import json
import urllib

video_id="6_zn4WCeX0o"
api_key="Your API KEY replace it!"
searchUrl="https://www.googleapis.com/***/v3/videos?id="+video_id+"&key="+api_key+"&part=contentDetails"
response = urllib.urlopen(searchUrl).read()
data = json.loads(response)
all_data=data['items']
contentDetails=all_data[0]['contentDetails']
duration=contentDetails['duration']
print duration

控制台响应:

>>>PT6M22S

对应于 6 分 22 秒.

Corresponds to 6 minutes and 22 seconds.

第二种方式:

另一种但不适用于所有视频的方法是使用 pafy 外部包:

Another way but not works for all videos is with pafy external package:

import pafy

url = "http://www.***.com/watch?v=cyMHZVT91Dw"
video = pafy.new(url)
print video.length

我从 https://pypi.python.org/pypi/pafy/安装了 pafy0.3.42