From 5b261e82c209122c8073dd276fd394c14ed15692 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 17 Oct 2023 18:26:18 +0300 Subject: [PATCH] Try to get title from file metadata --- tape-playlist.lua | 2 ++ tape-playlist.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tape-playlist.lua b/tape-playlist.lua index 1307111..f0cfd13 100644 --- a/tape-playlist.lua +++ b/tape-playlist.lua @@ -24,6 +24,7 @@ local function bytes2time(b) return string.format("%dm, %ds", math.floor(s / 60), s % 60) end +term.clear() local screen_w, screen_h = term.getSize() parallel.waitForAll( @@ -56,6 +57,7 @@ function() drive.play() end elseif ev == "term_resize" then + term.clear() screen_w, screen_h = term.getSize() elseif ev == "tape_present" then table_of_contents = {} diff --git a/tape-playlist.py b/tape-playlist.py index 89ec17b..c9e9ba5 100644 --- a/tape-playlist.py +++ b/tape-playlist.py @@ -7,9 +7,11 @@ from sys import argv from pathlib import Path from os.path import splitext from math import ceil +from re import search DFPWM_ENCODER_EXECUTABLE = "aucmp" FFMPEG_EXECUTABLE = "ffmpeg" +FFPROBE_EXECUTABLE = "ffprobe" DFPWM_SAMPLE_RATE = 6000 @@ -22,6 +24,7 @@ with TemporaryDirectory(prefix="tapedrive") as tmpdir_str: tmpdir = Path(tmpdir_str) filelist: list[Path] = [] + titles: list[bytes] = [] for i, name in enumerate(input_folder.rglob("*.mp3")): if i >= 48: print(f"more than 48 tracks, skipping {name}") @@ -29,16 +32,20 @@ with TemporaryDirectory(prefix="tapedrive") as tmpdir_str: encoded_file = tmpdir / (splitext(name.name)[0] + ".dfpwm") filelist.append(encoded_file) with encoded_file.open("wb") as fout: + with Popen([ FFPROBE_EXECUTABLE, name ], stderr=PIPE) as ffprobe: + metadata: str = ffprobe.stderr.read().decode() # type: ignore + if (match := search(r"title\s+: (.*)", metadata)): + titles.append(match.groups()[0].encode("ascii", errors="ignore")) + else: + titles.append(splitext(name.name)[0].encode("ascii", errors="ignore")) with Popen([ DFPWM_ENCODER_EXECUTABLE ], stdout=fout, stdin=PIPE) as dfpwm: with Popen([ FFMPEG_EXECUTABLE, "-i", name, "-f", "s8", "-ac", "1", "-ar", "48k", "-" ], stdout=dfpwm.stdin) as ffmpeg: ffmpeg.wait() offset: int = 6000 - titles: list[bytes] = [] positions: list[tuple[int, int]] = [] for file in filelist: - titles.append(file.name.removesuffix(".dfpwm").encode("ascii", errors="replace")) size = ceil(file.stat().st_size / DFPWM_SAMPLE_RATE) * DFPWM_SAMPLE_RATE positions.append((offset, size)) offset += size @@ -50,7 +57,7 @@ with TemporaryDirectory(prefix="tapedrive") as tmpdir_str: name = (titles[i] if i < len(titles) else b"")[:117] pos = positions[i] if i < len(titles) else (0, 0) if i < len(titles): - print(f"{i:2d} {pos[0]} + {pos[1]}") + print(f"{i:2d} {pos[0]} + {pos[1]} {name}") written_bytes += fout.write(pos[0].to_bytes(4, "big")) written_bytes += fout.write(pos[1].to_bytes(4, "big")) written_bytes += fout.write(name)