From c04d7b88560cdc6f44f20514719bd4013e38f2a6 Mon Sep 17 00:00:00 2001 From: Arona Jones Date: Sun, 7 Feb 2021 21:37:56 +0000 Subject: [PATCH] New version of lecturemunger --- lecturemunger.py | 158 ++++++++++++++++++ .../all_together_now.py | 0 process_audio.py => old/process_audio.py | 0 rename.py => old/rename.py | 0 4 files changed, 158 insertions(+) create mode 100644 lecturemunger.py rename all_together_now.py => old/all_together_now.py (100%) rename process_audio.py => old/process_audio.py (100%) rename rename.py => old/rename.py (100%) diff --git a/lecturemunger.py b/lecturemunger.py new file mode 100644 index 0000000..aca7417 --- /dev/null +++ b/lecturemunger.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Tests the audacity pipe. + +Depends on Destreamer and python-dateutil + +""" + + +from subprocess import call +import os +import sys +from pathlib import Path +import dateutil.parser as dparser +import re + +# TODO +path_to_destreamer = os.path.abspath(sys.argv[1]) +working_dir = os.path.abspath(sys.argv[2]) +urls = sys.argv[3:] + +# Platform specific constants +if sys.platform == 'win32': + print("recording-test.py, running on windows") + PIPE_TO_AUDACITY = '\\\\.\\pipe\\ToSrvPipe' + PIPE_FROM_AUDACITY = '\\\\.\\pipe\\FromSrvPipe' + EOL = '\r\n\0' +else: + print("recording-test.py, running on linux or mac") + PIPE_TO_AUDACITY = '/tmp/audacity_script_pipe.to.' + str(os.getuid()) + PIPE_FROM_AUDACITY = '/tmp/audacity_script_pipe.from.' + str(os.getuid()) + EOL = '\n' + + +print("Write to \"" + PIPE_TO_AUDACITY +"\"") +if not os.path.exists(PIPE_TO_AUDACITY): + print(""" ..does not exist. + Ensure Audacity is running with mod-script-pipe.""") + sys.exit() + +print("Read from \"" + PIPE_FROM_AUDACITY +"\"") +if not os.path.exists(PIPE_FROM_AUDACITY): + print(""" ..does not exist. + Ensure Audacity is running with mod-script-pipe.""") + sys.exit() + +print("-- Both pipes exist. Good.") + +TOPIPE = open(PIPE_TO_AUDACITY, 'w') +print("-- File to write to has been opened") +FROMPIPE = open(PIPE_FROM_AUDACITY, 'r') +print("-- File to read from has now been opened too\r\n") + + +def send_command(command): + """Send a command to Audacity.""" + print("Send: >>> "+command) + TOPIPE.write(command + EOL) + TOPIPE.flush() + + +def get_response(): + """Get response from Audacity.""" + line = FROMPIPE.readline() + result = "" + while True: + result += line + line = FROMPIPE.readline() + # print(f"Line read: [{line}]") + if line == '\n': + return result + + +def do_command(command): + """Do the command. Return the response.""" + send_command(command) + # time.sleep(0.1) # may be required on slow machines + response = get_response() + print("Rcvd: <<< " + response) + return response + + +def export(filename): + """Export the new track, and deleted both tracks.""" + do_command("Select: Track=1 mode=Set") + do_command("SelTrackStartToEnd") + do_command("Export2: Filename={} NumChannels=1.0".format(filename)) + do_command("SelectAll") + do_command("RemoveTracks") + + +def do_command(command): + """Send one command, and return the response.""" + send_command(command) + response = get_response() + print("Rcvd: <<< \n" + response) + return response + +def mkdir_if_not_exists(directory): + if not os.path.exists(directory): + os.makedirs(directory) + return os.path.abspath(directory) + +def do(): + mkdir_if_not_exists(working_dir) + if working_dir: + # Setup + #os.chdir(working_dir) + downloaded = mkdir_if_not_exists(working_dir + "/downloaded") + processed_audio_dir = mkdir_if_not_exists(working_dir + "/processed_audio") + final = mkdir_if_not_exists(working_dir + "/final") + # Download videos + print("Using destreamer.js at {}".format(path_to_destreamer)) + call("node --max-http-header-size 32768 {} --skip -k -o {} -i {}".format(path_to_destreamer, downloaded, " ".join(urls)), shell=True) + for entry in os.scandir(downloaded): + # Normalise names + # Put file extension to one side for safekeeping + split = entry.name.split('.') + extension = split[1] + name = split[0].replace(',', '').split('#')[0] # Discard all commas, and unique id doodad + head, sep, tail = name.partition(' ') # Discard leading date + split2 = tail.split() + date = dparser.parse(split2[-1], fuzzy=True) # Parse the date + folder_path = split2[0] + split2[1] # TODO Fragile + title = re.search(r'\d+-\d+\s[\d\w\s]*', tail) + if re.match: + title = title.group(0).strip().replace(' ', '_') + # Add date back + if date is not None: + title += "_" + date.strftime('%Y-%m-%d') + # Rename file + source = entry.path + actual_folder_path = '{}/{}'.format(target, folder_path) + filename = "{}.{}".format(title, extension) + dest = "{}/{}".format(actual_folder_path, filename) + print ("Source: " + source) + print ("Dest: " + dest) + mkdir_if_not_exists(actual_folder_path) + os.rename(source, dest) + + # Extract audio, process it, add it back to the video + video_in = os.path.abspath(filename) + processed_audio = processed_audio_dir.joinpath(filename + ".wav") + final_output_name = final.joinpath(filename) + do_command("Import2: Filename={}".format(video_in)) + do_command("SelectAll:") + do_command('Popmute:') + do_command("SelectAll:") + do_command('Amplify:') + export(processed_audio) + do_command('Close:') + # TODO Move to different script? + final_output_name = "output-" + video_in + print("Combining video with audio at {}", processed_audio) + call('ffmpeg -i {} -i {} -acodec copy -vcodec copy -f mkv {}'.format(video_in, processed_audio, final_output_name), shell=True) + +do() diff --git a/all_together_now.py b/old/all_together_now.py similarity index 100% rename from all_together_now.py rename to old/all_together_now.py diff --git a/process_audio.py b/old/process_audio.py similarity index 100% rename from process_audio.py rename to old/process_audio.py diff --git a/rename.py b/old/rename.py similarity index 100% rename from rename.py rename to old/rename.py