Encode multiple tracks concurrently

This commit is contained in:
George Lacey 2024-05-10 16:47:05 +01:00
parent 4633505b98
commit a65b6dd6e3

View File

@ -3,6 +3,7 @@ import subprocess
import shutil
from subprocess import CalledProcessError
from pathlib import Path
from multiprocessing import Pool
def get_args():
@ -30,15 +31,22 @@ def skip_album(path: Path):
return False
def transcode(transcode_list: list, encoder: Path):
def transcode(transcode_list: list, encoder: Path, workers=8):
worker_args = [(row[0], row[1], encoder) for row in transcode_list]
with Pool(workers) as p:
print(p.starmap_async(transcode_worker, worker_args))
p.close()
p.join()
def transcode_worker(in_track, out_track, encoder):
additional_args = ('--bitrate', '128', '--music')
for in_track, out_track in transcode_list:
subprocess_args = (str(encoder),) + additional_args + (in_track, out_track)
try:
result = subprocess.run(subprocess_args, capture_output=True, text=True, check=True)
except CalledProcessError:
print(f"ERROR: Transcoding of '{in_track}' failed with errors:")
print(result.stderr)
subprocess_args = (str(encoder),) + additional_args + (in_track, out_track)
try:
result = subprocess.run(subprocess_args, capture_output=True, text=True, check=True)
return f"Transcoded '{in_track}' successfully"
except CalledProcessError:
return f"ERROR: Transcoding of '{in_track}' failed with errors:\n{result.stderr}"
def main(input_dir: Path, output_dir: Path, encoder: Path):