2019-07-16 11:41:24 +00:00
|
|
|
#!/usr/bin/env python
|
2019-05-14 22:10:29 +00:00
|
|
|
"""
|
2020-04-27 18:16:10 +00:00
|
|
|
A script to automate my audio-encoding setup.
|
2019-09-11 12:44:40 +00:00
|
|
|
|
2020-04-27 18:16:10 +00:00
|
|
|
Dependencies:
|
|
|
|
- eac3to (https://www.videohelp.com/software/eac3to)
|
|
|
|
- flac (https://xiph.org/flac/index.html)
|
|
|
|
- qaac (https://github.com/nu774/qaac)
|
2020-04-27 23:56:28 +00:00
|
|
|
- ffmpeg (https://www.ffmpeg.org/download.html)
|
2019-05-14 22:10:29 +00:00
|
|
|
"""
|
2020-06-17 01:23:01 +00:00
|
|
|
|
2019-05-14 22:10:29 +00:00
|
|
|
import argparse
|
|
|
|
import glob
|
2020-04-27 18:16:10 +00:00
|
|
|
import mimetypes
|
2019-05-14 22:10:29 +00:00
|
|
|
import os
|
2020-04-27 18:16:10 +00:00
|
|
|
import subprocess
|
2020-04-27 23:56:28 +00:00
|
|
|
import tempfile
|
2019-05-14 22:10:29 +00:00
|
|
|
|
2020-04-27 18:16:10 +00:00
|
|
|
__author__ = "LightArrowsEXE"
|
|
|
|
__license__ = 'MIT'
|
2020-07-04 22:27:02 +00:00
|
|
|
__version__ = '1.0.8'
|
2019-11-08 20:58:16 +00:00
|
|
|
|
|
|
|
|
2020-04-27 18:16:10 +00:00
|
|
|
ignored_formats = ["audio/opus", "audio/aac"]
|
2019-11-08 20:58:16 +00:00
|
|
|
|
|
|
|
|
2019-09-11 12:44:40 +00:00
|
|
|
def main():
|
2020-06-19 00:55:05 +00:00
|
|
|
filelist = (glob.glob('**/*', recursive=True)
|
|
|
|
if args.recursive else glob.glob('*'))
|
2019-09-11 12:44:40 +00:00
|
|
|
|
|
|
|
for f in filelist:
|
2020-06-20 20:17:03 +00:00
|
|
|
mime = mimetypes.types_map.get(os.path.splitext(f)[-1], "")
|
2020-06-17 01:23:01 +00:00
|
|
|
if f.endswith('.m2ts'):
|
2020-07-04 22:27:02 +00:00
|
|
|
encode_main(f, wav_only=True)
|
2020-06-17 01:23:01 +00:00
|
|
|
elif mime.startswith("audio/") or mime.startswith("video/"):
|
2020-04-27 18:16:10 +00:00
|
|
|
if mime not in ignored_formats:
|
2020-07-04 22:27:02 +00:00
|
|
|
encode_main(f)
|
2020-04-27 18:16:10 +00:00
|
|
|
|
|
|
|
|
2020-05-05 03:35:16 +00:00
|
|
|
def encode_flac(f):
|
2020-06-20 20:17:03 +00:00
|
|
|
subprocess.run(["eac3to", f, "-log=NUL", f"{os.path.splitext(f)[0]}.flac"])
|
2020-05-05 03:35:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def encode_aac(f):
|
2020-06-20 20:17:03 +00:00
|
|
|
temp = tempfile.mkstemp(prefix=f"{os.path.splitext(f)[0]}_")
|
2020-07-04 22:27:02 +00:00
|
|
|
subprocess.run(["ffmpeg", "-i", f, "-loglevel", "panic", "-stats",
|
|
|
|
f"{temp[1]}.wav"])
|
|
|
|
subprocess.run(["qaac", f"{temp[1]}.wav", "-V 127", "--no-delay",
|
|
|
|
"-o", f"{os.path.splitext(f)[0]}.m4a"])
|
2020-05-05 03:35:16 +00:00
|
|
|
|
|
|
|
|
2020-07-04 22:27:02 +00:00
|
|
|
def encode_opus(f):
|
|
|
|
subprocess.run(["ffmpeg", "-i", f, "-stats",
|
|
|
|
"-c:a", "libopus", "-b:a", f"{args.bitrate}",
|
|
|
|
f"{os.path.splitext(f)[0]}.opus"])
|
|
|
|
|
|
|
|
|
|
|
|
def encode_main(f, wav_only: bool = False):
|
2020-04-27 18:16:10 +00:00
|
|
|
print(f"\n{f}\n")
|
2020-06-17 01:23:01 +00:00
|
|
|
if wav_only:
|
2020-04-27 21:20:52 +00:00
|
|
|
if args.track:
|
2020-06-19 00:55:05 +00:00
|
|
|
subprocess.run(["eac3to", f, "-log=NUL", f"{args.track}:",
|
2020-06-20 20:17:03 +00:00
|
|
|
f"{os.path.splitext(f)[0]}_Track0{args.track}.wav"])
|
2020-04-27 18:16:10 +00:00
|
|
|
else:
|
2020-06-19 00:55:05 +00:00
|
|
|
subprocess.run(["eac3to", f, "-log=NUL",
|
2020-06-20 20:17:03 +00:00
|
|
|
f"{os.path.splitext(f)[0]}.wav"])
|
2020-04-27 18:16:10 +00:00
|
|
|
else:
|
2020-04-27 23:56:28 +00:00
|
|
|
if not args.noflac:
|
2020-05-05 03:35:16 +00:00
|
|
|
encode_flac(f)
|
2020-07-04 22:27:02 +00:00
|
|
|
if not args.nolossy:
|
2020-07-04 23:01:05 +00:00
|
|
|
if args.codec == 'opus':
|
2020-07-04 22:27:02 +00:00
|
|
|
encode_opus(f)
|
2020-07-04 23:01:05 +00:00
|
|
|
elif args.codec == 'aac':
|
|
|
|
encode_aac(f)
|
2020-06-10 21:32:09 +00:00
|
|
|
if not args.keep:
|
|
|
|
os.remove(f)
|
2019-09-11 12:44:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-R", "--recursive",
|
2020-04-27 18:16:10 +00:00
|
|
|
action="store_true", default=False,
|
|
|
|
help="Encode files recursively (default: %(default)s)")
|
2020-07-04 22:27:02 +00:00
|
|
|
parser.add_argument("-C", "--codec",
|
2020-07-04 23:01:05 +00:00
|
|
|
action="store", type=str.lower, default="opus",
|
2020-07-04 22:27:02 +00:00
|
|
|
choices=('aac', 'opus'),
|
|
|
|
help="Pick lossy codec to encode with (default: %(default)s)")
|
|
|
|
parser.add_argument("-B", "--bitrate",
|
|
|
|
action="store", type=int, default=192000,
|
|
|
|
help="Bitrate for opus encoding (default: %(default)s)")
|
2020-06-10 21:32:09 +00:00
|
|
|
parser.add_argument("-K", "--keep",
|
|
|
|
action="store_true", default=False,
|
2020-06-20 20:17:03 +00:00
|
|
|
help="Do not delete source file after re-encoding (default: %(default)s)")
|
2020-04-27 18:16:10 +00:00
|
|
|
parser.add_argument("-T", "--track",
|
2020-04-27 21:20:52 +00:00
|
|
|
action="store", type=int, default=None,
|
2020-07-04 22:27:02 +00:00
|
|
|
help="Track to encode using eac3to. If none; autoselects first audio track (default: %(default)s)")
|
2020-04-27 23:56:28 +00:00
|
|
|
parser.add_argument("--noflac",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Disable FLAC encoding (default: %(default)s)")
|
2020-07-04 22:27:02 +00:00
|
|
|
parser.add_argument("-N", "--nolossy",
|
2020-04-27 23:56:28 +00:00
|
|
|
action="store_true", default=False,
|
2020-07-04 22:27:02 +00:00
|
|
|
help="Disable lossy encoding (default: %(default)s)")
|
2019-09-11 12:44:40 +00:00
|
|
|
parser.parse_args()
|
|
|
|
args = parser.parse_args()
|
2020-06-19 00:55:05 +00:00
|
|
|
|
2020-04-27 18:16:10 +00:00
|
|
|
main()
|