feat(audio_prompts): add default audio prompts for narrator

feat(audio_prompts): add en_narrator_deep audio prompt for narrator
feat(audio_prompts): add en_narrator_light_bg audio prompt for narrator
fix(video.py): fix indentation and add prompt for generating thumbnail
fix(montage.py): fix indentation and add prompt for generating thumbnail
fix(montage.py): fix image download for wikimage slides

fix(speak.py): remove unused import statement
fix(speak.py): remove unused variable 'fakenames'
feat(speak.py): add function 'remove_blank_moments' to remove silent parts from audio file
feat(speak.py): add function 'optimize_string_groups' to optimize string groups for audio generation
fix(speak.py): fix comment indentation in 'generate_voice' function
fix(speak.py): remove unused imports in 'generate_voice' function
fix(speak.py): remove unused variable 'reduced_noise' in 'generate_voice' function
fix(speak.py): remove unused import statements in 'generate_voice' function
fix(speak.py): remove unused import statement for 'logging' module
fix(speak.py): remove unused print statements in 'main' function
fix(speak.py): remove unused import statement for 'logging' module
fix(speak.py): remove unused print statements in 'main' function
fix(speak.py):

fix(wiki_downloader.py): fix Google search URL to include correct query parameter
fix(wiki_downloader.py): reduce sleep time after page load to 1 second
fix(wiki_downloader.py): increase sleep time after image click to 5 seconds
This commit is contained in:
Paillat
2023-07-02 11:17:10 +02:00
parent f1de2ad596
commit f7835f6604
13 changed files with 206 additions and 114 deletions

View File

