diff --git a/bcg.png b/bcg.png deleted file mode 100644 index cd44680..0000000 Binary files a/bcg.png and /dev/null differ diff --git a/generators/ideas.py b/generators/ideas.py index 2d1666e..2d3543a 100644 --- a/generators/ideas.py +++ b/generators/ideas.py @@ -5,18 +5,23 @@ from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") -subject = os.getenv("SUBJECT") with open('prompts/ideas.txt') as f: - prompt = f.read().replace('[subject]', subject) + prompt = f.read() f.close() -async def generate_ideas(path): - with open(f'{path}/ideas.json', 'r') as f: - ideas = f.read() - ides_json = json.loads(ideas) - f.close() - prmpt = prompt.replace('[existing ideas]', ideas) +async def generate_ideas(path, subject): + prmpt = prompt.replace('[subject]', subject) + try: + with open(f'{path}/ideas.json', 'r') as f: + ideas = f.read() + ides_json = json.loads(ideas) + f.close() + except: + ides_json = [] + ideas = "There are no existing ideas." + prmpt = prmpt.replace('[existing ideas]', ideas) + print(prmpt) response = await openai.ChatCompletion.acreate( model="gpt-3.5-turbo", messages=[ diff --git a/generators/miniature.py b/generators/miniature.py index 1a84df2..9fb8e20 100644 --- a/generators/miniature.py +++ b/generators/miniature.py @@ -61,7 +61,15 @@ def generate_miniature(path, title, description): generate_image(path, text1, text2) def generate_image(path, text1, text2): - bcg = Image.open("bcg.png") + path_to_bcg = path.split("/")[:-1] + path_to_bcg = "/".join(path_to_bcg) + print(path_to_bcg) + if not os.path.exists(f"{path_to_bcg}/bcg.png"): + input("bcg.png not found. Please put bcg.png in the same folder as the video."+path_to_bcg) + if not os.path.exists(f"{path_to_bcg}/bcg.png"): + input("bcg.png still not found. Exiting.") + exit() + bcg = Image.open(f"{path_to_bcg}/bcg.png") img = Image.new('RGBA', (1920, 1080)) img, textcolor1, textcolor2 = rand_gradient(img) draw = ImageDraw.Draw(img) @@ -107,6 +115,4 @@ def generate_image(path, text1, text2): else: img.paste(imgtext2, (0, 0), imgtext2) img.save(path + "/miniature.png") - return path + "/miniature.png" - -generate_image("test", "Master python loops", "Effortlessly") \ No newline at end of file + return path + "/miniature.png" \ No newline at end of file diff --git a/generators/uploader.py b/generators/uploader.py index 1204a91..c4d10a6 100644 --- a/generators/uploader.py +++ b/generators/uploader.py @@ -28,7 +28,8 @@ RETRIABLE_STATUS_CODES = [500, 502, 503, 504] CLIENT_SECRETS_FILE = 'env/client_secret.json' -SCOPES = ['https://www.googleapis.com/auth/youtube.upload', 'POST https://www.googleapis.com/upload/youtube/v3/thumbnails/set', 'https://www.googleapis.com/auth/youtube.force-ssl'] +#SCOPES = ['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/upload/youtube/v3/thumbnails/set', 'https://www.googleapis.com/auth/youtube.force-ssl'] +SCOPES = ['https://www.googleapis.com/auth/youtube'] API_SERVICE_NAME = 'youtube' API_VERSION = 'v3' @@ -36,9 +37,9 @@ VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') # Authorize the request and store authorization credentials. -def get_authenticated_service(): - if os.path.exists('env/credentials.json'): - with open('env/credentials.json') as json_file: +def get_authenticated_service(credentialsPath=""): + if os.path.exists(f'{credentialsPath}/credentials.json'): + with open(f'{credentialsPath}/credentials.json') as json_file: data = json.load(json_file) credentials = google.oauth2.credentials.Credentials( token=data['token'], @@ -52,7 +53,7 @@ def get_authenticated_service(): flow = InstalledAppFlow.from_client_secrets_file( CLIENT_SECRETS_FILE, SCOPES) credentials = flow.run_local_server() - with open('env/credentials.json', 'w') as outfile: + with open(f'{credentialsPath}/credentials.json', 'w') as outfile: outfile.write(credentials.to_json()) return build(API_SERVICE_NAME, API_VERSION, credentials=credentials) @@ -67,10 +68,12 @@ def initialize_upload(youtube, options): title=options['title'], description=options['description'], tags=tags, - categoryId=options['category'] + categoryId=options['category'], + defaultLanguage='en' ), status=dict( - privacyStatus=options['privacyStatus'] + privacyStatus=options['privacyStatus'], + selfDeclaredMadeForKids=False ) ) @@ -81,7 +84,8 @@ def initialize_upload(youtube, options): media_body=MediaFileUpload(options['file'], chunksize=-1, resumable=True) ) - resumable_upload(insert_request) + videoid = resumable_upload(insert_request) + return videoid def resumable_upload(request): @@ -96,6 +100,7 @@ def resumable_upload(request): if 'id' in response: print('Video id "%s" was successfully uploaded.' % response['id']) + return response['id'] else: exit('The upload failed with an unexpected response: %s' % response) except HttpError as e: @@ -118,20 +123,25 @@ def resumable_upload(request): print('Sleeping %f seconds and then retrying...' % sleep_seconds) time.sleep(sleep_seconds) - -if __name__ == '__main__': - sample_options = { - 'file': './test.mp4', - 'title': 'Test Title', - 'description': 'Test Description', - 'category': 22, - 'keywords': 'test, video', - 'privacyStatus': 'private' +def upload_video(path, title, description, category, keywords, privacyStatus='private', credentials_path=""): + options = { + 'file': path +"/montage.mp4", + 'title': title, + 'description': description, + 'category': category, + 'keywords': keywords, + 'privacyStatus': privacyStatus } - - youtube = get_authenticated_service() - + youtube = get_authenticated_service(credentials_path) try: - initialize_upload(youtube, sample_options) + videoid = initialize_upload(youtube, options) except HttpError as e: - print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) \ No newline at end of file + print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) + upload_thumbnail(videoid, path + "/miniature.png", credentials_path) + +def upload_thumbnail(video_id, file, credentials_path=""): + youtube = get_authenticated_service(credentials_path) + youtube.thumbnails().set( + videoId=video_id, + media_body=file + ).execute() \ No newline at end of file diff --git a/main.py b/main.py index eeae410..6d945a5 100644 --- a/main.py +++ b/main.py @@ -7,10 +7,12 @@ from generators.ideas import generate_ideas from generators.script import generate_script from generators.montage import mount, prepare, translate from generators.miniature import generate_miniature +from generators.uploader import upload_video logging.basicConfig(level=logging.INFO) async def main(): + if not os.path.exists('videos'): os.makedirs('videos') with open('env/subjects.txt', 'r', encoding='utf-8') as f: subjects = f.read().splitlines() f.close() @@ -18,11 +20,11 @@ async def main(): print(str(i) + ". " + subjects[i]) subject = int(input("Which subject do you want to generate ideas for? (enter the number): ")) subject = subjects[subject] - subjectdirpath = subject[:25].replace(" ", "_").replace(":", "") + subjectdirpath = "videos/" + subject[:25].replace(" ", "_").replace(":", "") if not os.path.exists(subjectdirpath): os.makedirs(subjectdirpath) if input("Do you want to generate new ideas? (y/n)") == "y": - await generate_ideas(subjectdirpath) - with open('ideas/ideas.json', 'r', encoding='utf-8') as f: + await generate_ideas(subjectdirpath, subject) + with open(subjectdirpath + '/ideas.json', 'r', encoding='utf-8') as f: ideas = json.load(f) f.close() for i in range(len(ideas)): @@ -55,6 +57,7 @@ async def main(): f.write(f"Titre: {transtitle}\nDescription: {transdesc}\nCrédits musicaux: {credits}") f.close() generate_miniature(path, title=idea['title'], description=idea['description']) + upload_video(path, idea['title'], idea['description'], 28, "", "private", subjectdirpath) print(f"Your video is ready! You can find it in {path}.") if __name__ == "__main__": diff --git a/montage.mp4 b/montage.mp4 deleted file mode 100644 index bfbc0a3..0000000 Binary files a/montage.mp4 and /dev/null differ diff --git a/nobg_logo_2.png b/nobg_logo_2.png deleted file mode 100644 index d4d4ce7..0000000 Binary files a/nobg_logo_2.png and /dev/null differ diff --git a/prompts/ideas.txt b/prompts/ideas.txt index f4dba71..94e56da 100644 --- a/prompts/ideas.txt +++ b/prompts/ideas.txt @@ -6,12 +6,12 @@ Here is an example of the output: ``` [ { - "title": "Python Tutorial for Beginners", - "description": "A video for beginners to learn Python. The following topics are covered: variables, data types, functions, classes, and more." + "title": "TITLE OF THE VIDEO", + "description": "A video about something. Concept1 and concept2 are explained. Concept3 is also explained a bit." }, { - "title": "DRAMA: MrBeast did WHAT?!", - "description": "MrBeast did something crazy. You won't believe what he did. Watch the video to find out." + "title": "TITLE OF THE VIDEO", + "description": "A video about something. Concept1 and concept2 are explained. Concept3 is also explained a bit." } ] ``` diff --git a/prompts/script.txt b/prompts/script.txt index b530a2b..681e1da 100644 --- a/prompts/script.txt +++ b/prompts/script.txt @@ -27,6 +27,10 @@ Your video will be detailed, long and very complete. Here is an example: { "spoken":"This is a latex formula wich is very important. It is the formula of the integral of x squared from 0 to infinity. You can see it on the screen.", "markdown":"$$\n\\int_0^\\infty x^2 dx\n$$" +}, +{ + "spoken":"tHanks for watching this video. If you liked it, please like, share and subscribe. See you in the next video.", + "image":"thanks+goodbye" } ]