script now uses argparse instead of reinventing the wheel

This commit is contained in:
Kjistóf 2017-01-16 00:09:49 +01:00
parent ce32850410
commit d4c5fd437b
1 changed files with 12 additions and 4 deletions

View File

@ -2,13 +2,22 @@ from subprocess import call, Popen, PIPE, check_output, DEVNULL
from os import listdir, remove
from os.path import splitext, exists
from re import match
from sys import argv
from enum import Enum
from datetime import timedelta
from math import ceil
from argparse import ArgumentParser
# parse arguments
parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.')
parser.add_argument('-nv', '--nonverbose', help='Turn off non-critical messages to user.')
parser.add_argument('url', type=str, help='The URL of the site containing the video to download.')
args = parser.parse_args()
VERBOSE = False if args.nonverbose else True
class Stream(Enum):
AUDIO = 1
VIDEO = 2
@ -18,7 +27,6 @@ class File(Enum):
LOOP = 2
OUTPUT = 3
VERBOSE = True
def print_opt(*args, **kwargs):
if VERBOSE:
print(*args, **kwargs)
@ -81,11 +89,11 @@ def yes_no_question(question, default):
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'}
OUTPUT_KEYS = [File.OUTPUT]
URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if ''
URL = args.url
# fetch video title
FILES[File.OUTPUT] = check_output(('/usr/bin/env', 'youtube-dl', '--get-title', argv[1])).decode('utf-8').strip() + '.mp4'
FILES[File.OUTPUT] = check_output(('/usr/bin/env', 'youtube-dl', '--get-title', args.url)).decode('utf-8').strip() + '.mp4'
# ask what to do if output exists
if exists(FILES[File.OUTPUT]):