-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_storage.py
More file actions
76 lines (62 loc) · 4.15 KB
/
Copy pathazure_storage.py
File metadata and controls
76 lines (62 loc) · 4.15 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
import os
from azure.storage.blob import BlobServiceClient
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
container_name = os.getenv("AZURE_CONTAINER_NAME", "youtubecontainer333992")
def upload_to_azure_blob(title, channel, thumbnailInput):
try:
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Create a "folder" in the container with the title name
folder_name = title
blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" + folder_name + '/')
blob_client.upload_blob('', overwrite=True)
# Upload video file
video_file_name = title + '.mp4'
video_blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" +folder_name + '/' + video_file_name)
mp4name = str(title).capitalize().replace("_", " ") + ".mp4"
video_file_path = os.path.join('output', 'videos', title, mp4name)
with open(video_file_path, "rb") as video_data:
video_blob_client.upload_blob(video_data, overwrite=True) # Overwrite existing blob if it exists
print("\033[92mVideo uploaded successfully to Azure Blob Storage\033[0m")
if thumbnailInput == "yes":
# Upload thumbnail file
thumbnail_file_name = title + '.jpg'
thumbnail_blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" + folder_name + '/' + thumbnail_file_name)
thumbnail_file_path = os.path.join('output', 'thumbnails', title + '.jpg')
with open(thumbnail_file_path, "rb") as thumbnail_data:
thumbnail_blob_client.upload_blob(thumbnail_data, overwrite=True) # Overwrite existing blob if it exists
print("\033[92mThumbnail uploaded successfully to Azure Blob Storage\033[0m")
else:
print("\n\033[94mNo thumbnail found, skipping thumbnail upload.\033[0m\n")
# Upload subtitles.srt file
srt_file_name = title + '.jpg'
srt_blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" +folder_name + '/' + srt_file_name)
srt_file_path = os.path.join('output', 'videos', title, 'subtitles.srt')
with open(srt_file_path, "rb") as thumbnail_data:
srt_blob_client.upload_blob(thumbnail_data, overwrite=True) # Overwrite existing blob if it exists
print("\033[92mSubtitles uploaded successfully to Azure Blob Storage\033[0m")
# Upload description.txt file
print("\nSkipping description.txt...\n")
# desc_file_name = 'description.txt'
# desc_blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" +folder_name + '/' + desc_file_name)
# desc_file_path = os.path.join('output', 'videos', title, 'description.txt')
# with open(desc_file_path, "rb") as desc_data:
# desc_blob_client.upload_blob(desc_data, overwrite=True) # Overwrite existing blob if it exists
# print("\033[92mDescription uploaded successfully to Azure Blob Storage\033[0m")
# Upload tags.txt file
tags_file_name = 'tags.txt'
tags_blob_client = blob_service_client.get_blob_client(container=container_name, blob=channel + "/" +folder_name + '/' + tags_file_name)
tags_file_path = os.path.join('output', 'videos', title, 'tags.txt')
with open(tags_file_path, "rb") as tags_data:
tags_blob_client.upload_blob(tags_data, overwrite=True) # Overwrite existing blob if it exists
print("\033[92mTags uploaded successfully to Azure Blob Storage\033[0m")
# # Remove default video
# default_video_file_path = os.path.join('output', 'videos', title, 'finished.mp4')
# if os.path.exists(default_video_file_path):
# os.remove(default_video_file_path)
# print(f"{default_video_file_path} has been successfully removed.")
# else:
# print(f"The file {default_video_file_path} does not exist.")
except Exception as e:
print(f"Error uploading files to Azure Blob Storage: {e}")