-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_video_to_youtube.py
40 lines (35 loc) · 1.24 KB
/
upload_video_to_youtube.py
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
import os
from pathlib import Path
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
client_secrets_file = "client_secrets.json"
scopes = ["https://www.googleapis.com/auth/youtube.upload"]
def upload_video(file: Path, title, thumbnail):
# Get the OAuth 2.0 credentials
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes
)
credentials = flow.run_local_server(port=8080)
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
request = youtube.videos().insert(
part="snippet,status",
body={
"snippet": {
"categoryId": "22", # Example: People & Blogs
"description": "",
"title": title,
},
"status": {"privacyStatus": "unlisted"}, # "private", "public", "unlisted"
},
media_body=str(file.absolute()),
)
response = request.execute()
print(response)
# Upload the thumbnail
if thumbnail:
youtube.thumbnails().set(
videoId=response["id"],
media_body=googleapiclient.http.MediaFileUpload(thumbnail),
).execute()
return response