1. 程式人生 > 程式設計 >python 實現多執行緒下載視訊的程式碼

python 實現多執行緒下載視訊的程式碼

程式碼:

def thread(url):
  r = requests.get(url,headers=None,stream=True,timeout=30)
  # print(r.status_code,r.headers)
  headers = {}
  all_thread = 1
  # 獲取視訊大小
  file_size = int(r.headers['content-length'])
  # 如果獲取到檔案大小,建立一個和需要下載檔案一樣大小的檔案
  if file_size:
    fp = open('2.mp4','wb')
    fp.truncate(file_size)
    print('視訊大小:' + str(int(file_size / 1024 / 1024)) + "MB")
    fp.close()
  # 每個執行緒每次下載大小為5M
  size = 5242880
  # 當前檔案大小需大於5M
  if file_size > size:
    # 獲取匯流排程數
    all_thread = int(file_size / size)
    # 設最大執行緒數為10,如匯流排程數大於10
    # 執行緒數為10
    if all_thread > 10:
      all_thread = 10
  part = file_size // all_thread
  threads = []
  starttime = datetime.datetime.now().replace(microsecond=0)
  for i in range(all_thread):
    # 獲取每個執行緒開始時的檔案位置
    start = part * i
    # 獲取每個檔案結束位置
    if i == all_thread - 1:
      end = file_size
    else:
      end = start + part
    if i > 0:
      start += 1
    headers = headers.copy()
    headers['Range'] = "bytes=%s-%s" % (start,end)
    t = threading.Thread(target=Handler,name='th-' + str(i),kwargs={'start': start,'end': end,'url': url,'filename': '2.mp4','headers': headers})
    t.setDaemon(True)
    threads.append(t)
  # 執行緒開始
  for t in threads:
    time.sleep(0.2)
    t.start()
  # 等待所有執行緒結束
  for t in threads:
    t.join()
  endtime = datetime.datetime.now().replace(microsecond=0)
  print('用時:%s' % (endtime - starttime))
def Handler(start,end,url,filename,headers={}):
  tt_name = threading.current_thread().getName()
  print(tt_name + ' is begin')
  r = requests.get(url,headers=headers,stream=True)
  total_size = end - start
  downsize = 0
  startTime = time.time()
  with open(filename,'r+b') as fp:
    fp.seek(start)
    var = fp.tell()
    for chunk in r.iter_content(204800):
      if chunk:
        fp.write(chunk)
        downsize += len(chunk)
        line = tt_name + '-downloading %d KB/s - %.2f MB, 共 %.2f MB'
        line = line % (
          downsize / 1024 / (time.time() - startTime),downsize / 1024 / 1024,total_size / 1024 / 1024)
        print(line,end='\r')
if __name__ == '__main__':
  url = input('輸入視訊連結(請輸入視訊原鏈):')
  thread(url)

效果:

可以看見,38MB,一秒下完。

唯一的缺點就是,要有視訊原鏈,而一般這個視訊原鏈都是不會輕易被找到的,這就叫反爬。

找視訊原鏈,就找爬蟲,視訊爬蟲只是爬蟲的一種。

可以根據視訊大小,改變執行緒數。

總結

以上所述是小編給大家介紹的python 實現多執行緒下載視訊的程式碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!