Skip to content

Instantly share code, notes, and snippets.

@bplaat
Created November 19, 2021 08:57
Show Gist options
  • Save bplaat/9d1659d56d438863f14e646161f4374c to your computer and use it in GitHub Desktop.
Save bplaat/9d1659d56d438863f14e646161f4374c to your computer and use it in GitHub Desktop.
Multithreaded youtube video music downloader with nice ansi progress bars
import math, os, re, subprocess, sys, tempfile, threading
size = os.get_terminal_size()
cursorY = 0
def printLine(y, line):
global cursorY
if (y <= cursorY):
sys.stdout.write('\033[F' * (cursorY - y))
if (y > cursorY):
sys.stdout.write('\033[E' * (y - cursorY))
sys.stdout.write('\033[K' + line + '\n')
cursorY = y + 1
def downloadThread(lineY):
_, temp_file_path = tempfile.mkstemp()
temp_file_path += '.m4a'
with subprocess.Popen(['yt-dlp', '--newline', '-f', 'bestaudio[ext=m4a]', 'https://www.youtube.com/watch?v=LDU_Txk06tM', '-o', temp_file_path], stdout=subprocess.PIPE) as process:
while process.poll() is None:
line = process.stdout.readline().decode()
procents = re.findall(r'[\d\.]+%', line)
if len(procents) > 0:
procent = float(procents[0][:-1]) / 100
width = size.columns - 14
filled = math.ceil(width * procent)
printLine(lineY, '%2d. %6.02f%% [%s%s]' % (lineY, procent * 100, '#' * filled, ' ' * (width - filled)))
def main():
printLine(0, '# Album downloader')
for i in range(1, 1 + 10):
printLine(i, 'Loading...')
thread = threading.Thread(target=downloadThread, args=[i])
thread.start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment