Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Brief and clear description of the problem #532

Open
vuhaopro90 opened this issue Jan 1, 2025 · 0 comments
Open

[BUG] Brief and clear description of the problem #532

vuhaopro90 opened this issue Jan 1, 2025 · 0 comments
Assignees
Labels
BUG Something isn't working enhancement New feature or request

Comments

@vuhaopro90
Copy link

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}")

@vuhaopro90 vuhaopro90 added BUG Something isn't working enhancement New feature or request labels Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants