From e42e761dca58557799084ceafdf2088d85fe40c5 Mon Sep 17 00:00:00 2001 From: Sacha Chua Date: Tue, 25 Oct 2022 11:13:23 -0400 Subject: Caption daemon --- roles/caption/templates/process-captions.py | 46 ++++++++++++++++------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'roles/caption/templates/process-captions.py') diff --git a/roles/caption/templates/process-captions.py b/roles/caption/templates/process-captions.py index 6ad890a..72e9ad2 100755 --- a/roles/caption/templates/process-captions.py +++ b/roles/caption/templates/process-captions.py @@ -46,7 +46,8 @@ AUDIO_REGEXP = '\.(ogg|opus)$' ALWAYS = False TRIM_AUDIO = False MODEL = os.environ.get('MODEL', 'large') # Set to tiny for testing -JSON_FILE = '/data/emacsconf/2022/talks.json' +WORK_DIR = "{{ emacsconf_caption_dir }}" +JSON_FILE = os.path.join(WORK_DIR, 'talks.json') def get_slug_from_filename(filename): m = re.search('emacsconf-[0-9]+-([a-z]+)--', filename) @@ -100,14 +101,14 @@ def get_files_to_work_on(directory): return needs_work def extract_audio(work): - output = subprocess.check_output(['ffprobe', video_file], stderr=subprocess.STDOUT) + output = subprocess.check_output(['ffprobe', work['video']], stderr=subprocess.STDOUT) extension = 'opus' if 'Audio: vorbis' in output.decode(): extension = 'ogg' - new_file = os.path.join(os.path.dirname(video_file), base_name(video_file) + '.' + extension) - acodec = 'copy' if re.search('webm$', video_file) else 'libopus' - log("Extracting audio from %s acodec %s" % (video_file, acodec)) - output = subprocess.check_output(['ffmpeg', '-y', '-i', video_file, '-acodec', acodec, '-vn', new_file], stderr=subprocess.STDOUT) + new_file = work['base'] + '.' + extension + acodec = 'copy' if re.search('webm$', work['video']) else 'libopus' + log("Extracting audio from %s acodec %s" % (work['video'], acodec)) + output = subprocess.check_output(['ffmpeg', '-y', '-i', work['video'], '-acodec', acodec, '-vn', new_file], stderr=subprocess.STDOUT) work['audio'] = new_file return work @@ -213,19 +214,22 @@ def base_name(s): # assert(base_name('/home/sachac/current/sqlite/emacsconf-2022-sqlite--using-sqlite-as-a-data-source-a-framework-and-an-example--andrew-hyatt--normalized.webm.vtt') == 'emacsconf-2022-sqlite--using-sqlite-as-a-data-source-a-framework-and-an-example--andrew-hyatt--main') log(f"MODEL {MODEL} ALWAYS {ALWAYS} TRIM_AUDIO {TRIM_AUDIO}") -directory = sys.argv[1] if len(sys.argv) > 1 else "~/current" +directory = sys.argv[1] if len(sys.argv) > 1 else WORK_DIR + needs_work = get_files_to_work_on(directory) -if THREADS > 0: - torch.set_num_threads(THREADS) -for work in needs_work: - log("Started processing %s" % work['base']) - if work['audio']: - if ALWAYS or not 'vtt' in work: - work = generate_captions(work) - if ALWAYS or not 'srv2' in work: - work = generate_srv2(work) -# print("Aligning words", audio_file, datetime.datetime.now()) -# word_cuts = align_words(cuts) -# convert_cuts_to_word_timing(audio_file, word_cuts) - log("Done %s" % str(work['base'])) - +if len(needs_work) > 0: + if THREADS > 0: + torch.set_num_threads(THREADS) + for work in needs_work: + log("Started processing %s" % work['base']) + if work['audio']: + if ALWAYS or not 'vtt' in work: + work = generate_captions(work) + if ALWAYS or not 'srv2' in work: + work = generate_srv2(work) + # print("Aligning words", audio_file, datetime.datetime.now()) + # word_cuts = align_words(cuts) + # convert_cuts_to_word_timing(audio_file, word_cuts) + log("Done %s" % str(work['base'])) +else: + log("No work needed.") -- cgit v1.2.3