Skip to content

Instantly share code, notes, and snippets.

@netalkGB
Last active February 26, 2023 11:11
Show Gist options
  • Select an option

  • Save netalkGB/33aa115443891d79f5daec6541b32fb9 to your computer and use it in GitHub Desktop.

Select an option

Save netalkGB/33aa115443891d79f5daec6541b32fb9 to your computer and use it in GitHub Desktop.
先頭末尾無音除去
import sys
import os
class Audacity:
def __init__(self):
if sys.platform == 'win32':
TONAME = '\\\\.\\pipe\\ToSrvPipe'
FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
self._EOL = '\r\n\0'
else:
TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
self._EOL = '\n'
self._TOFILE = open(TONAME, 'w')
self._FROMFILE = open(FROMNAME, 'rt')
def send_command(self, command):
self._TOFILE.write(command + self._EOL)
self._TOFILE.flush()
def get_response(self):
result = ''
line = ''
while True:
result += line
line = self._FROMFILE.readline()
if line == '\n' and len(result) > 0:
break
return result
def do_command(self, command):
self.send_command(command)
response = self.get_response()
return response
class AudacityControl:
def __init__(self, audacity, verbose=False):
self._audacity = audacity
self._verbose = verbose
def _do_command(self, command):
if self._verbose:
print(command)
result = self._audacity.do_command(command)
print(result)
else:
self._audacity.do_command(command)
def export_as_wave(self, file_name_str, num_channels):
self._do_command('Export2: Filename="' + file_name_str + '" ' + 'NumChannels="' + str(num_channels) + '"')
def select_all(self):
self._do_command('SelectAll:')
def select_from_top(self, start_time, end_time):
self._do_command('Select: End="' + str(end_time) +'" ' + 'Mode="Set" ' + 'Start="' + str(start_time) + '"')
def select_from_bottom(self, start_time):
self._do_command('Select: ' + 'Mode="Set" ' + 'Start="' + str(-1 * start_time) + '"' + ' RelativeTo=ProjectEnd')
def truncate_silence(self):
self._do_command('TruncateSilence:Action="Truncate Detected Silence" Compress="50" Independent="0" Minimum="2" Threshold="-75" Truncate="0.5"')
def normalize(self):
self._do_command('Normalize: ApplyGain="1" PeakLevel="0" RemoveDcOffset="0" StereoIndependent="0"')
def save_as_project(self, file_name_str):
self._do_command('SaveProject2:AddToHistory="0" Filename="' + file_name_str + '"')
def close(self):
self._do_command('Close:')
if __name__ == "__main__":
audacity_control = AudacityControl(Audacity())
audacity_control.select_from_top(0,10)
audacity_control.truncate_silence()
audacity_control.select_from_bottom(-10)
audacity_control.truncate_silence()
import sys
import subprocess
from audacity import Audacity, AudacityControl
import time
if len(sys.argv) < 2:
print('Error', file=sys.stderr)
sys.exit(1)
task_file_path = sys.argv[1]
with open(task_file_path) as f:
project_file_list = f.read().splitlines()
file_count = len(project_file_list)
audacity = AudacityControl(Audacity())
for idx, project_path in enumerate(project_file_list):
print('[' + str(idx + 1) + '/' + str(file_count) + ']' + ' ' + project_path)
subprocess.Popen('C:\\Users\\gb\\Desktop\\Audacity\\Audacity.exe' + ' ' + project_path)
time.sleep(3) # 3秒でAudacityが起動してプロジェクトファイル(トラックは1つ)が読み込み終わる想定
audacity.select_from_top(0,10)
audacity.truncate_silence()
audacity.select_from_bottom(-10)
audacity.truncate_silence()
audacity.select_all()
# audacity.normalize()
audacity.export_as_wave(project_path.replace('.aup3', '.wav'), 2)
audacity.save_as_project(project_path + '.trimmed.aup3')
audacity.close()
print('done')
@netalkGB
Copy link
Author

Windowsでしか試していない
macで動くかはわからない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment