将v.redd.it视频下载为mp4

时间:2018-06-29 10:16:43

标签: python-3.x wget reddit

我正在尝试制作一个Python脚本,允许用户从Reddit帖子(v.redd.it)中输入URL并将其作为mp4下载到我的Downloads文件夹中。

这是由于URL的json被解码和读取并存储为变量而发生的。然后将json的字典导航到 fallback_url ,其中 wget.download 将导航URL上的唯一视频下载到我的“下载”文件夹中。

url = ''.join((url,'.json')) #add .json to end of url input for python to extract video url

with urllib.request.urlopen(url) as url: #python open url as http request
            data = json.loads(url.read().decode()) #read the json url
            file = data[0]['data']['children'][0]['data']['secure_media']['reddit_video']['fallback_url'] #navigate the json to the url that has the mp4 (fallback_url)

            #debug
            print(file) #print if url is read
            time.sleep(4) #reddit has a request limit per minute (see below)
            wget.download(file,'C:/Users/x/Downloads/') #use wget to download the file, hopefully in .mp4, but doesn't work
  

每分钟不超过30个请求。这样可以使您的请求更加突兀,但请保持理智。平均而言,每两秒钟我们收到的请求不超过一个。

因此,我首先输入Reddit URL,它存储在变量 url 中。 然后将 .json 添加到 www.reddit.com/r/example /.../ .json 的末尾,脚本将通过该结尾打开URL,将Json的数据存储到字典 data 变量中。然后它将通过字典的树,到达mp4所在的 fallback_url 的键。

但是,如果您打开该url,则可以看到它只有一个元素,即mp4视频。但是,当我使用 wget.download(file,'location')时,它的下载标题为 DASH_9_6_M (URL的标题),没有扩展名,我d希望是mp4。该文件没有扩展名,但大小与mp4一样。

所以我的问题是,如何让wget以mp4格式下载视频?

2 个答案:

答案 0 :(得分:0)

您可以使用urllib.urlretrieve来完成。尝试使用urllib.urlretrieve(file, "download.mp4")

答案 1 :(得分:0)

尝试urllib.request.urlretrieve(file, "download.mp4")