2017-08-30 84 views
0

我有问题,从我的服务器下载影片如http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro%20USLUGE.mp4的Python下载视频

所有完美的作品时,互联网是确定的,但是当我从树莓派断开局域网电缆,并保持这样的小于10-15秒。但是,当互联网已关闭超过10-15秒,下载没有继续或视频无法正常下载(我后来把它们合并与MP4Box和他们需要)。如果有人有建议如何解决这个问题,并帮助我,我将非常感激。

这里是我的代码:

import os 
import urllib 
import urllib2 
import time 
import commands 
import requests 
import shutil 
from urllib2 import URLError 

urls = ['http://screensfiles.dbtouch.com/screens2/Companies/89/HD/00 APPS OVERVIEW.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/ILIRIJA BIOGRAD 2016.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Restoran marina.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/HT Screens.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Tasks.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Croatia Full of life.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/04 PROJECTS.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/05 ATTEND.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro Hotel.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Tasks.mp4', 
'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4'] 

directory = "/home/pi/pythonSignage/current_playlist/videos_to_merge/" 


i=1 

for url in urls: 

    i += 1 
    print("current iter: ") 
    print(i) 

    if (len(urls) > 1): 
     url_formatted = url.split('/')[-1].replace(" ", "").replace("%20", "") + " " 
    else: 
     url_formatted = url.split('/')[-1].replace(" ", "").replace("%20", "") 

    url_formatted_name = url.split('/')[-1].replace(" ", "").replace("%20", "").rstrip() 

    while True: 

     print("inside while true") 
     try: 
      """ method 0 doesn't work """ 
      print("try") 
      response = urllib2.urlopen(url, timeout=5) 
      content = response.read() 
      print("content") 
      f = open(directory + url_formatted_name, 'wb') 
      f.write(content) 
      f.close() 

      """ method 1 doesn't work """ 
      #video_fancy_downloader = urllib.FancyURLopener() 
      #video_fancy_downloader.retrieve(url, directory + url_formatted_name) 

      """ method 2 - doesn't work """ 
      #my_file = urllib.URLopener() 
      #my_file = retrieve(url, directory + url_formatted_name) 

      """ method 3 - doesn't work """ 
      #response = requests.get(url, stream=True) 
      #response.raise_for_status() 
      #with open(directory + url_formatted_name, 'wb') as handle: 
      # for block in response.iter_content(1024): 
      #  handle.write(block) 

     except: 
      print("error download, sleep 5 sec") 
      time.sleep(5) 


print("end") 

回答

0

我已成功地解决我的问题。也许这不是最好的方法,但它的工作原理。

这里是功能下载视频并返回响应:下载功能的

def do_download(destination, url): 
    comm = ["wget", "-c", "-O", destination, "-t", "15000", "-T", "5", url] 
    proc = subprocess.Popen(comm, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
    tmp = proc.stdout.read() 
    if "wget: unable to resolve host address" in tmp: 
     return False 
    else: 
     return True 

核心部分几乎是相同的,但现在它调用do_download内while循环,并检查响应:

if os.path.isfile(directory+url_formatted_name) is False: 
    print("must download file!") 

    while downl_success is False: 
     print("inside while true") 
     try: 
      print("try") 

      while(do_download(directory + url_formatted_name, url) is False): 
       print(" ------- inside while for download ----------- ") 
       time.sleep(5) 

      downl_success = True 

      print("file downloaded fully!") 
      break 

     except HTTPError, e: 
      print "HTTPError", e.code, url 
      time.sleep(5) 

     except URLError, e: 
      print "URL Error", e.reason, url 
      time.sleep(5) 

else: 
    print("file already downloaded no need to download it again!")