当前位置:首页 » python教程 » 正文

python 实现多线程下载视频的代码

看: 719次  时间:2021-02-22  分类 : 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 实现多线程下载视频的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对python博客网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

标签:requests  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!