-
Notifications
You must be signed in to change notification settings - Fork 47
/
shorts.py
83 lines (69 loc) · 3.22 KB
/
shorts.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
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
# Import everything
from dotenv import load_dotenv
import random
import os
import openai
from gtts import gTTS
from moviepy.editor import *
import moviepy.video.fx.crop as crop_vid
load_dotenv()
# Ask for video info
title = input("\nEnter the name of the video > ")
option = input('Do you want AI to generate content? (yes/no) > ')
if option == 'yes':
# Generate content using OpenAI API
theme = input("\nEnter the theme of the video > ")
### MAKE .env FILE AND SAVE YOUR API KEY ###
openai.api_key = os.environ["OPENAI_API"]
response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt=f"Act as an script writer who writes engaging and professional scripts for tiktok shorts. you never do a grammatical mistake and write very engaging scripts that touches the viewers' attention. At the beginning of the video you never forget to add a viral hook that will catch the viewers attention and will break the scroll. Your scripts are so loved by users that they subscribe and follow the channel. Don't mention any channel's name but at appropriate point tell the viewer to like the video and follow or subscribe the channel or account. (Remember tiktok shorts have max limit of 1 min). Now Generate content on - \"{theme}\"",
temperature=0.7,
max_tokens=200,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print(response.choices[0].text)
yes_no = input('\nIs this fine? (yes/no) > ')
if yes_no == 'yes':
content = response.choices[0].text
else:
content = input('\nEnter > ')
else:
content = input('\nEnter the content of the video > ')
# Create the directory
if os.path.exists('generated') == False:
os.mkdir('generated')
# Generate speech for the video
speech = gTTS(text=content, lang='en', tld='ca', slow=False)
speech.save("generated/speech.mp3")
gp = random.choice(["1", "2"])
start_point = random.randint(1, 480)
audio_clip = AudioFileClip("generated/speech.mp3")
if (audio_clip.duration + 1.3 > 58):
print('\nSpeech too long!\n' + str(audio_clip.duration) + ' seconds\n' + str(audio_clip.duration + 1.3) + ' total')
exit()
print('\n')
### VIDEO EDITING ###
# Trim a random part of minecraft gameplay and slap audio on it
video_clip = VideoFileClip("gameplay/gameplay_" + gp + ".mp4").subclip(start_point, start_point + audio_clip.duration + 1.3)
final_clip = video_clip.set_audio(audio_clip)
# Resize the video to 9:16 ratio
w, h = final_clip.size
target_ratio = 1080 / 1920
current_ratio = w / h
if current_ratio > target_ratio:
# The video is wider than the desired aspect ratio, crop the width
new_width = int(h * target_ratio)
x_center = w / 2
y_center = h / 2
final_clip = crop_vid.crop(final_clip, width=new_width, height=h, x_center=x_center, y_center=y_center)
else:
# The video is taller than the desired aspect ratio, crop the height
new_height = int(w / target_ratio)
x_center = w / 2
y_center = h / 2
final_clip = crop_vid.crop(final_clip, width=w, height=new_height, x_center=x_center, y_center=y_center)
# Write the final video
final_clip.write_videofile("generated/" + title + ".mp4", codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)