Skip to content

Commit 204623a

Browse files
code v1
1 parent dfc9371 commit 204623a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

main.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import threading
2+
import os
3+
import requests
4+
from concurrent.futures import ThreadPoolExecutor, as_completed
5+
6+
def download_part(start, end, url, file_name):
7+
headers = {'Range': 'bytes={}-{}'.format(start, end)}
8+
r = requests.get(url, headers=headers, stream=True)
9+
with open(file_name, 'wb') as f:
10+
for chunk in r.iter_content(chunk_size=1024):
11+
if chunk:
12+
f.write(chunk)
13+
14+
def download(url, filename):
15+
num_parts = 30
16+
response = requests.head(url)
17+
total_length = int(response.headers.get('content-length'))
18+
19+
part_size = total_length // num_parts
20+
21+
threads = []
22+
for i in range(num_parts):
23+
start = i * part_size
24+
end = start + part_size - 1 if i < num_parts - 1 else total_length - 1
25+
part_file_name = '.paths/{}_{}.mp4'.format(filename, i)
26+
t = threading.Thread(target=download_part, args=(start, end, url, part_file_name))
27+
threads.append(t)
28+
29+
for t in threads:
30+
t.start()
31+
32+
for t in threads:
33+
t.join()
34+
35+
with open(f'videos/{filename}.mp4', 'wb') as f:
36+
for i in range(num_parts):
37+
part_file_name = '.paths/{}_{}.mp4'.format(filename, i)
38+
with open(part_file_name, 'rb') as part_file:
39+
f.write(part_file.read())
40+
41+
# delete the part file
42+
os.remove(part_file_name)
43+
44+
45+
down_links = ['https://example.com/video1.mp4', 'https://example.com/video2.mp4']
46+
File_name = ['video1', 'video2']
47+
48+
with ThreadPoolExecutor(max_workers=10) as executor:
49+
futures = []
50+
for url, filename in zip(down_links, File_name):
51+
futures.append(executor.submit(download, url, filename))
52+
53+
for future in as_completed(futures):
54+
result = future.result()
55+
print(result)

0 commit comments

Comments
 (0)