Skip to content

Commit ae05e4d

Browse files
14 April 2025: transcode_audio.py is now somewhat more robust
1 parent dcfb67b commit ae05e4d

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ Accumulating several small changes to multiple scripts, mostly making default ex
445445
------------
446446
* `git_committer.py` no longer uses `--aggressive` or `--prune=now` on any of its `git gc` commands, because that can make memory usage spike on repos with many many objects.
447447

448+
14 Apr 2025
449+
-----------
450+
* `transcode_audio.py` now allows file processing to continue even when tag copying fails.
451+
* `transcode_audio.py` now has a `--quiet` command-line options to suppress some verbiage.
452+
448453
KNOWN BUGS
449454
==========
450455
* No known bugs listed at the moment. However, check individual module files to be sure that I haven't just forgotten to list something here.

music_file_handling.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import shutil
1616
import subprocess
1717
import sys
18+
import warnings
1819

1920
from pathlib import Path
2021
from typing import Any, Generator, Iterable, List, TextIO, Union
@@ -232,7 +233,8 @@ def value_of(what: Any) -> str:
232233
assert from_f.exists()
233234
assert to_f.exists()
234235

235-
from_data, to_data = mutagen.File(from_f, easy=True), mutagen.File(to_f, easy=True)
236+
from_data = mutagen.File(from_f, easy=True)
237+
to_data = mutagen.File(to_f, easy=True)
236238

237239
for data, which_f in ((from_data, from_f), (to_data, to_f)):
238240
if not data:
@@ -432,6 +434,7 @@ def run_conversion(infile: Path,
432434
new_suffix: str = '.mp3',
433435
quiet: bool = False,
434436
vbrfix: bool = None,
437+
allow_tag_copy_to_fail: bool = False,
435438
) -> Path:
436439
"""Takes INFILE, a file to be processed, and processes it by starting two processes
437440
modeled by two Popen instances. The first is started using DEC_ARGS as the
@@ -449,6 +452,9 @@ def run_conversion(infile: Path,
449452
single pass it makes.) If VBRFIX is None (the default), this VBR-fixing pass is
450453
performed iff the extension of the new file is (case-insensitive) ".mp3".
451454
455+
If ALLOW_TAG_COPY_TO_FAIL is True, issues a warning instead of failing when tags
456+
can't be copied from the old to the new file.
457+
452458
ENC_ARGS is modified before the process is started by appending the name of the
453459
desired output file after that output filename has been generated. This requires
454460
that the command be constructed in such a way that it ends with the output
@@ -468,7 +474,7 @@ def run_conversion(infile: Path,
468474
vbrfix = (new_suffix.strip().casefold().endswith('.mp3'))
469475

470476
outfile = clean_name(infile.with_suffix(new_suffix))
471-
enc_args.append(outfile)
477+
enc_args.append(str(outfile))
472478

473479
old_stdout, old_stderr = sys.stdout, sys.stderr
474480
try:
@@ -485,13 +491,20 @@ def run_conversion(infile: Path,
485491

486492
if vbrfix:
487493
do_vbrfix(outfile, quiet)
488-
do_copy_tags(infile, outfile, quiet)
494+
495+
try:
496+
do_copy_tags(infile, outfile, quiet)
497+
except Exception as errrr:
498+
if allow_tag_copy_to_fail:
499+
warnings.warn(f"Could not copy tags from {infile} to {outfile}! The system said: {errrr}")
500+
else:
501+
raise errrr
502+
489503
if not quiet:
490504
print('\n\n'.join([th.unicode_of(i) for i in out if i]).strip(), end="\n\n")
491505
finally:
492506
sys.stdout, sys.stderr = old_stdout, old_stderr
493507

494-
495508
return outfile
496509

497510

transcode_audio.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
def convert_files(filename: List[Type[Path]],
2222
delete: bool,
2323
mp4: bool,
24-
no_vbrfix: bool) -> None:
24+
no_vbrfix: bool,
25+
quiet: bool) -> None:
2526
for f in filename:
2627
assert isinstance(f, Path)
2728
assert f.is_file()
2829

30+
if not quiet:
31+
print(f"Converting {f.name} ...")
32+
2933
if (ext := f.suffix.strip().casefold()) == '.flac':
3034
dec_args = [mfh.executable_locations['flac']] + mfh.config['flac options'] + [str(f.resolve())]
3135
elif ext in ['.wav', '.wave']:
@@ -40,15 +44,16 @@ def convert_files(filename: List[Type[Path]],
4044

4145
# FIXME! mp4 -> mp3 tag conversion gives the literal Python string representation of 1-item lists of strings!
4246
new_f = mfh.run_conversion(f, dec_args=dec_args, enc_args=enc_args, new_suffix=('.m4a' if mp4 else '.mp3'),
43-
quiet=False, vbrfix=(not no_vbrfix))
47+
quiet=False, vbrfix=(not no_vbrfix), allow_tag_copy_to_fail=True)
4448

4549
if new_f.exists() and delete:
4650
f.unlink()
4751

4852

4953
def process_command_line(args: List[str]) -> None:
50-
parser = argparse.ArgumentParser(prog='transcode_audio', description=__doc__.strip().split('\n')[0],
51-
epilog=__doc__.strip().split('\n')[-1])
54+
parser = argparse.ArgumentParser(prog='transcode_audio',
55+
description=__doc__.strip().split('\n\n')[0].replace('\n', ' '),
56+
epilog=__doc__.strip().split('\n\n')[-1].replace('\n', ' '))
5257

5358
parser.add_argument('filename', type=Path, nargs='+') # positional argument
5459
parser.add_argument('-4', '--mp4', '--m4a', '--to-mp4', '--to-m4a', action='store_true',
@@ -57,6 +62,7 @@ def process_command_line(args: List[str]) -> None:
5762
action='store_true', help="Delete originals after conversion.")
5863
parser.add_argument('-n', '--no-vbrfix', action='store_true',
5964
help="Don't run VBRfix after conversion, even on .mp3 files.")
65+
parser.add_argument('-q', '--quiet', action='store_true', help="Suppress unneeded jibber-jabber.")
6066

6167
convert_files(**vars(parser.parse_args(args)))
6268

@@ -68,7 +74,7 @@ def process_command_line(args: List[str]) -> None:
6874
if __name__ == "__main__":
6975
force_debugging = False
7076
if force_debugging:
71-
process_command_line(['/home/patrick/Music/by Artist/Daniel Munkus/[2024] Oft Come Devil/Polka Dot Deli - Oft Come Devil - 01 Kings No More.flac'])
77+
process_command_line(["/home/patrick/Music/by Artist/The The/[2000] NakedSelf/01 - The The - Boiling Point.mp4"])
7278
sys.exit()
7379

7480
process_command_line(sys.argv[1:])

0 commit comments

Comments
 (0)