2017-08-30 82 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!")