File size: 2,947 Bytes
d85aab3
7762994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a96293b
7762994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7290077
7762994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887c004
7762994
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import pickle
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

CLIENT_SECRETS_FILE = 'client_secrets.json'
CREDENTIALS_FILE = 'youtube_token.pickle'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']

def authenticate_youtube():
    creds = None
    if os.path.exists(CREDENTIALS_FILE):
        with open(CREDENTIALS_FILE, 'rb') as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                CLIENT_SECRETS_FILE, SCOPES)
            # 手动认证:命令行打印链接,复制粘贴访问授权
            creds = flow.run_console()
        with open(CREDENTIALS_FILE, 'wb') as token:
            pickle.dump(creds, token)
    youtube = build('youtube', 'v3', credentials=creds)
    return youtube

def upload_video(youtube, video_path, title, description, tags=None, privacy_status="private"):
    if not os.path.exists(video_path):
        print(f"错误:找不到视频文件 {video_path}")
        return None
    
    body = {
        'snippet': {
            'title': title,
            'description': description,
            'tags': tags or [],
            'categoryId': '22'  # People & Blogs
        },
        'status': {
            'privacyStatus': privacy_status,
            'selfDeclaredMadeForKids': False
        }
    }
    
    media = MediaFileUpload(video_path, chunksize=-1, resumable=True, mimetype='video/*')
    request = youtube.videos().insert(
        part=','.join(body.keys()),
        body=body,
        media_body=media
    )
    response = request.execute()
    
    print(f"上传成功!视频链接:https://www.youtube.com/watch?v={response['id']}")
    return response['id']

def main():
    youtube = authenticate_youtube()
    
    # 在这里替换为你想上传的视频文件及信息
    videos_to_upload = [
        {
            'path': 'your_video_1.mp4',
            'title': '视频标题1',
            'description': '这是视频描述1',
            'tags': ['示例', '测试'],
            'privacy_status': 'private'
        },
        {
            'path': 'your_video_2.mp4',
            'title': '视频标题2',
            'description': '这是视频描述2',
            'tags': ['样例', '上传'],
            'privacy_status': 'unlisted'
        }
    ]
    
    for video_info in videos_to_upload:
        upload_video(
            youtube,
            video_info['path'],
            video_info['title'],
            video_info['description'],
            video_info.get('tags'),
            video_info.get('privacy_status', 'private')
        )

if __name__ == '__main__':
    main()