-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added forced alignment for subtitle creation
- Loading branch information
1 parent
83fa3e5
commit ae46e07
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |