0

上記のビデオのURLを指定して、プログラムでYouTubeビデオの長さを取得する最も簡単な方法を理解する必要があります。

YouTube API は最良の方法ですか? やや複雑に見えますが、これまで使用したことがないため、慣れるまでに少し時間がかかる可能性がありますが、最も迅速な解決策が本当に欲しい. そこにリストされることを期待してビデオページのソースを一瞥しましたが、明らかにそうではありません(ただし、解析が容易な非常に優れたリストに推奨ビデオ時間がリストされています). それが最善の方法である場合、誰かがスニピットを持っていますか?

理想的には、これを Python で行うことができ、最終的には次の形式にする必要があります。

00:00:00.000

しかし、私は誰もが持っている可能性のある解決策に対して完全にオープンです.

洞察をいただければ幸いです。

4

5 に答える 5

3

Youtube API 2.0 から返された XML から要素のseconds属性を読み取るだけです。yt:duration最終的には秒単位の解像度しかありません (まだミリ秒単位ではありません)。次に例を示します。

from datetime import timedelta
from urllib2 import urlopen
from xml.dom.minidom import parseString
for vid in ('wJ4hPaNyHnY', 'dJ38nHlVE78', 'huXaL8qj2Vs'):
    url = 'https://gdata.youtube.com/feeds/api/videos/{0}?v=2'.format(vid)
    s = urlopen(url).read()
    d = parseString(s)
    e = d.getElementsByTagName('yt:duration')[0]
    a = e.attributes['seconds']
    v = int(a.value)
    t = timedelta(seconds=v)
    print(t)

出力は次のとおりです。

0:00:59
0:02:24
0:04:49
于 2013-06-21T17:00:55.087 に答える
1

(「事前ダウンロード」が何を指しているのかわかりません。)

の長さを取得する最も簡単な方法VIDEO_IDは、

http://gdata.youtube.com/feeds/api/videos/VIDEO_ID?v=2&alt=jsonc

data次に、返された->duration要素の値を確認します。動画の長さを秒単位で設定します。

于 2013-06-21T16:26:24.383 に答える
1

With python and V3 youtube 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/youtube/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

Console response:

>>>PT6M22S

Corresponds to 6 minutes and 22 seconds.

于 2015-11-15T21:27:34.367 に答える
0

これにはいつでもData API v3を利用できます。videos->list呼び出しを実行するだけです。

GET https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+fileDetails&id={VIDEO_ID}&key={YOUR_API_KEY}

応答として、ISO 8601 形式のcontentDetails.duration を取得します

または、 fileDetails.durationMsからミリ秒単位で期間を取得できます。

于 2013-06-21T20:16:28.767 に答える