File size: 2,107 Bytes
b619216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
import os
import yt_dlp
from concurrent.futures import ThreadPoolExecutor

# Load the JSON data from your file
with open('first_half22.json') as f:
    data = json.load(f)

# Set the number of concurrent downloads
num_concurrent_downloads = 6  # 可以根据需要调整这个值

keys = list(data.keys())
index = keys.index('3QmgdnDX8vk')

# Keep only the keys from 'NUMo9xv7FnY' onwards
new_data = {k: data[k] for k in keys[index:]}



def download_video(video_id, video_data):
    folder = video_data['folder']
    print(f'Video ID: {video_id}, Folder: {folder}')

    # Create a folder for each video if it doesn't exist
    video_folder = os.path.join('videos1', folder)
    if not os.path.exists(video_folder):
        os.makedirs(video_folder)

    # Check if the .mp4 video file already exists
    video_filename_mp4 = os.path.join(video_folder, f'{video_id}.mp4')
    if os.path.exists(video_filename_mp4):
        print(f'Video file for Video ID: {video_id} already exists. Skipping download.')
        return

    # Download with yt_dlp
    video_url = f'https://www.youtube.com/watch?v={video_id}'
    ydl_opts = {
        'format': 'best',
        'outtmpl': os.path.join(video_folder, f'{video_id}.%(ext)s'),  # output filename template
        'postprocessors': [{'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4'}],  # convert to mp4
        # 'username': '你的用户名',  # 添加这一行
        # 'password': '你的密码',  # 添加这一行
    }
    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([video_url])
        print(f'Downloaded video for Video ID: {video_id} and saved in folder: {folder} with yt_dlp')
    except Exception as e:
        print(f'Error downloading video for Video ID: {video_id} with yt_dlp: {str(e)}')

# Use ThreadPoolExecutor to download videos concurrently
with ThreadPoolExecutor(max_workers=num_concurrent_downloads) as executor:
    futures = {executor.submit(download_video, video_id, video_data) for video_id, video_data in new_data.items()}