Advertisement
Najeebsk

YOUTUBE-VLC-DOWNLOAD2.py

Mar 16th, 2024 (edited)
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.39 KB | None | 0 0
  1. import tkinter as tk
  2. import vlc
  3. from youtube_search import YoutubeSearch
  4. from pytube import YouTube
  5. from pytube.exceptions import AgeRestrictedError
  6. import webbrowser
  7.  
  8. class VideoPlayer:
  9.     def __init__(self, root):
  10.         self.root = root
  11.         self.instance = vlc.Instance('--no-xlib')  # Create a VLC instance
  12.         self.player = self.instance.media_player_new()  # Create a media player
  13.         self.video_urls = {}  # Initialize video URL dictionary
  14.  
  15.         # Color Palette
  16.         self.bg_color = "#336699"
  17.         self.button_color = "#4CAF50"
  18.         self.text_color = "white"
  19.  
  20.         self.main_frame = tk.Frame(root, bg=self.bg_color)
  21.         self.main_frame.pack(fill="both", expand=True)
  22.  
  23.         self.title_label = tk.Label(self.main_frame, text="NAJEEB VIDEO PLAYER", font=("Arial", 24, "bold"), fg=self.text_color, bg=self.bg_color)
  24.         self.title_label.pack(pady=10)
  25.  
  26.         self.search_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  27.         self.search_frame.pack(fill="x")
  28.  
  29.         self.search_entry = tk.Entry(self.search_frame)
  30.         self.search_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
  31.  
  32.         self.search_button = tk.Button(self.search_frame, text="Search YouTube", command=self.search_and_play, bg=self.button_color, fg=self.text_color)
  33.         self.search_button.pack(side="left", padx=5, pady=5)
  34.  
  35.         self.save_button = tk.Button(self.search_frame, text="Save", command=self.save_results, bg=self.button_color, fg=self.text_color)
  36.         self.save_button.pack(side="left", padx=5, pady=5)
  37.  
  38.         self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  39.         self.list_frame.pack(side="left", fill="both", expand=False)
  40.  
  41.         self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  42.         self.button_frame.pack(side="bottom", fill="x")
  43.  
  44.         self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
  45.         self.lst.pack(side="left", fill="both", expand=True)
  46.  
  47.         self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
  48.         self.scrollbar.pack(side="right", fill="y")
  49.         self.lst.config(yscrollcommand=self.scrollbar.set)
  50.  
  51.         self.page = 1
  52.         self.per_page = 10
  53.  
  54.         self.prev_button = tk.Button(self.button_frame, text="Prev", command=self.prev_page, bg=self.button_color, fg=self.text_color)
  55.         self.prev_button.pack(side="left", padx=5, pady=5)
  56.  
  57.         self.next_button = tk.Button(self.button_frame, text="Next", command=self.next_page, bg=self.button_color, fg=self.text_color)
  58.         self.next_button.pack(side="left", padx=5, pady=5)
  59.  
  60.         self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
  61.         self.play_button.pack(side="left", padx=5, pady=5)
  62.  
  63.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
  64.         self.stop_button.pack(side="left", padx=5, pady=5)
  65.  
  66.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  67.         self.current_video_label.pack(pady=5)
  68.  
  69.         self.time_label = tk.Label(root, text="", bg=self.bg_color, fg=self.text_color)
  70.         self.time_label.pack(pady=5)
  71.  
  72.         self.player_frame = tk.Frame(self.main_frame)
  73.         self.player_frame.pack(side="right", fill="both", expand=True)
  74.  
  75.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  76.  
  77.         # Load saved video list when the application starts
  78.         self.load_saved_results()
  79.  
  80.     def search_and_play(self):
  81.         query = self.search_entry.get()
  82.         search_results = YoutubeSearch(query, max_results=self.per_page*self.page).to_dict()
  83.         if search_results:
  84.             self.lst.delete(0, tk.END)
  85.             for result in search_results:
  86.                 video_title = result['title']
  87.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  88.                 self.lst.insert(tk.END, video_title)
  89.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  90.             self.save_results()  # Save updated list of videos
  91.  
  92.     def play_video(self):
  93.         if self.lst.curselection():
  94.             title = self.lst.get(self.lst.curselection())
  95.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  96.             print("Playing video:", video_url)  # Print URL for debugging
  97.  
  98.             try:
  99.                 # Use pytube to get the video URL
  100.                 yt = YouTube(video_url)
  101.                 video_url = yt.streams.filter(progressive=True, file_extension='mp4').first().url
  102.  
  103.                 media = self.instance.media_new(video_url)
  104.                 self.player.set_media(media)
  105.                 self.player.set_hwnd(self.player_frame.winfo_id())
  106.                 self.player.play()
  107.                 self.current_video_label.config(text=f"Playing: {title}")
  108.                 self.update_time_label()  # Update video timing display
  109.             except AgeRestrictedError:
  110.                 print("Age restricted video:", video_url)
  111.                 webbrowser.open(video_url)  # Open age-restricted videos in the default web browser
  112.             except Exception as e:
  113.                 print("Error:", e)
  114.                 self.current_video_label.config(text="An error occurred while playing the video.")
  115.         else:
  116.             print("No video selected.")
  117.  
  118.     def stop_video(self):
  119.         self.player.stop()
  120.         self.current_video_label.config(text="Video stopped")
  121.  
  122.     def save_results(self):
  123.         with open("YouTube_list.txt", "a", encoding="utf-8") as file:  # Specify UTF-8 encoding
  124.             for title, url in self.video_urls.items():
  125.                 file.write(f"{title}: {url}\n")
  126.  
  127.     def load_saved_results(self):
  128.         try:
  129.             with open("YouTube_list.txt", "r", encoding="utf-8") as file:  # Specify UTF-8 encoding
  130.                 for line in file:
  131.                     if ':' in line:
  132.                         title, url = line.strip().split(': ', 1)
  133.                         self.video_urls[title] = url
  134.                         self.lst.insert(tk.END, title)
  135.         except FileNotFoundError:
  136.             print("No saved results found.")
  137.  
  138.     def show_video(self, event):
  139.         if hasattr(self, "download_button"):
  140.             self.download_button.destroy()  # Remove existing download button if any
  141.  
  142.         if self.lst.curselection():
  143.             title = self.lst.get(self.lst.curselection())
  144.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  145.  
  146.             # Display download button after stop button
  147.             self.download_button = tk.Button(self.button_frame, text="Download 360p", command=lambda: self.download_video(video_url), bg=self.button_color, fg=self.text_color)
  148.             self.download_button.pack(side="left", padx=5, pady=5, after=self.stop_button)
  149.  
  150.     def download_video(self, video_url):
  151.         try:
  152.             yt = YouTube(video_url)
  153.             stream = yt.streams.filter(res="360p", progressive=True).first()  # Filter for 360p quality
  154.             if stream:
  155.                 stream.download()
  156.                 print("Downloaded successfully.")
  157.             else:
  158.                 print("No 360p stream available for download.")
  159.         except Exception as e:
  160.             print("Error occurred during download:", e)
  161.  
  162.     def prev_page(self):
  163.         if self.page > 1:
  164.             self.page -= 1
  165.             self.search_and_play()
  166.  
  167.     def next_page(self):
  168.         self.page += 1
  169.         self.search_and_play()
  170.  
  171.     def update_time_label(self):
  172.         current_time = self.player.get_time() / 1000  # Convert to seconds
  173.         duration = self.player.get_length() / 1000  # Convert to seconds
  174.  
  175.         current_time_formatted = self.format_time(current_time)
  176.         duration_formatted = self.format_time(duration)
  177.  
  178.         self.time_label.config(text=f"{current_time_formatted} / {duration_formatted}")
  179.         self.root.after(1000, self.update_time_label)  # Update every second
  180.  
  181.     def format_time(self, time_seconds):
  182.         minutes = int(time_seconds // 60)
  183.         seconds = int(time_seconds % 60)
  184.         return f"{minutes:02}:{seconds:02}"
  185.  
  186. root = tk.Tk()
  187. root.geometry("800x600+300+50")
  188. root.title("Najeeb VLC Player")
  189. root.configure(bg="#336699")
  190. video_player = VideoPlayer(root)
  191. root.mainloop()
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement