124 lines
3.5 KiB
Python
124 lines
3.5 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Tests the audacity pipe.
|
|
|
|
Keep pipe_test.py short!!
|
|
You can make more complicated longer tests to test other functionality
|
|
or to generate screenshots etc in other scripts.
|
|
|
|
Make sure Audacity is running first and that mod-script-pipe is enabled
|
|
before running this script.
|
|
|
|
Requires Python 2.7 or later. Python 3 is strongly recommended.
|
|
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from subprocess import call
|
|
from pathlib import Path
|
|
|
|
# Platform specific file name and file path.
|
|
# PATH is the location of files to be imported / exported.
|
|
|
|
#PATH = './'
|
|
PATH = sys.argv[1]
|
|
|
|
|
|
# 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(f"Export2: Filename={os.path.join(PATH, filename)} NumChannels=1.0")
|
|
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 process():
|
|
for entry in os.scandir(sys.argv[1]):
|
|
if entry.path.endswith(".mkv"):
|
|
do_command('New:')
|
|
video_in = os.path.abspath(entry.path)
|
|
processed_audio = '{}-out.wav'.format(entry.path.split('.')[0])
|
|
final_output_name = "out/{}".format(entry.name)
|
|
do_command("Import2: Filename={}".format(video_in))
|
|
do_command('Popmute:')
|
|
do_command('Amplify:')
|
|
export(processed_audio)
|
|
do_command('Close:')
|
|
# TODO Move to different script?
|
|
final_output_name = "output-" + video
|
|
rc = call('ffmpeg -i {} -i {} -acodec copy -vcodec copy -f mkv {}'.format(video, processed_audio, final_output_name))
|
|
print(rc)
|
|
|
|
process()
|