Skip to content

Commit 60ae97f

Browse files
authored
Merge pull request sumanth-0#713 from sumanth-0/fix-issue-movie
Fix: resolved issue sumanth-0#7 (description of fix)
2 parents c317dab + f3db2ac commit 60ae97f

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

youtube_url_downloader/readme.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 🎬 YouTube Video Downloader
2+
3+
**YouTube Video Downloader** is a simple Python tool to download YouTube videos or audio using the `pytube` library.
4+
It supports downloading the **highest resolution video** or extracting **audio as MP3**.
5+
6+
---
7+
8+
## Features
9+
10+
- 📥 Download YouTube videos (highest resolution)
11+
- 🎵 Download audio only (converted to MP3)
12+
- 💾 Saves files in a `downloads` folder in the current directory
13+
- 🔄 Handles common YouTube errors with retry mechanism
14+
- 🖥️ Interactive and beginner-friendly
15+
16+
---
17+
18+
## Getting Started
19+
20+
1. **Install Python 3**
21+
2. Install `pytube` (latest version):
22+
```bash
23+
pip install --upgrade pytube
24+
25+
📥 YouTube Video Downloader
26+
27+
Menu:
28+
1️⃣ Download YouTube Video/Audio
29+
2️⃣ Exit
30+
Choose an option (1-2): 1
31+
Enter YouTube video URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ
32+
33+
🎥 Video Title: Rick Astley - Never Gonna Give You Up (Official Music Video)
34+
Download as (v)ideo or (a)udio? [v/a]: v
35+
Downloading video to downloads ...
36+
✅ Video downloaded successfully!
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
YouTube Video Downloader (Robust Version)
3+
- Download video or audio using pytube
4+
- Handles common YouTube errors and retry mechanism
5+
"""
6+
7+
import os
8+
import time
9+
10+
try:
11+
from pytube import YouTube
12+
except ImportError:
13+
print("pytube not installed. Install it with 'pip install pytube'")
14+
exit()
15+
16+
# Downloads folder in current directory
17+
DOWNLOAD_FOLDER = os.path.join(os.getcwd(), "downloads")
18+
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
19+
20+
def fetch_video(url, retries=3):
21+
"""
22+
Fetch YouTube object with retry mechanism
23+
"""
24+
for attempt in range(retries):
25+
try:
26+
yt = YouTube(url)
27+
return yt
28+
except Exception as e:
29+
print(f"⚠️ Attempt {attempt+1} failed: {e}")
30+
time.sleep(1)
31+
print("❌ Failed to fetch video info. Check URL or update pytube.")
32+
return None
33+
34+
def download_video(url):
35+
yt = fetch_video(url)
36+
if not yt:
37+
return
38+
print(f"\n🎥 Video Title: {yt.title}")
39+
choice = input("Download as (v)ideo or (a)udio? [v/a]: ").strip().lower()
40+
41+
if choice == "v":
42+
try:
43+
stream = yt.streams.get_highest_resolution()
44+
print(f"Downloading video to {DOWNLOAD_FOLDER} ...")
45+
stream.download(DOWNLOAD_FOLDER)
46+
print("✅ Video downloaded successfully!")
47+
except Exception as e:
48+
print("❌ Download failed:", e)
49+
elif choice == "a":
50+
try:
51+
stream = yt.streams.filter(only_audio=True).first()
52+
out_file = stream.download(DOWNLOAD_FOLDER)
53+
base, ext = os.path.splitext(out_file)
54+
new_file = base + ".mp3"
55+
os.rename(out_file, new_file)
56+
print(f"✅ Audio downloaded successfully as {new_file}")
57+
except Exception as e:
58+
print("❌ Audio download failed:", e)
59+
else:
60+
print("Invalid choice. Cancelled.")
61+
62+
def main():
63+
print("\n📥 YouTube Video Downloader")
64+
while True:
65+
print("\nMenu:")
66+
print("1️⃣ Download YouTube Video/Audio")
67+
print("2️⃣ Exit")
68+
choice = input("Choose an option (1-2): ").strip()
69+
if choice == "1":
70+
url = input("Enter YouTube video URL: ").strip()
71+
if url:
72+
download_video(url)
73+
else:
74+
print("URL cannot be empty.")
75+
elif choice == "2":
76+
print("Goodbye! 🎬")
77+
break
78+
else:
79+
print("Invalid option. Try again.")
80+
81+
if __name__ == "__main__":
82+
main()

0 commit comments

Comments
 (0)