Flesh out audio conversion script and add initial network sync

This commit is contained in:
James Grogan 2024-09-03 09:33:09 +01:00
parent f0430f8713
commit a6382a9b96
2 changed files with 116 additions and 26 deletions

View file

@ -4,38 +4,58 @@ import logging
import shutil import shutil
from pathlib import Path from pathlib import Path
import subprocess import subprocess
import uuid
from multiprocessing import Pool from multiprocessing import Pool
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def launch_task(cmd): def convert_audio_file(args):
subprocess.run(cmd, shell=True, check=True) cmd, output_tmp, output_path = args
def convert(input_dir: Path, output_dir: Path): subprocess.run(cmd, shell=True)
os.makedirs(output_path.parent, exist_ok=True)
shutil.move(output_tmp, output_path)
logger.info("Converting files in %s", input_dir) class AudioFileConverter():
logger.info("Writing output to: %s", output_dir)
os.makedirs(output_dir, exist_ok=True) def __init__(self, input_dir: Path, output_dir: Path):
self.input_dir = input_dir
self.output_dir = output_dir
self.output_fmt = "mp3"
self.input_fmt = "flac"
flac_files = list(input_dir.resolve().rglob("*.flac")) def get_converted_path(self, input_path: Path):
tasks = [] relative_path = input_path.relative_to(self.input_dir)
output_filename = str(input_path.stem) + f".{self.output_fmt}"
for idx, path in enumerate(flac_files):
logger.info("Converting file %d of %d", idx, len(flac_files))
relative_path = path.relative_to(input_dir)
output_filename = str(path.stem) + ".mp3"
output_filename.replace("'", "") output_filename.replace("'", "")
output_path = self.output_dir / relative_path.parent / output_filename
return output_path
output_path = output_dir / relative_path.parent / output_filename def convert(self):
cmd = f"ffmpeg -i '{path}' -ab 320k -map_metadata 0 -id3v2_version 3 '{output_path}'" logger.info("Converting files in %s", self.input_dir)
tasks.append(cmd) logger.info("Writing output to: %s", self.output_dir)
os.makedirs(self.output_dir, exist_ok=True)
input_files = list(self.input_dir.resolve().rglob(f"*.{self.input_fmt}"))
output_files = []
for input_file in input_files:
candidate_output = self.get_converted_path(input_file)
if not candidate_output.exists():
output_files.append(candidate_output)
tasks = []
for idx, (input_path, output_path) in enumerate(zip(input_files, output_files)):
logger.info("Converting file %d of %d", idx, len(input_files))
output_tmp = self.output_dir / (str(uuid.uuid4()) + f".{self.output_fmt}")
cmd = f"ffmpeg -i '{input_path}' -ab 320k -map_metadata 0 -id3v2_version 3 '{output_tmp}'"
tasks.append((cmd, output_tmp, output_path))
with Pool(10) as p: with Pool(10) as p:
p.map(launch_task, tasks) p.map(convert_audio_file, tasks)
def move(input_dir: Path, output_dir: Path): def move(input_dir: Path, output_dir: Path):
@ -51,7 +71,6 @@ def move(input_dir: Path, output_dir: Path):
os.makedirs(output_path.parent, exist_ok=True) os.makedirs(output_path.parent, exist_ok=True)
shutil.move(path, output_path) shutil.move(path, output_path)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -71,7 +90,8 @@ if __name__ == "__main__":
logging.basicConfig() logging.basicConfig()
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
# convert(args.input_dir.resolve(), args.output_dir.resolve()) converter = AudioFileConverter(args.input_dir.resolve(), args.output_dir.resolve())
move(args.input_dir.resolve(), args.output_dir.resolve()) converter.convert()
#move(args.input_dir.resolve(), args.output_dir.resolve())

70
src/network/sync.py Normal file
View file

@ -0,0 +1,70 @@
import argparse
import os
import logging
import shutil
from pathlib import Path
import subprocess
import uuid
from fabric import Connection
logger = logging.getLogger(__name__)
def sync(source_dir: Path, target_dir: Path, host: str):
logger.info("Syncing files in %s to %s on % s", source_dir, target_dir, host)
cmd = f"find {target_dir} -type f"
if host:
result = Connection(host).run(cmd, hide=True)
target_files = result.stdout.splitlines()
else:
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
target_files = result.stdout.splitlines()
target_files = [Path(f).relative_to(target_dir) for f in target_files]
source_files = []
for dir_path, _, files in os.walk(source_dir):
for each_file in files:
source_files.append(Path(dir_path + "/" + each_file).relative_to(source_dir))
sync_files = []
for source_file in source_files:
if source_file not in target_files:
sync_files.append(source_file)
sync_dir = Path(os.getcwd())/"sync"
os.makedirs(sync_dir, exist_ok=True)
for sync_file in sync_files:
os.makedirs((sync_dir/sync_file).parent, exist_ok=True)
shutil.copy(source_dir / sync_file, sync_dir / sync_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--source_dir',
type=Path,
default=Path(),
help="Directory with source files to sync from.")
parser.add_argument('--host',
type=str,
default = "",
help="Name of host system to sync with.")
parser.add_argument('--target_dir',
type=Path,
default=Path(),
help="Directory with source files to sync to.")
args = parser.parse_args()
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
sync(args.source_dir.resolve(), args.target_dir.resolve(), args.host)