General code cleanup
This commit is contained in:
parent
e7c18045bd
commit
2e6c398cd1
60
dlaudio.py
Executable file → Normal file
60
dlaudio.py
Executable file → Normal file
@ -1,65 +1,61 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
"""Download audio using youtube-dl, passing
|
"""Download audio using youtube-dl.
|
||||||
a specific set of options specified by the user.
|
|
||||||
|
|
||||||
=====
|
Dependencies:
|
||||||
Usage
|
=============
|
||||||
=====
|
* youtube-dl
|
||||||
>>> dlaudio -f flac -n <filename> "<url>"
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO add support for downloading in flac, and then reencoding it
|
|
||||||
# in opus
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
# =========== Constants ==========
|
||||||
|
YOUTUBE_DL_BIN = '/usr/bin/youtube-dl'
|
||||||
|
DEFAULT_FILENAME = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s"
|
||||||
|
|
||||||
|
# ========== Error Codes ==========
|
||||||
|
E_NOURLS = 2
|
||||||
|
|
||||||
|
# =========== Functions ==========
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-b', '--batch-dl',
|
parser.add_argument('-b', '--batchfile',
|
||||||
dest='batchfile',
|
|
||||||
type=str,
|
type=str,
|
||||||
|
nargs=1,
|
||||||
help='provide the links from a text file')
|
help='provide the links from a text file')
|
||||||
parser.add_argument('-f', '--format',
|
parser.add_argument('-f', '--format',
|
||||||
type=str,
|
type=str,
|
||||||
default='flac',
|
default='opus',
|
||||||
help='the format to use')
|
help='the format to use')
|
||||||
parser.add_argument('-n', '--filename',
|
parser.add_argument('-n', '--filename',
|
||||||
type=str,
|
type=str,
|
||||||
help='the name of the downloaded file (without extension)')
|
help='downloaded filename (without extension)')
|
||||||
parser.add_argument('urls',
|
parser.add_argument('urls',
|
||||||
nargs='*',
|
nargs='*',
|
||||||
help='video URLs')
|
help='video URLs')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
default_filename = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s"
|
dl_opts = [YOUTUBE_DL_BIN,
|
||||||
|
'--no-part',
|
||||||
dl_opts = []
|
'--no-continue',
|
||||||
dl_opts.append('--no-part')
|
'--extract-audio',
|
||||||
dl_opts.append('--no-continue')
|
'--audio-format={args.format}']
|
||||||
dl_opts.append('--extract-audio')
|
|
||||||
dl_opts.append(f"--audio-format={args.format}")
|
|
||||||
|
|
||||||
dl_opts.append(f"--output={args.filename}")
|
|
||||||
|
|
||||||
# filename handling
|
# filename handling
|
||||||
# -b and -n should not be used together
|
# if -b is used, DEFAULT_FILENAME must take precedence
|
||||||
if args.filename and args.batchfile:
|
if args.filename:
|
||||||
print('Ignoring --batch-dl and --filename')
|
dl_opts.append('--output={args.filename}')
|
||||||
dl_opts.append(f"--output={default_filename}")
|
|
||||||
elif args.filename:
|
|
||||||
dl_opts.append(f"--output={pathlib.Path.home()}/Music/{args.filename}.%(ext)s")
|
|
||||||
else:
|
else:
|
||||||
dl_opts.append(f"--output={default_filename}")
|
dl_opts.append('--output={DEFAULT_FILENAME}')
|
||||||
|
|
||||||
# URL handling
|
# URL handling
|
||||||
if args.batchfile:
|
if args.batchfile:
|
||||||
dl_opts.append(f"--batch-file={args.batchfile}")
|
dl_opts.append(f"--batch-file={args.batchfile}")
|
||||||
elif len(args.urls) == 0:
|
elif not args.urls:
|
||||||
print("URLs are required")
|
print("URLs are required")
|
||||||
exit(2)
|
exit(E_NOURLS)
|
||||||
else:
|
else:
|
||||||
dl_opts.extend(args.urls)
|
dl_opts.extend(args.urls)
|
||||||
|
|
||||||
dl = subprocess.run(['youtube-dl'] + dl_opts)
|
subprocess.run(dl_opts)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user