-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (45 loc) · 1.71 KB
/
main.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
import pytube
from pytube import YouTube
import os
import requests
from mutagen.mp4 import MP4
def download_youtube_mp3(video_url, download_path="D:/Coding/YTtoFile/Songs"):
try:
yt = YouTube(video_url)
audio_stream = yt.streams.filter(only_audio=True).first()
os.makedirs(download_path, exist_ok=True)
output_file = audio_stream.download(output_path=download_path)
base, ext = os.path.splitext(output_file)
new_file = base + '.mp3'
os.rename(output_file, new_file)
print(f"Downloaded and converted to MP3: {yt.title}")
except Exception as e:
print(f"An error occurred: {e}")
def download_youtube_mp4(video_url, download_path="D:/Coding/YTtoFile/Videos"):
try:
yt = YouTube(video_url)
thumbnail_url = yt.thumbnail_url
video_stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by('resolution').desc().first()
if video_stream is None:
pass
os.makedirs(download_path, exist_ok=True)
video_filename = video_stream.download(output_path=download_path)
response = requests.get(thumbnail_url, stream=True)
response.raise_for_status()
video = MP4(video_filename)
video["covr"] = [response.content]
video.save()
print(f"Downloaded video as MP4: {yt.title}")
except Exception as e:
print(f"An error occurred: {e}")
print("""
What output would you like?
[1]. MP3
[2]. MP4
""")
choice = int(input("> "))
video_url = input("Enter the YouTube video URL: ")
if choice == 1:
download_youtube_mp3(video_url)
if choice == 2:
download_youtube_mp4(video_url)