Skip to content

Commit

Permalink
Merge pull request #23 from tmoroney/testing
Browse files Browse the repository at this point in the history
Added ability to select a specific template text and fixed auto translation bug
  • Loading branch information
tmoroney committed Mar 22, 2024
2 parents 1305452 + cefc019 commit b4e708f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

settings.txt
51 changes: 37 additions & 14 deletions auto-subs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
ui.Label({ 'Text': "Video Track for Subtitles", 'Weight': 0, 'Font': ui.Font({ 'PixelSize': 14 }) }),
ui.SpinBox({"ID": "TrackSelector", "Min": 1, "Value": 2, 'MaximumSize': [2000, 40]}),
ui.VGap(1),
ui.Label({ 'Text': "Select Template Text", 'Weight': 0, 'Font': ui.Font({ 'PixelSize': 14 })}),
ui.ComboBox({"ID": "Template", 'MaximumSize': [2000, 55]}),
ui.VGap(1),
ui.Label({ 'Text': "Transcription Model", 'Weight': 0, 'Font': ui.Font({ 'PixelSize': 14 })}),
ui.ComboBox({"ID": "WhisperModel", 'MaximumSize': [2000, 55]}),
ui.VGap(3),
Expand Down Expand Up @@ -239,7 +242,7 @@ def OnTranscribe(ev):
elif itm['WhisperModel'].CurrentIndex == 4:
chosenModel = "medium"

if itm['SubsOutput'].CurrentIndex == 1: # use english only model
if itm['SubsOutput'].CurrentIndex == 0: # use english only model
chosenModel = chosenModel + ".en"

print("Using model -> [", chosenModel, "]")
Expand Down Expand Up @@ -289,17 +292,17 @@ def OnTranscribe(ev):
model = stable_whisper.load_model(chosenModel) # load whisper transcription model

# TRANSCRIBE AUDIO TO SRT FILE
if itm['SubsOutput'].CurrentIndex == 3: # translate to english
result = model.transcribe(location, fp16=False, regroup=True, only_voice_freq=True, task = 'translate')
if itm['SubsOutput'].CurrentIndex == 0 or itm['SubsOutput'].CurrentIndex == 1: # subtitles in original language
result = model.transcribe(location, fp16=False, regroup=True, only_voice_freq=True) # transcribe audio file
(
result
.split_by_punctuation([('.', ' '), '。', '?', '?', ',', ','])
.split_by_gap(itm['SplitByGap'].Value)
.merge_by_gap(.10, max_words=3)
.split_by_length(max_words=itm['MaxWords'].Value, max_chars=itm['MaxChars'].Value)
)
else: # subtitles in original language
result = model.transcribe(location, fp16=False, regroup=True, only_voice_freq=True) # transcribe audio file
elif itm['SubsOutput'].CurrentIndex == 2: # translate to english
result = model.transcribe(location, fp16=False, regroup=True, only_voice_freq=True, task = 'translate')
(
result
.split_by_punctuation([('.', ' '), '。', '?', '?', ',', ','])
Expand Down Expand Up @@ -458,13 +461,8 @@ def OnGenerate(ev):
print("Found", len(subs), "subtitles in SRT file")

# ADD TEXT+ TO TIMELINE
templateText = None
for item in items:
itemName = item.GetName();
if itemName == "Text+" or itemName == "Fusion Title" : # Find Text+ in Media Pool
templateText = item
print("Found Text+ in Media Pool!")
break
templateText = mediaPoolItemsList[itm['Template'].CurrentIndex] # get selected Text+ template
print(templateText.GetClipProperty()['Clip Name'], "selected as template")
if not templateText:
print("No Text+ found in Media Pool")
itm['DialogBox'].Text = "No Text+ found in Media Pool!"
Expand Down Expand Up @@ -617,6 +615,31 @@ def loadSettings():
itm['FormatText'].CurrentIndex = format_text
itm['RemovePunc'].Checked = remove_punc

# Search Media Pool for Text+ template
mediaPoolItemsList = []
def searchMediaPool():
mediaPool = project.GetMediaPool()
folder = mediaPool.GetRootFolder()
items = folder.GetClipList()
itm['Template'].Clear()
recursiveSearch(folder)

def recursiveSearch(folder):
items = folder.GetClipList()
for item in items:
itemType = item.GetClipProperty()["Type"]
if itemType == "Generator":
itemName = item.GetName()
clipName = item.GetClipProperty()['Clip Name']
itm['Template'].AddItem(clipName)
if re.search('text|Text|title|Title|subtitle|Subtitle', itemName) or re.search('text|Text|title|Title|subtitle|Subtitle', clipName):
itm['Template'].CurrentIndex = len(mediaPoolItemsList) - 1 # set default template to Text+
mediaPoolItemsList.append(item)
subfolders = folder.GetSubFolderList()
for subfolder in subfolders:
recursiveSearch(subfolder)
return

# Add the items to the FormatText ComboBox menu
itm['FormatText'].AddItem("None")
itm['FormatText'].AddItem("all lowercase")
Expand Down Expand Up @@ -662,8 +685,8 @@ def loadSettings():
win.On.RefreshSubs.Clicked = OnPopulateSubs # refresh subtitles
# Note: there appears to be multiple ways to define event handlers

# Load Settings from file
loadSettings();
loadSettings() # Load Settings from file
searchMediaPool() # Search media pool for possible templates

# Main dispatcher loop
win.Show()
Expand Down
22 changes: 22 additions & 0 deletions search-media-pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
projectManager = resolve.GetProjectManager()
project = projectManager.GetCurrentProject()
mediaPool = project.GetMediaPool()
folder = mediaPool.GetRootFolder()
#items = folder.GetClipList()

clipList = []
def recursiveSearch(folder):
items = folder.GetClipList()
for i in items:
clipList.append(i)
subfolders = folder.GetSubFolderList()
for subfolder in subfolders:
recursiveSearch(subfolder)
return

print("Searching media pool...")
recursiveSearch(folder)
print("Found " + str(len(clipList)) + " items in media pool")
for i in clipList:
print("Name:", i.GetName())
print(i.GetClipProperty())

0 comments on commit b4e708f

Please sign in to comment.