You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My code works fine; it can download videos from Douyin, but it's very slow. For the requested link, it returns a JSON string, and I only need to extract the link to download to my computer. However, accessing that link via a browser is very fast, but with Python, it's very slow. Does anyone have a solution? Thank you.
import requests
import os
# Đường link chứa dữ liệu JSON
url = 'https://api.douyin.wtf/api/hybrid/video_data?url=https://www.douyin.com/video/7450388870656527679&minimal=false'
# Gửi yêu cầu GET để lấy dữ liệu JSON
response = requests.get(url)
# Kiểm tra nếu yêu cầu thành công
if response.status_code == 200:
# Chuyển đổi dữ liệu trả về thành JSON
data = response.json()
# Lấy giá trị aweme_id từ JSON
aweme_id = data.get('data', {}).get('aweme_id', 'default_aweme_id')
# Lấy danh sách URL từ vị trí 'data->video->play_addr_h264->url_list'
url_list = data.get('data', {}).get('video', {}).get('play_addr_h264', {}).get('url_list', [])
# Kiểm tra nếu có URL trong danh sách
if url_list:
for idx, video_url in enumerate(url_list):
print(f"Đang tải video từ: {video_url}")
# Gửi yêu cầu GET để tải video
video_response = requests.get(video_url, stream=True)
# Kiểm tra nếu yêu cầu tải video thành công
if video_response.status_code == 200:
# Xác định tên file để lưu video dựa trên aweme_id
video_filename = os.path.join('downloads', f'{aweme_id}.mp4')
# Đảm bảo thư mục downloads tồn tại
os.makedirs(os.path.dirname(video_filename), exist_ok=True)
# Mở file để ghi nội dung video
with open(video_filename, 'wb') as file:
for chunk in video_response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print(f"Video đã được tải về: {video_filename}")
break
else:
print(f"Lỗi khi tải video từ URL: {video_url}")
else:
print("Không tìm thấy URL trong danh sách.")
else:
print(f"Lỗi khi truy cập URL: {response.status_code}")
The text was updated successfully, but these errors were encountered:
My code works fine; it can download videos from Douyin, but it's very slow. For the requested link, it returns a JSON string, and I only need to extract the link to download to my computer. However, accessing that link via a browser is very fast, but with Python, it's very slow. Does anyone have a solution? Thank you.
The text was updated successfully, but these errors were encountered: