-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube_uploader.py
More file actions
115 lines (89 loc) · 3.63 KB
/
Copy pathyoutube_uploader.py
File metadata and controls
115 lines (89 loc) · 3.63 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import google.auth.transport.requests
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
def authenticate_youtube():
flow = InstalledAppFlow.from_client_secrets_file(
"client_secret.json", SCOPES
)
credentials = flow.run_local_server(port=8080)
youtube = build("youtube", "v3", credentials=credentials)
return youtube
def upload_video(file_path, title, description, tags=None, privacy="public"):
youtube = authenticate_youtube()
body = {
"snippet": {
"title": title,
"description": description,
"tags": tags or [],
"categoryId": "22" # People & Blogs
},
"status": {
"privacyStatus": privacy
}
}
media = MediaFileUpload(file_path, chunksize=-1, resumable=True, mimetype="video/*")
request = youtube.videos().insert(
part="snippet,status",
body=body,
media_body=media
)
response = request.execute()
print(f"✅ Video uploaded: https://youtu.be/{response['id']}")
import os
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
# === CONFIGURATION ===
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
VIDEO_DIR = "videos" # Folder where your videos are stored
PRIVACY = "unlisted" # public | unlisted | private
def authenticate_youtube():
flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", SCOPES)
credentials = flow.run_local_server(port=8080)
youtube = build("youtube", "v3", credentials=credentials)
return youtube
def get_latest_video(folder_path):
video_files = [f for f in os.listdir(folder_path)
if f.lower().endswith(('.mp4', '.mov', '.avi', '.mkv'))]
if not video_files:
raise FileNotFoundError("No video files found in the folder.")
# Sort by creation time, select the latest
video_files.sort(key=lambda f: os.path.getctime(os.path.join(folder_path, f)))
latest = video_files[-1]
return os.path.join(folder_path, latest)
def format_title_and_description(filename):
title = os.path.splitext(os.path.basename(filename))[0].replace('_', ' ').title()
description = f"This video titled \"{title}\" was uploaded automatically using Python."
return title, description
def upload_video_to_youtube(youtube, file_path, title, description, tags=None):
body = {
"snippet": {
"title": title,
"description": description,
"tags": tags or [],
"categoryId": "22" # People & Blogs
},
"status": {
"privacyStatus": PRIVACY
}
}
media = MediaFileUpload(file_path, chunksize=-1, resumable=True, mimetype="video/*")
request = youtube.videos().insert(
part="snippet,status",
body=body,
media_body=media
)
response = request.execute()
print(f"✅ Video uploaded: https://youtu.be/{response['id']}")
def main():
print("🚀 Starting YouTube AutoUploader...")
video_path = get_latest_video(VIDEO_DIR)
title, description = format_title_and_description(video_path)
print(f"📄 Uploading: {title}")
youtube = authenticate_youtube()
upload_video_to_youtube(youtube, video_path, title, description, tags=["python", "automation", "upload"])
if __name__ == "__main__":
main()