Skip to content

Commit

Permalink
Self assemble str files from the already available audio clips and th…
Browse files Browse the repository at this point in the history
…e sentences.
  • Loading branch information
stumi01 committed Feb 5, 2024
1 parent c3ab93f commit 0f896ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def generate():
final_audio.write_audiofile(tts_path)

# Generate subtitles
subtitles_path = generate_subtitles(tts_path)
subtitles_path = generate_subtitles(sentences=sentences, audio_clips=paths)

# Concatenate videos
temp_audio = AudioFileClip(tts_path)
Expand Down
50 changes: 34 additions & 16 deletions Backend/video.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import uuid
from datetime import timedelta

import requests
import srt_equalizer
import assemblyai as aai

from typing import List
from moviepy.editor import *
Expand All @@ -15,6 +16,7 @@

ASSEMBLY_AI_API_KEY = os.getenv("ASSEMBLY_AI_API_KEY")


def save_video(video_url: str, directory: str = "../temp") -> str:
"""
Saves a video from a given URL and returns the path to the video.
Expand All @@ -32,33 +34,49 @@ def save_video(video_url: str, directory: str = "../temp") -> str:

return video_path

def generate_subtitles(audio_path: str) -> str:

def generate_subtitles(sentences: list[str], audio_clips: list[AudioFileClip]) -> str:
"""
Generates subtitles from a given audio file and returns the path to the subtitles.
Args:
audio_path (str): The path to the audio file to generate subtitles from.
sentences (list[str]): all the sentences said out loud in the audio clips
audio_clips (list[AudioFileClip]): all the individual audio clips which will make up the final audio track
Returns:
str: The path to the generated subtitles.
"""
def equalize_subtitles(srt_path: str, max_chars: int = 10) -> None:
# Equalize subtitles
srt_equalizer.equalize_srt_file(srt_path, srt_path, max_chars)

aai.settings.api_key = ASSEMBLY_AI_API_KEY

transcriber = aai.Transcriber()
def convert_to_srt_time_format(total_seconds):
# Convert total seconds to the SRT time format: HH:MM:SS,mmm
if total_seconds == 0:
return "0:00:00,0"
return str(timedelta(seconds=total_seconds))[:-3].replace('.', ',')

transcript = transcriber.transcribe(audio_path)
def equalize_subtitles(srt_path: str, max_chars: int = 10) -> None:
# Equalize subtitles
srt_equalizer.equalize_srt_file(srt_path, srt_path, max_chars)

# Save subtitles
subtitles_path = f"../subtitles/{uuid.uuid4()}.srt"

subtitles = transcript.export_subtitles_srt()
start_time = 0
subtitles = []
print(colored("[+] Creating subtitles", "blue"))

with open(subtitles_path, "w") as f:
f.write(subtitles)
for i, (sentence, audio_clip) in enumerate(zip(sentences, audio_clips), start=1):
duration = audio_clip.duration
end_time = start_time + duration

# Format: subtitle index, start time --> end time, sentence
subtitle_entry = f"{i}\n{convert_to_srt_time_format(start_time)} --> {convert_to_srt_time_format(end_time)}\n{sentence}\n"
subtitles.append(subtitle_entry)
print(colored("[+] subtitle_entry: ", "blue"), subtitle_entry)

start_time += duration # Update start time for the next subtitle

with open(subtitles_path, "w") as file:
file.write("\n".join(subtitles))

# Equalize subtitles
equalize_subtitles(subtitles_path)
Expand All @@ -68,7 +86,6 @@ def equalize_subtitles(srt_path: str, max_chars: int = 10) -> None:
return subtitles_path



def combine_videos(video_paths: List[str], max_duration: int) -> str:
"""
Combines a list of videos into one video and returns the path to the combined video.
Expand Down Expand Up @@ -97,7 +114,7 @@ def combine_videos(video_paths: List[str], max_duration: int) -> str:
# so we need to resize them
clip = crop(clip, width=1080, height=1920, \
x_center=clip.w / 2, \
y_center=clip.h / 2)
y_center=clip.h / 2)
clip = clip.resize((1080, 1920))

clips.append(clip)
Expand All @@ -108,6 +125,7 @@ def combine_videos(video_paths: List[str], max_duration: int) -> str:

return combined_video_path


def generate_video(combined_video_path: str, tts_path: str, subtitles_path: str) -> str:
"""
This function creates the final video, with subtitles and audio.
Expand All @@ -122,7 +140,7 @@ def generate_video(combined_video_path: str, tts_path: str, subtitles_path: str)
"""
# Make a generator that returns a TextClip when called with consecutive
generator = lambda txt: TextClip(txt, font=f"../fonts/bold_font.ttf", fontsize=100, color="#FFFF00",
stroke_color="black", stroke_width=5)
stroke_color="black", stroke_width=5)

# Burn the subtitles into the video
subtitles = SubtitlesClip(subtitles_path, generator)
Expand Down

0 comments on commit 0f896ea

Please sign in to comment.