@@ -473,14 +473,11 @@ def save_readalong( # noqa C901
473473 output_dir : str ,
474474 output_basename : str ,
475475 config = None ,
476- text_grid : bool = False ,
477- closed_captioning : bool = False ,
478- output_xhtml : bool = False ,
479476 audiofile : str ,
480477 audiosegment : AudioSegment = None ,
481- html : bool = False ,
478+ output_formats = [] ,
482479):
483- """Save the results from align_audio() into the otuput files required for a
480+ """Save the results from align_audio() into the output files required for a
484481 readalong
485482
486483 Args:
@@ -489,10 +486,8 @@ def save_readalong( # noqa C901
489486 output_dir should already exist, files it contains may be overwritten
490487 output_basename (str): basename of the files to save in output_dir
491488 config ([type TODO], optional): alignment configuration loaded from the json
492- text_grid (bool, optional): if True, also save in Praat TextGrid and ELAN EAF formats
493- closed_captioning (bool, optional): if True, also save in .vtt and .srt subtitle formats
494- output_xhtml (bool, optional): if True, convert XML into XHTML format before writing
495489 audiofile (str): path to the audio file passed to align_audio()
490+ output_formats (List[str], optional): list of desired output formats
496491 audiosegment (AudioSegment): a pydub.AudioSegment object of processed audio.
497492 if None, then original audio will be saved at `audiofile`
498493
@@ -510,30 +505,41 @@ def save_readalong( # noqa C901
510505
511506 output_base = os .path .join (output_dir , output_basename )
512507
513- if text_grid :
508+ # Create textgrid object if outputting to TextGrid or eaf
509+ if "TextGrid" in output_formats or "eaf" in output_formats :
514510 audio = read_audio_from_file (audiofile )
515511 duration = audio .frame_count () / audio .frame_rate
516512 words , sentences = return_words_and_sentences (align_results )
517513 textgrid = write_to_text_grid (words , sentences , duration )
514+
515+ if "TextGrid" in output_formats :
518516 textgrid .to_file (output_base + ".TextGrid" )
517+
518+ if "eaf" in output_formats :
519519 textgrid .to_eaf ().to_file (output_base + ".eaf" )
520520
521- if closed_captioning :
521+ # Create webvtt object if outputting to vtt or srt
522+ if "srt" in output_formats or "vtt" in output_formats :
522523 words , sentences = return_words_and_sentences (align_results )
523- webvtt_sentences = write_to_subtitles (sentences )
524- webvtt_sentences . save ( output_base + "_sentences.vtt" )
525- webvtt_sentences . save_as_srt ( output_base + "_sentences.srt" )
526- webvtt_words = write_to_subtitles ( words )
527- webvtt_words . save (output_base + "_words.vtt " )
528- webvtt_words .save_as_srt (output_base + "_words.srt" )
529-
530- if output_xhtml :
531- convert_to_xhtml ( align_results [ "tokenized" ] )
532- tokenized_xml_path = output_base + ".xhtml"
533- else :
534- tokenized_xml_path = output_base + ".xml"
524+ cc_sentences = write_to_subtitles (sentences )
525+ cc_words = write_to_subtitles ( words )
526+
527+ if "srt" in output_formats :
528+ cc_sentences . save_as_srt (output_base + "_sentences.srt " )
529+ cc_words .save_as_srt (output_base + "_words.srt" )
530+
531+ if "vtt" in output_formats :
532+ cc_words . save ( output_base + "_words.vtt" )
533+ cc_sentences . save ( output_base + "_sentences.vtt" )
534+
535+ tokenized_xml_path = output_base + ".xml"
535536 save_xml (tokenized_xml_path , align_results ["tokenized" ])
536537
538+ if "xhtml" in output_formats :
539+ convert_to_xhtml (align_results ["tokenized" ])
540+ tokenized_xhtml_path = output_base + ".xhtml"
541+ save_xml (tokenized_xhtml_path , align_results ["tokenized" ])
542+
537543 _ , audio_ext = os .path .splitext (audiofile )
538544 audio_path = output_base + audio_ext
539545 audio_format = audio_ext [1 :]
@@ -562,7 +568,7 @@ def save_readalong( # noqa C901
562568 )
563569 save_txt (smil_path , smil )
564570
565- if html :
571+ if " html" in output_formats :
566572 html_out_path = output_base + ".html"
567573 html_out = create_web_component_html (tokenized_xml_path , smil_path , audio_path )
568574 with open (html_out_path , "w" ) as f :
0 commit comments