Advertisement
Najeebsk

YOUTUBE-AGE-RESTRICTED.py

Mar 11th, 2024
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 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.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
  52.         self.play_button.pack(side="left", padx=5, pady=5)
  53.  
  54.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
  55.         self.stop_button.pack(side="left", padx=5, pady=5)
  56.  
  57.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  58.         self.current_video_label.pack(pady=5)
  59.  
  60.         self.player_frame = tk.Frame(self.main_frame)
  61.         self.player_frame.pack(side="right", fill="both", expand=True)
  62.  
  63.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  64.  
  65.         # Load saved video list when the application starts
  66.         self.load_saved_results()
  67.  
  68.     def search_and_play(self):
  69.         query = self.search_entry.get()
  70.         search_results = YoutubeSearch(query, max_results=5).to_dict()
  71.         if search_results:
  72.             self.lst.delete(0, tk.END)
  73.             for result in search_results:
  74.                 video_title = result['title']
  75.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  76.                 self.lst.insert(tk.END, video_title)
  77.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  78.             self.save_results()  # Save updated list of videos
  79.  
  80.     def play_video(self):
  81.         if self.lst.curselection():
  82.             title = self.lst.get(self.lst.curselection())
  83.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  84.             print("Playing video:", video_url)  # Print URL for debugging
  85.  
  86.             try:
  87.                 # Use pytube to get the video URL
  88.                 yt = YouTube(video_url)
  89.                 video_url = yt.streams.filter(progressive=True, file_extension='mp4').first().url
  90.  
  91.                 media = self.instance.media_new(video_url)
  92.                 self.player.set_media(media)
  93.                 self.player.set_hwnd(self.player_frame.winfo_id())
  94.                 self.player.play()
  95.                 self.current_video_label.config(text=f"Playing: {title}")
  96.             except AgeRestrictedError:
  97.                 print("Age restricted video:", video_url)
  98.                 webbrowser.open(video_url)  # Open age-restricted videos in the default web browser
  99.             except Exception as e:
  100.                 print("Error:", e)
  101.                 self.current_video_label.config(text="An error occurred while playing the video.")
  102.         else:
  103.             print("No video selected.")
  104.  
  105.     def stop_video(self):
  106.         self.player.stop()
  107.         self.current_video_label.config(text="Video stopped")
  108.  
  109.     def save_results(self):
  110.         with open("YouTube_list.txt", "a") as file:  # Append mode to append new results
  111.             for title, url in self.video_urls.items():
  112.                 file.write(f"{title}: {url}\n")
  113.  
  114.     def load_saved_results(self):
  115.         try:
  116.             with open("YouTube_list.txt", "r") as file:
  117.                 for line in file:
  118.                     if ':' in line:
  119.                         title, url = line.strip().split(': ', 1)
  120.                         self.video_urls[title] = url
  121.                         self.lst.insert(tk.END, title)
  122.         except FileNotFoundError:
  123.             print("No saved results found.")
  124.  
  125.     def show_video(self, event):
  126.         # Add implementation to show the selected video
  127.         pass
  128.  
  129. root = tk.Tk()
  130. root.geometry("800x600+300+50")
  131. root.title("Najeeb VLC Player")
  132. root.configure(bg="#336699")
  133. video_player = VideoPlayer(root)
  134. root.mainloop()
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement