Skip to content

Commit

Permalink
specify vertical_alignment instead of marginv in ass_file_config
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Rastorgueva <erastorgueva@nvidia.com>
  • Loading branch information
erastorgueva-nv committed Aug 3, 2023
1 parent ba7b2c1 commit 7c72a4b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
5 changes: 4 additions & 1 deletion tools/nemo_forced_aligner/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CTMFileConfig:
@dataclass
class ASSFileConfig:
fontsize: int = 20
marginv: int = 20
vertical_alignment: str = "center"
# if resegment_text_to_fill_space is True, the ASS files will use new segments
# such that each segment will not take up more than (approximately) max_lines_per_segment
# when the ASS file is applied to a video
Expand Down Expand Up @@ -183,6 +183,9 @@ def main(cfg: AlignmentConfig):
if cfg.ctm_file_config.minimum_timestamp_duration < 0:
raise ValueError("cfg.minimum_timestamp_duration cannot be a negative number")

if cfg.ass_file_config.vertical_alignment not in ["top", "center", "bottom"]:
raise ValueError("cfg.ass_file_config.vertical_alignment must be one of 'top', 'center' or 'bottom'")

for rgb_list in [
cfg.ass_file_config.text_already_spoken_rgb,
cfg.ass_file_config.text_already_spoken_rgb,
Expand Down
29 changes: 24 additions & 5 deletions tools/nemo_forced_aligner/utils/make_ass_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
PLAYERRESY = 288
MARGINL = 10
MARGINR = 10
MARGINV = 20


def seconds_to_ass_format(seconds_float):
Expand Down Expand Up @@ -112,7 +113,7 @@ def resegment_utt_obj(utt_obj, ass_file_config):
approx_chars_per_line = (PLAYERRESX - MARGINL - MARGINR) / (
ass_file_config.fontsize * 0.6
) # assume chars 0.6 as wide as they are tall
approx_lines_per_segment = (PLAYERRESY - ass_file_config.marginv) / (
approx_lines_per_segment = (PLAYERRESY - MARGINV) / (
ass_file_config.fontsize * 1.15
) # assume line spacing is 1.15
if approx_lines_per_segment > ass_file_config.max_lines_per_segment:
Expand Down Expand Up @@ -188,13 +189,22 @@ def make_word_level_ass_file(
"BorderStyle": "1",
"Outline": "1",
"Shadow": "0",
"Alignment": "2",
"Alignment": None, # will specify below
"MarginL": str(MARGINL),
"MarginR": str(MARGINR),
"MarginV": str(ass_file_config.marginv),
"MarginV": str(MARGINV),
"Encoding": "0",
}

if ass_file_config.vertical_alignment == "top":
default_style_dict["Alignment"] = "8" # text will be 'center-justified' and in the top of the screen
elif ass_file_config.vertical_alignment == "center":
default_style_dict["Alignment"] = "5" # text will be 'center-justified' and in the middle of the screen
elif ass_file_config.vertical_alignment == "bottom":
default_style_dict["Alignment"] = "2" # text will be 'center-justified' and in the bottom of the screen
else:
raise ValueError(f"got an unexpected value for ass_file_config.vertical_alignment")

output_dir = os.path.join(output_dir_root, "ass", "words")
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, f"{utt_obj.utt_id}.ass")
Expand Down Expand Up @@ -316,13 +326,22 @@ def make_token_level_ass_file(
"BorderStyle": "1",
"Outline": "1",
"Shadow": "0",
"Alignment": "2",
"Alignment": None, # will specify below
"MarginL": str(MARGINL),
"MarginR": str(MARGINR),
"MarginV": str(ass_file_config.marginv),
"MarginV": str(MARGINV),
"Encoding": "0",
}

if ass_file_config.vertical_alignment == "top":
default_style_dict["Alignment"] = "8" # text will be 'center-justified' and in the top of the screen
elif ass_file_config.vertical_alignment == "center":
default_style_dict["Alignment"] = "5" # text will be 'center-justified' and in the middle of the screen
elif ass_file_config.vertical_alignment == "bottom":
default_style_dict["Alignment"] = "2" # text will be 'center-justified' and in the bottom of the screen
else:
raise ValueError(f"got an unexpected value for ass_file_config.vertical_alignment")

output_dir = os.path.join(output_dir_root, "ass", "tokens")
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, f"{utt_obj.utt_id}.ass")
Expand Down

0 comments on commit 7c72a4b

Please sign in to comment.