Skip to content

Commit

Permalink
Added forced alignment for subtitle creation
Browse files Browse the repository at this point in the history
  • Loading branch information
SophiaPerzan committed Jul 10, 2023
1 parent 83fa3e5 commit ae46e07
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions forcedAligner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import requests
import json
from datetime import timedelta


class ForcedAligner:
def __init__(self, gentleUrl, env):
self.gentleUrl = gentleUrl
self.env = env

def align(self, audioPath, transcription, srtOutputPath):
# Open the audio file
with open(audioPath, 'rb') as audioFile:
# Send a POST request to the Gentle API
response = requests.post(
f'{self.gentleUrl}/transcriptions?async=false',
data={'transcript': transcription},
files={'audio': audioFile}
)

# Check the status code of the response
if response.status_code != 200:
print(
f"Error: received status code {response.status_code} from Gentle")
return

# Parse the alignment results
alignment = json.loads(response.text)

# Convert the alignment to SRT format and save it to the output file
with open(srtOutputPath, 'w') as srtFile:
srtIndex = 1
phrase = []
phraseCharCount = 0
for word in alignment['words']:
if word['case'] != 'success':
continue

wordLen = len(word['alignedWord'])
if phraseCharCount + wordLen <= 15:
phrase.append(word)
phraseCharCount += wordLen
else:
self.writeSrtCue(srtFile, srtIndex, phrase)
srtIndex += 1
phrase = [word]
phraseCharCount = wordLen

# Write the final phrase, if any
if phrase:
self.writeSrtCue(srtFile, srtIndex, phrase)

def writeSrtCue(self, srtFile, index, phrase):
startTime = timedelta(seconds=phrase[0]['start'])
endTime = timedelta(seconds=phrase[-1]['end'])
words = [word['alignedWord'] for word in phrase]
srtFile.write(f"{index}\n")
srtFile.write(f"{startTime} --> {endTime}\n")
srtFile.write(' '.join(words) + "\n\n")

0 comments on commit ae46e07

Please sign in to comment.