|
| 1 | +# Media Searcher and Downloader Project |
| 2 | + |
| 3 | +This project is a Python-based application designed to search and download media from YouTube. It consists of several modules, each responsible for different functionalities, and offers a command-line interface for user interaction. The primary aim is to provide a streamlined way to search for media and download it with specific resolutions. |
| 4 | + |
| 5 | +### Project Structure |
| 6 | + |
| 7 | +- `Application.py`: The main script that serves as the entry point of the application. |
| 8 | +- `__modules__/header_module.py`: Contains the function to display the application header. |
| 9 | +- `__modules__/youtube_search_Module.py`: Contains functions to search for YouTube videos. |
| 10 | +- `__modules__/youtube_Media_Downloader.py`: Contains functions to download YouTube videos. |
| 11 | + |
| 12 | +### Functionality Overview |
| 13 | + |
| 14 | +#### 1. Application Header (`__modules__/header_module.py`) |
| 15 | + |
| 16 | +**Function: `app_header()`** |
| 17 | + |
| 18 | +- Displays a styled header for the application. |
| 19 | +- Uses a loop to print the header text character by character with delays for a typewriter effect. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +``` |
| 24 | +import sys |
| 25 | +import time |
| 26 | +
|
| 27 | +def app_header(): |
| 28 | + input_str = '''===========================================\n\tMedia Search and Downloader\n===========================================''' |
| 29 | + sys.stdout.write('\n') |
| 30 | + for c in input_str: |
| 31 | + if c == '=': |
| 32 | + time.sleep(0.01) |
| 33 | + else: |
| 34 | + time.sleep(0.1) |
| 35 | +
|
| 36 | + sys.stdout.write(c) |
| 37 | + sys.stdout.flush() |
| 38 | + sys.stdout.write('\n') |
| 39 | + ``` |
| 40 | + |
| 41 | +#### 2. Media Search Module (`__modules__/youtube_search_Module.py`) |
| 42 | + |
| 43 | +**Functions:** |
| 44 | + |
| 45 | +- `search_query(user_query, r_limit)`: Uses the `pytube` library to search for YouTube videos based on user input. |
| 46 | +- `media_search()`: Asks the user for a search term and result limit, then displays the search results with video titles, URLs, and thumbnails. |
| 47 | + |
| 48 | + |
| 49 | +``` |
| 50 | +from pytube import Search |
| 51 | +from pytube import YouTube |
| 52 | +import asyncio |
| 53 | +
|
| 54 | +async def search_query(user_query, r_limit): |
| 55 | + q = Search(user_query) |
| 56 | + results = [] |
| 57 | +
|
| 58 | + while len(results) < r_limit: |
| 59 | + next_results = q.results |
| 60 | + if not next_results: |
| 61 | + break |
| 62 | + results.extend(next_results) |
| 63 | +
|
| 64 | + search_term = user_query |
| 65 | + video_info = [{'title': video.title, 'video_id': video.video_id, 'thumbnail': video.thumbnail_url} for video in results] |
| 66 | +
|
| 67 | + return search_term, video_info[:r_limit] |
| 68 | +
|
| 69 | +async def media_search(): |
| 70 | + try: |
| 71 | + user_input = input("Search here : ") |
| 72 | + result_limit = int(input("Set Result Limit : ")) |
| 73 | + query_results = await search_query(user_input, result_limit) |
| 74 | +
|
| 75 | + search_term, video_info = query_results |
| 76 | + print() |
| 77 | + print('-------------------------------------- Result ---------------------------------------') |
| 78 | + print(f'Search Term : {search_term}') |
| 79 | + print() |
| 80 | +
|
| 81 | + for num, video_info in enumerate(video_info, start=1): |
| 82 | + print(f'{num} | Title ==> {video_info["title"]} <==\n | URL : https://www.youtube.com/watch?v={video_info["video_id"]}\n | Thumbnail : {video_info["thumbnail"]}\n-------------------------------------------------------------------------------\n') |
| 83 | + except Exception as e: |
| 84 | + print(f"Error occurred : {e}") |
| 85 | +``` |
| 86 | + |
| 87 | +#### 3. Media Downloader Module (`__modules__/youtube_Media_Downloader.py`) |
| 88 | + |
| 89 | +**Functions:** |
| 90 | + |
| 91 | +- `get_default_dir()`: Returns the default directory for downloads. |
| 92 | +- `download_media(url, save_to, resolution)`: Downloads a YouTube video at the specified resolution using `yt_dlp` and saves it to the specified directory. |
| 93 | +- `downloadMedia()`: Prompts the user for the video URL, save directory, and desired resolution, then calls `download_media`. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +``` |
| 98 | +import yt_dlp |
| 99 | +import os |
| 100 | +import argparse |
| 101 | +from win10toast import ToastNotifier |
| 102 | +
|
| 103 | +def get_default_dir(): |
| 104 | + return os.path.join(os.path.expanduser('~'), "Downloads") |
| 105 | +
|
| 106 | +def download_media(url, save_to=None, resolution=None): |
| 107 | + if not save_to: |
| 108 | + save_to = get_default_dir() |
| 109 | +
|
| 110 | + if not os.path.exists(save_to): |
| 111 | + os.mkdir(save_to, exist_ok=True) |
| 112 | +
|
| 113 | + ydl_opts = { |
| 114 | + 'outtmpl': os.path.join(save_to, '%(title)s.%(ext)s'), # Output path and filename template |
| 115 | + 'format': 'bestvideo[height<=?{0}]+bestaudio/best[height<=?{0}]'.format(resolution or '1080') |
| 116 | + } |
| 117 | +
|
| 118 | + try: |
| 119 | + with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
| 120 | + info_dict = ydl.extract_info(url, download=True) |
| 121 | + downloaded_file_path = os.path.join(save_to, f"{info_dict.get('title')}.mp4") |
| 122 | + ToastNotifier().show_toast('Downloading Completed Successfully', f'File saved to : {downloaded_file_path}') |
| 123 | + print(f"Download completed successfully. File saved to: {downloaded_file_path}") |
| 124 | +
|
| 125 | + print('\n------------------------------Video Info------------------------------\n') |
| 126 | + print(f"Title: {info_dict.get('title')}") |
| 127 | + print(f"Author: {info_dict.get('uploader')}") |
| 128 | + print(f"Views: {info_dict.get('view_count')}") |
| 129 | + print(f"Length: {info_dict.get('duration')} seconds") |
| 130 | + print(f"Description: {info_dict.get('description')}") |
| 131 | + print('------------------------------------------------------------------------\n') |
| 132 | +
|
| 133 | + except Exception as e: |
| 134 | + print(f"An error occurred: {e}") |
| 135 | +
|
| 136 | +def downloadMedia(): |
| 137 | + url = input("Enter URL Here: ") |
| 138 | + save_to = input("Save to (default is 'Downloads'): ").strip() |
| 139 | + resolution = input("Enter desired resolution (e.g., 360p, 720p): ").strip() |
| 140 | + download_media(url, save_to, resolution) |
| 141 | + ``` |
| 142 | + |
| 143 | +#### 4. Main Application (`Application.py`) |
| 144 | + |
| 145 | +**Functionality:** |
| 146 | + |
| 147 | +- Displays the application header. |
| 148 | +- Provides a command-line interface with options to search for media, download media, or exit the application. |
| 149 | +- Uses async functions to handle media search and download operations efficiently. |
| 150 | + |
| 151 | +``` |
| 152 | +from __modules__.header_module import app_header |
| 153 | +from __modules__.youtube_search_Module import media_search as search_media |
| 154 | +from __modules__.youtube_Media_Downloader import downloadMedia |
| 155 | +
|
| 156 | +async def search_media_function(): |
| 157 | + await search_media() |
| 158 | +
|
| 159 | +def download_media_function(): |
| 160 | + downloadMedia() |
| 161 | +
|
| 162 | +async def main(): |
| 163 | + app_header() |
| 164 | +
|
| 165 | + while True: |
| 166 | + print("1. Search Social Media") |
| 167 | + print("2. Download Media") |
| 168 | + print("3. Exit") |
| 169 | + print() |
| 170 | +
|
| 171 | + select_option = input("Select Option: ") |
| 172 | +
|
| 173 | + if select_option == '1': |
| 174 | + while True: |
| 175 | + await search_media_function() |
| 176 | + user_choice = input('Type "exit" to Main Menu: ') |
| 177 | + if user_choice.lower() == 'exit': |
| 178 | + break |
| 179 | + |
| 180 | + elif select_option == '2': |
| 181 | + while True: |
| 182 | + download_media_function() |
| 183 | + user_choice = input('Type "exit" to Main Menu: ') |
| 184 | + if user_choice.lower() == 'exit': |
| 185 | + break |
| 186 | + |
| 187 | + elif select_option == '3': |
| 188 | + break |
| 189 | + |
| 190 | + else: |
| 191 | + print("Invalid option. Please select 1, 2, or 3.") |
| 192 | +
|
| 193 | +if __name__ == '__main__': |
| 194 | + import asyncio |
| 195 | + asyncio.run(main()) |
| 196 | + ``` |
| 197 | + |
| 198 | +### Usage |
| 199 | + |
| 200 | +1. **Search Social Media**: |
| 201 | + |
| 202 | + - Select option 1. |
| 203 | + - Enter a search term and the number of results to display. |
| 204 | + - View the search results with video titles, URLs, and thumbnails. |
| 205 | +2. **Download Media**: |
| 206 | + |
| 207 | + - Select option 2. |
| 208 | + - Enter the URL of the video, the directory to save the video (default is Downloads), and the desired resolution. |
| 209 | + - The video will be downloaded, and a notification will be displayed upon completion. |
| 210 | +3. **Exit**: |
| 211 | + |
| 212 | + - Select option 3 to exit the application. |
| 213 | + |
| 214 | +### Dependencies |
| 215 | + |
| 216 | +- `pytube` |
| 217 | +- `yt_dlp` |
| 218 | +- `win10toast` |
| 219 | +- `asyncio` |
| 220 | + |
| 221 | +### Installation |
| 222 | + |
| 223 | +1. Clone the repository: |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + ``` |
| 228 | + git clone https://github.com/JawadSher/Python-Projects-Beginner-to-Advance/tree/main/Project%2014%20-%20Media%20Searcher%20and%20Downloader |
| 229 | + cd Media-Searcher-and-Downloader |
| 230 | + ``` |
| 231 | + |
| 232 | +2. Install the required dependencies: |
| 233 | + |
| 234 | +
|
| 235 | + |
| 236 | + ``` |
| 237 | + pip install pytube yt-dlp win10toast |
| 238 | + ``` |
| 239 | + |
| 240 | +3. Run the application: |
| 241 | + |
| 242 | + |
| 243 | + ``` |
| 244 | + python Application.py |
| 245 | + ``` |
| 246 | + |
| 247 | +
|
| 248 | +### Conclusion |
| 249 | +
|
| 250 | +This project provides a comprehensive tool for searching and downloading media from YouTube, with a user-friendly command-line interface and clear separation of functionalities across different modules. The use of async functions ensures efficient handling of I/O-bound operations, making the application responsive and efficient. |
0 commit comments