Last active
January 26, 2021 21:12
-
-
Save guibeira/a51a06f37269f824d89144660cdd7177 to your computer and use it in GitHub Desktop.
Python script to donwload and converter youtube videos to MP3
This file contains hidden or 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 asyncio | |
import functools | |
import itertools | |
import time | |
DONWLOAD_COMMAND = "youtube-dl -x -i -q --audio-format mp3 --audio-quality 0 --embed-thumbnail --no-playlist --no-warnings {}" | |
musics = [ # examples of musics to donwload | |
"https://www.youtube.com/watch?v=iJPX8zyUmxQ", | |
"https://www.youtube.com/watch?v=9PObvIbB1Fg", | |
"https://www.youtube.com/watch?v=31SicpJGvcg", | |
"https://www.youtube.com/watch?v=pPKx-fon1nY", | |
"https://www.youtube.com/watch?v=QLJHbd-1CEc", | |
"https://www.youtube.com/watch?v=JY1Adhb16fs", | |
] | |
async def spin(msg): | |
for char in itertools.cycle("⠇⠋⠙⠸⠴⠦"): | |
status = msg + " " + char | |
print(status, flush=True, end="\r") | |
try: | |
await asyncio.sleep(0.1) | |
except asyncio.CancelledError: | |
break | |
print(" " * len(status), end="\r") | |
def spinner_msg(msg): | |
def wrapper(func): | |
@functools.wraps(func) | |
async def wrapped(*args): | |
spinner = asyncio.create_task(spin(msg)) | |
return await func(*args) | |
spinner.cancel() | |
return wrapped | |
return wrapper | |
async def runner(cmd, show_output=False): | |
proc = await asyncio.create_subprocess_shell( | |
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE | |
) | |
stdout, stderr = await proc.communicate() | |
if not show_output: | |
return | |
print(f"[{cmd!r} exited with {proc.returncode}]") | |
if stdout: | |
print(f"[stdout]\n{stdout.decode()}") | |
if stderr: | |
print(f"[stderr]\n{stderr.decode()}") | |
async def dowload_music(url_music): | |
cmd = DONWLOAD_COMMAND.format(url_music) | |
await runner(cmd) | |
@spinner_msg("Installing dependencies!") | |
async def install_dependencies(): | |
await runner("pip install --upgrade youtube-dl") | |
@spinner_msg("Downloading musics!") | |
async def donwload_musics(musics): | |
tasks = [dowload_music(music) for music in musics] | |
await asyncio.gather(*tasks) | |
def main(): | |
t0 = time.perf_counter() | |
asyncio.run(install_dependencies()) | |
asyncio.run(donwload_musics(musics)) | |
dt = time.perf_counter() - t0 | |
print(f"Finished in : {dt:0.3}s") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires ffmpeg or avconv and ffprobe or avprobe