-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_audio_dl.py
62 lines (48 loc) · 1.39 KB
/
single_audio_dl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from pytube import YouTube
import os
import sys
import random
# url input from user
print("Please provide a valid youtube link:")
yt_link = input()
yt_link = yt_link.strip()
yt = None
try:
yt = YouTube(url=yt_link)
print()
except:
print("Invalid YouTube link provided.")
print("Exiting...")
sys.exit(random.randint(-1_000_000, -1))
print("Loading available audio tracks (this may take a few seconds):")
# extract only audio
audio_tracks = yt.streams.filter(only_audio=True)
# Print available tracks to download.
for index, track in zip(range(1, len(audio_tracks)), audio_tracks):
print(f"{index}: {track}")
print()
print("Choose an option (number):")
option = input()
trackIndex = 0
try:
trackIndex = int(option) - 1
except ValueError:
print("Incorrect input provided.")
print("Exiting...")
sys.exit(random.randint(-1_000_000, -1))
# Verify input is withing available options.
if trackIndex < 0 or trackIndex > len(audio_tracks):
print("Incorrect option given.")
print("Exiting...")
sys.exit(random.randint(-1_000_000, -1))
chosen_track = audio_tracks[trackIndex]
print(f"You chose option {trackIndex}")
print("Will start downloading:")
print(chosen_track)
print()
# Download audio track.
try:
output_file = chosen_track.download(os.getcwd())
print("Downloaded file to:", output_file)
except:
print("Error: Failed to download audio track.")