diff --git a/fileD.exe b/fileD.exe new file mode 100644 index 0000000..71dea43 Binary files /dev/null and b/fileD.exe differ diff --git a/fileD.py b/fileD.py new file mode 100644 index 0000000..5b34847 --- /dev/null +++ b/fileD.py @@ -0,0 +1,57 @@ +from sys import argv, exit +from functools import partial +from pathlib import Path +from shutil import copyfileobj +from requests import get +from tqdm.auto import tqdm + + +def download(url, filename): + print(filename) + print("Downloading...") + + r = get(url, stream=True, allow_redirects=True) + if r.status_code != 200: + r.raise_for_status() # Will only raise for 4xx codes, so... + raise RuntimeError(f"Request to {url} returned status code {r.status_code}") + file_size = int(r.headers.get('Content-Length', 0)) + + path = Path(filename).expanduser().resolve() + path.parent.mkdir(parents=True, exist_ok=True) + + desc = "(Unknown total file size)" if file_size == 0 else "" + r.raw.read = partial(r.raw.read, decode_content=True) # Decompress if needed + with tqdm.wrapattr(r.raw, "read", total=file_size, desc=desc) as r_raw: + with path.open("wb") as f: + copyfileobj(r_raw, f) + + print("Complete!") + print("Downloaded to", path) + +if len(argv) == 2: + # take system argument as string + url = argv[1] + + filename = url.split('/')[-1] + filename = filename.split('?')[0] + + download(url, filename) + +elif len(argv) == 3: + # take system argument as string + url = argv[1] + filename = argv[2] + + # Getting the file extension + name = url.split('/')[-1] + name = name.split('?')[0] + file_extension = name.split('.')[-1] + + + filename = filename + '.' + file_extension + + download(url, filename) + +else: + print("Usage: fileD '' ") + exit(1) \ No newline at end of file diff --git a/ytd.py b/ytd.py new file mode 100644 index 0000000..e6cfa15 --- /dev/null +++ b/ytd.py @@ -0,0 +1,115 @@ +#!python + +import sys +from pytube import YouTube +import subprocess + +# a function to download only audio +def downloaderaudio(url): + yt = YouTube(url) + title = yt.title.encode("cp850","replace").decode("cp850") + + # remove special characters from title + specialCharacters = ['\\','/',':','*','?','\"','<','>','|'] + for f in specialCharacters: + title = title.replace(f,'_') + #replace spaces with underscores + title = title.replace(' ','_') + + print("Title: ", title) + audio = yt.streams.filter(only_audio=True) + audio.first().download("./", filename=title + ".mp3") + +def downloader(url): + yt = YouTube(url) + #Title of video + title = yt.title + + + title = title.encode("cp850","replace").decode("cp850") + print(title) + + + # remove special characters from title + specialCharacters = ['\\','/',':','*','?','\"','<','>','|'] + for f in specialCharacters: + title = title.replace(f,'_') + #replace spaces with underscores + title = title.replace(' ','_') + + print(title) + video = yt.streams.filter(only_video=True) + audio = yt.streams.filter(only_audio=True) + print("Downloading...") + # download video and audio and then merge them together + video.first().download("./",filename="temp.mp4") + audio.first().download("./",filename="temp.mp3") + print("Downloaded") + # merge video and audio + args = """ffmpeg -i temp.mp4 -i temp.mp3 -c:v copy -c:a aac -strict experimental temp2.mp4""" + subprocess.call(args, shell=True) + # delete temp files + args = """rm temp.mp4 temp.mp3""" + subprocess.call(args, shell=True) + print("Deleted") + # rename file to title of video + args = """mv temp2.mp4 {}.mp4""".format(title) + subprocess.call(args, shell=True) + print("Renamed") + print("Done") + return + +def downloaderlow(url): + yt = YouTube(url) + print(yt.title.encode("cp850","replace").decode("cp850")) + ys = yt.streams.get_highest_resolution() + print("Low Quality") + print("Downloading...") + # Download video + ys.download() + print("Downloaded") + + +def main(): + url = '' + if len(sys.argv) > 1: + if len(sys.argv) == 2: + print("URL: ",sys.argv[1]) + url = sys.argv[1] + downloader(url) + elif len(sys.argv) == 3: + if sys.argv[1] == 'l': + print("URL: ",sys.argv[2]) + print("Low Quality") + url = sys.argv[2] + downloaderlow(url) + elif sys.argv[1] == 'a': + print("URL: ",sys.argv[2]) + print("Audio") + url = sys.argv[2] + downloaderaudio(url) + else: + print("Invalid Arguments") + print("Usage: python ytd.py [l] [url]") + else: + print("Invalid Arguments") + print("Usage: python ytd.py [l] [url]") + url = input('Enter url: ') + quality = input('Enter quality or a for audio only: ') + if quality == "" or quality == "h": + downloader(url) + elif quality == "l" and url != "": + print("Low Quality") + downloaderlow(url) + elif quality == "l" and url == "": + print("Invalid Arguments") + print("Usage: python ytd.py [l] [url]") + elif quality == "a" and url != "": + print("Audio") + downloaderaudio(url) + else: + print("Invalid Arguments") + print("Usage: python ytd.py [l] [url]") + return + +main() \ No newline at end of file