2018-11-15 06:49:53 -08:00
|
|
|
#!/usr/bin/python3
|
2019-01-20 22:01:51 -08:00
|
|
|
"""Download audio using youtube-dl.
|
2018-10-22 10:45:11 -07:00
|
|
|
|
2019-01-20 22:01:51 -08:00
|
|
|
Dependencies:
|
|
|
|
=============
|
|
|
|
* youtube-dl
|
2018-10-02 12:29:27 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import pathlib
|
2019-02-20 02:18:34 -08:00
|
|
|
import shutil
|
2018-10-02 12:29:27 -07:00
|
|
|
import subprocess
|
|
|
|
|
2019-01-20 22:01:51 -08:00
|
|
|
# =========== Constants ==========
|
2019-03-06 23:34:48 -08:00
|
|
|
YOUTUBE_DL_BIN = shutil.which("youtube-dl")
|
2019-01-20 22:01:51 -08:00
|
|
|
DEFAULT_FILENAME = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s"
|
|
|
|
|
|
|
|
# ========== Error Codes ==========
|
|
|
|
E_NOURLS = 2
|
|
|
|
|
2019-01-22 10:31:04 -08:00
|
|
|
# ========== Main Script ==========
|
|
|
|
parser = argparse.ArgumentParser()
|
2019-03-06 23:34:48 -08:00
|
|
|
parser.add_argument("-b", "--batchfile", help="provide the links from a text file")
|
|
|
|
parser.add_argument(
|
|
|
|
"-f", "--format", type=str, default="opus", help="the format to use"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n", "--filename", type=str, help="downloaded filename (without extension)"
|
|
|
|
)
|
|
|
|
parser.add_argument("urls", nargs="*", help="video URLs")
|
2019-01-22 10:31:04 -08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-03-06 23:34:48 -08:00
|
|
|
dl_opts = [
|
|
|
|
YOUTUBE_DL_BIN,
|
|
|
|
"--no-part",
|
|
|
|
"--no-continue",
|
|
|
|
"--extract-audio",
|
|
|
|
f"--audio-format={args.format}",
|
|
|
|
]
|
2019-01-22 10:31:04 -08:00
|
|
|
|
|
|
|
# filename handling
|
|
|
|
# if -b is used, DEFAULT_FILENAME must take precedence
|
2019-02-20 02:47:48 -08:00
|
|
|
if args.filename is not None and args.batchfile is None:
|
|
|
|
dl_opts.append(f"--output={args.filename}")
|
2019-01-22 10:31:04 -08:00
|
|
|
else:
|
2019-02-20 02:47:48 -08:00
|
|
|
dl_opts.append(f"--output={DEFAULT_FILENAME}")
|
2019-01-22 10:31:04 -08:00
|
|
|
|
|
|
|
# URL handling
|
2019-02-20 02:47:48 -08:00
|
|
|
if args.batchfile is not None:
|
2019-01-22 10:31:04 -08:00
|
|
|
dl_opts.append(f"--batch-file={args.batchfile}")
|
2019-02-20 02:47:48 -08:00
|
|
|
elif args.urls is not None:
|
|
|
|
dl_opts.extend(args.urls)
|
|
|
|
else:
|
2019-01-22 10:31:04 -08:00
|
|
|
print("URLs are required")
|
|
|
|
exit(E_NOURLS)
|
|
|
|
|
|
|
|
subprocess.run(dl_opts)
|