Last active
October 15, 2021 15:16
-
-
Save gaycookie/5df7bfad70fc2528858c54736020a9f3 to your computer and use it in GitHub Desktop.
YouTube Downloader, something like this happens when you're out of idea's.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import youtube_dl, json, os, argparse | |
from pathlib import Path | |
class Config: | |
def __init__(self): | |
self.location = str(Path.joinpath(Path.home(), "Downloads")) | |
self.format = "mp3" | |
@staticmethod | |
def check_config(): | |
return os.path.isfile("config.json") | |
def save_config(self): | |
file = open("config.json", "w+") | |
file.write(json.dumps(self, default=lambda o: o.__dict__, indent=4)) | |
file.close() | |
def load_config(self): | |
if (self.check_config()): | |
file = open("config.json", "r") | |
self.__dict__ = json.loads(file.read()) | |
file.close() | |
class Download: | |
@staticmethod | |
def audio(url): | |
youtube_dl.YoutubeDL({ | |
"format": "bestaudio", | |
"postprocessors": [{ | |
"key": "FFmpegExtractAudio", | |
"preferredcodec": "mp3", | |
"preferredquality": "192", | |
}] | |
}).extract_info(url) | |
@staticmethod | |
def video(url): | |
youtube_dl.YoutubeDL({ | |
"format": "bestvideo" | |
}).extract_info(url) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
config = Config() | |
parser.add_argument('-u', '--url', required=True, help="URL for the video to download.") | |
parser.add_argument('-f', '--format', required=False, default=config.format, help=f"What file format you want to download (default: {config.format}).") | |
if (Config.check_config() == False): | |
print("No config was found, let's create one.") | |
print("In order to create a config, a couple questions will be asked.") | |
location = input(f"1) Location to save downloads (default: {config.location}): ") | |
format = input(f"2) Default download type (default: {config.format}): ") | |
if (location != "" and location != None): config.location = location | |
if (format != "" and format != None): config.format = format | |
config.save_config() | |
else: | |
config.load_config() | |
os.chdir(config.location) | |
args = parser.parse_args() | |
if args.format != config.format: | |
format = args.format | |
Download.audio(args.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
youtube_dl==2021.6.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment