Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to allow for max word count per subtitle #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example.config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ task = transcribe

[OPTIONS]
generate_transcript = True
max_words_per_subtitle = 5

; use this option to generate summary
generate_summary = True
Expand Down
32 changes: 21 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def read_config(config_file):
openai_model = config.get("OPENAI", "model")
text_chunk_size = config.getint("OPTIONS", "text_chunk_size")
max_chunk = config.getint("OPTIONS", "max_chunk")
max_words_per_subtitle = config.getint("OPTIONS", "max_words_per_subtitle")

return (
input_folder,
Expand All @@ -50,7 +51,8 @@ def read_config(config_file):
openai_api_base,
openai_model,
text_chunk_size,
max_chunk
max_chunk,
max_words_per_subtitle
)


Expand Down Expand Up @@ -95,12 +97,11 @@ def convert_to_mp3(video_file, audio_file):
print(f"Conversion of {video_file} success")
return True


def generate_subtitles(model, task, audio_file, subtitle_file, transcript_file=None):
def generate_subtitles(model, task, audio_file, subtitle_file, max_words_per_subtitle, transcript_file=None):
"""
Transcribes the specified audio file using the Faster-Whisper model, generates an SRT subtitle file
at the specified output file path, and optionally generates a transcript file. Returns True if the
subtitle generation is successful, False otherwise.
at the specified output file path, and optionally generates a transcript file. Adjusts subtitles to not exceed
the max words per subtitle. Returns True if successful, False otherwise.
"""
if os.path.exists(subtitle_file):
print(f"Skipping {audio_file} - subtitles file already exists")
Expand All @@ -122,11 +123,18 @@ def generate_subtitles(model, task, audio_file, subtitle_file, transcript_file=N
texts.append(text)

if text:
# Create a subtitle object for the segment
subtitle = srt.Subtitle(
index=i + 1, start=start_time, end=end_time, content=text
)
subtitles.append(subtitle)
words = text.split()
for j in range(0, len(words), max_words_per_subtitle):
chunk = " ".join(words[j:j+max_words_per_subtitle])
sub_start_time = start_time + datetime.timedelta(seconds=j * (segment.end - segment.start) / len(words))
sub_end_time = start_time + datetime.timedelta(seconds=min(j+max_words_per_subtitle, len(words)) * (segment.end - segment.start) / len(words))
subtitle = srt.Subtitle(
index=len(subtitles) + 1,
start=sub_start_time,
end=sub_end_time,
content=chunk
)
subtitles.append(subtitle)

# Write the subtitles to the SRT file
with open(subtitle_file, "w", encoding="utf-8") as f:
Expand Down Expand Up @@ -276,7 +284,8 @@ def summarize(
openai_api_base,
openai_model,
text_chunk_size,
max_chunk
max_chunk,
max_words_per_subtitle
) = read_config(config_file)

logging.basicConfig()
Expand Down Expand Up @@ -315,6 +324,7 @@ def summarize(
task,
audio_file,
subtitle_file,
max_words_per_subtitle,
transcript_file,
)
try:
Expand Down