@@ -10,7 +10,7 @@ from moviepy.editor import concatenate_videoclips, CompositeAudioClip, concatena
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.audio.fx.all import volumex, audio_fadein, audio_fadeout # type: ignore
from utils.misc import getenv
from utils.wiki_downloader import download_image as wiki_download_image
unsplash_access = getenv("unsplash_access_key")
if not unsplash_access:
@@ -22,64 +22,82 @@ async def prepare(path):
script = json.load(f)
f.close()
if not os.path.exists(path + "/slides"): os.mkdir(path + "/slides")
fresh = False
if not os.path.exists(path + "/audio"):
os.mkdir(path + "/audio")
fresh = True
with open("prompts/marp.md", 'r', encoding='utf-8') as f:
if not os.path.exists(path + "/audio"): os.mkdir(path + "/audio")
choosen_voice = random.choice(voices)
with open(os.path.join(os.getcwd(), "prompts", "marp.md"), 'r', encoding='utf-8') as f:
marp = f.read()
f.close()
if fresh:
choosen_voice = random.choice(voices)
generator = VoiceGenerator(speaker=choosen_voice)
for i in range(len(script)):
audio_path = path + "/audio/audio" + str(i) + ".wav"
if not os.path.exists(audio_path):
generator.generate_voice(audio_path, script[i]['spoken'])
if "image" in script[i]:
if os.path.exists(path + "/slides/assets/slide" + str(i) + ".md"):
#skip this slide
f.close()
for i in range(len(script)):
audio_path = os.path.join(path, "audio", "audio" + str(i) + ".wav")
generator = None
if not os.path.exists(audio_path):
if not generator:
generator = VoiceGenerator(speaker=choosen_voice)
print("Generating audio for slide " + str(i))
generator.generate_voice(audio_path, script[i]['spoken'])
if "image" in script[i]:
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")) and os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".png")):
#skip this slide
continue
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
url= unsplash_url + script[i]['image'].replace("+", ",")
r = requests.get(url)
real_url = r.json()['urls']['raw']
with open(path + "/slides/assets/slide" + str(i) + ".jpg", 'wb') as f:
f.write(requests.get(real_url).content)
f.close()
content = marp + f"\n\n![bg 70%](assets/slide{i}.jpg)"
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
elif "wikimage" in script[i]:
if os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".md")) and os.path.exists(os.path.join(path, "slides", "slide" + str(i) + ".png")):
#skip this slide
continue
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
r = 0
while True:
try:
print("Trying to download image for slide " + str(i))
wiki_download_image(script[i]['wikimage'], os.path.abspath(os.path.join(path, "slides", "assets", "slide" + str(i) + ".jpg")))
print("Downloaded image for slide with wikiimage " + str(i))
break
except:
r += 1
if r > 5:
break
continue
if not os.path.exists(path + "/slides/assets"):
os.mkdir(path + "/slides/assets")
url= unsplash_url + script[i]['image']
r = requests.get(url)
real_url = r.json()['urls']['raw']
with open(path + "/slides/assets/slide" + str(i) + ".jpg", 'wb') as f:
f.write(requests.get(real_url).content)
f.close()
content = marp + f"\n\n![bg 70%](assets/slide{i}.jpg)"
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
elif "markdown" in script[i]:
if os.path.exists(path + "/slides/slide" + str(i) + ".md"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n" + script[i]['markdown'])
elif "huge" in script[i]:
#use fit
if os.path.exists(path + "/slides/slide" + str(i) + ".md"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n# <!-- fit --> " + script[i]['huge'])
else:
if os.path.exists(path + "/slides/slide" + str(i) + ".md"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n") # blank slide
content = marp + f"\n\n![bg 70%](assets/slide{i}.jpg)"
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(content)
elif "markdown" in script[i]:
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n" + script[i]['markdown'])
elif "huge" in script[i]:
#use fit
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n# <!-- fit --> " + script[i]['huge'])
else:
if os.path.exists(path + "/slides/slide" + str(i) + ".md") and os.path.exists(path + "/slides/slide" + str(i) + ".png"):
#skip this slide
continue
with open(path + "/slides/slide" + str(i) + ".md", 'w', encoding='utf-8') as f:
f.write(marp + "\n\n") # blank slide
for i in range(len(script)):
markdown_path = os.path.join(path, f"slides/slide{i}.md")
markdown_path = os.path.abspath(markdown_path)
image_path = os.path.join(path, f"slides/slide{i}.png")
image_path = os.path.abspath(image_path)
if os.path.exists(markdown_path):
#skip this slide
continue
command = f'marp.exe "{markdown_path}" -o "{image_path}" --allow-local-files'
os.system(command)
if not os.path.exists(image_path):
command = f'marp.exe "{markdown_path}" -o "{image_path}" --allow-local-files'
os.system(command)
return script
def convert_seconds_to_time_string(seconds):
@@ -113,8 +131,6 @@ async def mount(path, script):
])
length = complete_audio.duration
total_length += length
print(script[i])
print(script[i]['spoken'])
srt = subs(length, total_length, script[i]['spoken'], srt, i)
slide = ImageClip(path + "/slides/slide" + str(i) + ".png").set_duration(length)
slide = slide.set_audio(complete_audio)
@@ -122,7 +138,7 @@ async def mount(path, script):
randmusic = random.choice(os.listdir("musics"))
while randmusic.endswith(".txt"): randmusic = random.choice(os.listdir("musics"))
randpath = "musics/" + randmusic
music = AudioFileClip(randpath).set_duration(total_length)
music = AudioFileClip(randpath)
music = audio_fadein(music, 20)
music = audio_fadeout(music, 20)
music = volumex(music, 0.2)
@@ -131,6 +147,7 @@ async def mount(path, script):
for i in range(int(total_length / music.duration)):
musics.append(music)
music = concatenate_audioclips(musics)
music = music.set_duration(total_length)
final_clip = concatenate_videoclips(clips, method="compose")
existing_audio = final_clip.audio
final_audio = CompositeAudioClip([existing_audio, music])
@@ -142,4 +159,4 @@ async def mount(path, script):
f.close()
return music_credit or ""
else:
return None
return ""