Advertisement
Najeebsk

YOUTUB-VLC-DOWNLOAD.py

Mar 16th, 2024
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.28 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.slider = tk.Scale(root, from_=0, to=100, orient="horizontal", bg=self.bg_color, fg=self.text_color, command=self.set_video_position)
  73.         self.slider.pack(fill="x")
  74.  
  75.         self.player_frame = tk.Frame(self.main_frame)
  76.         self.player_frame.pack(side="right", fill="both", expand=True)
  77.  
  78.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  79.  
  80.         # Load saved video list when the application starts
  81.         self.load_saved_results()
  82.  
  83.     def search_and_play(self):
  84.         query = self.search_entry.get()
  85.         search_results = YoutubeSearch(query, max_results=self.per_page*self.page).to_dict()
  86.         if search_results:
  87.             self.lst.delete(0, tk.END)
  88.             for result in search_results:
  89.                 video_title = result['title']
  90.                 video_url = 'https://www.youtube.com' + result['url_suffix']
  91.                 self.lst.insert(tk.END, video_title)
  92.                 self.video_urls[video_title] = video_url  # Store title and URL in dictionary
  93.             self.save_results()  # Save updated list of videos
  94.  
  95.     def play_video(self):
  96.         if self.lst.curselection():
  97.             title = self.lst.get(self.lst.curselection())
  98.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  99.             print("Playing video:", video_url)  # Print URL for debugging
  100.  
  101.             try:
  102.                 # Use pytube to get the video URL
  103.                 yt = YouTube(video_url)
  104.                 video_url = yt.streams.filter(progressive=True, file_extension='mp4').first().url
  105.  
  106.                 media = self.instance.media_new(video_url)
  107.                 self.player.set_media(media)
  108.                 self.player.set_hwnd(self.player_frame.winfo_id())
  109.                 self.player.play()
  110.                 self.current_video_label.config(text=f"Playing: {title}")
  111.                 self.update_time_label()  # Update video timing display
  112.                 self.update_slider()  # Update video slider position
  113.             except AgeRestrictedError:
  114.                 print("Age restricted video:", video_url)
  115.                 webbrowser.open(video_url)  # Open age-restricted videos in the default web browser
  116.             except Exception as e:
  117.                 print("Error:", e)
  118.                 self.current_video_label.config(text="An error occurred while playing the video.")
  119.         else:
  120.             print("No video selected.")
  121.  
  122.     def stop_video(self):
  123.         self.player.stop()
  124.         self.current_video_label.config(text="Video stopped")
  125.  
  126.     def save_results(self):
  127.         with open("YouTube_list.txt", "a", encoding="utf-8") as file:  # Specify UTF-8 encoding
  128.             for title, url in self.video_urls.items():
  129.                 file.write(f"{title}: {url}\n")
  130.  
  131.     def load_saved_results(self):
  132.         try:
  133.             with open("YouTube_list.txt", "r", encoding="utf-8") as file:  # Specify UTF-8 encoding
  134.                 for line in file:
  135.                     if ':' in line:
  136.                         title, url = line.strip().split(': ', 1)
  137.                         self.video_urls[title] = url
  138.                         self.lst.insert(tk.END, title)
  139.         except FileNotFoundError:
  140.             print("No saved results found.")
  141.  
  142.     def show_video(self, event):
  143.         if hasattr(self, "download_button"):
  144.             self.download_button.destroy()  # Remove existing download button if any
  145.  
  146.         if self.lst.curselection():
  147.             title = self.lst.get(self.lst.curselection())
  148.             video_url = self.video_urls.get(title)  # Get URL from dictionary
  149.  
  150.             # Display download button after stop button
  151.             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)
  152.             self.download_button.pack(side="left", padx=5, pady=5, after=self.stop_button)
  153.  
  154.     def download_video(self, video_url):
  155.         try:
  156.             yt = YouTube(video_url)
  157.             stream = yt.streams.filter(res="360p", progressive=True).first()  # Filter for 360p quality
  158.             if stream:
  159.                 stream.download()
  160.                 print("Downloaded successfully.")
  161.             else:
  162.                 print("No 360p stream available for download.")
  163.         except Exception as e:
  164.             print("Error occurred during download:", e)
  165.  
  166.     def prev_page(self):
  167.         if self.page > 1:
  168.             self.page -= 1
  169.             self.search_and_play()
  170.  
  171.     def next_page(self):
  172.         self.page += 1
  173.         self.search_and_play()
  174.  
  175.     def update_time_label(self):
  176.         current_time = self.player.get_time() / 1000  # Convert to seconds
  177.         duration = self.player.get_length() / 1000  # Convert to seconds
  178.  
  179.         current_time_formatted = self.format_time(current_time)
  180.         duration_formatted = self.format_time(duration)
  181.  
  182.         self.time_label.config(text=f"{current_time_formatted} / {duration_formatted}")
  183.         self.root.after(1000, self.update_time_label)  # Update every second
  184.  
  185.     def update_slider(self):
  186.         current_time = self.player.get_time() / 1000  # Convert to seconds
  187.         duration = self.player.get_length() / 1000  # Convert to seconds
  188.  
  189.         if duration > 0:
  190.             slider_position = (current_time / duration) * 100
  191.             self.slider.set(slider_position)
  192.  
  193.         self.root.after(1000, self.update_slider)  # Update every second
  194.  
  195.     def set_video_position(self, value):
  196.         duration = self.player.get_length() / 1000  # Convert to seconds
  197.         target_time = (float(value) / 100) * duration
  198.         self.player.set_time(int(target_time * 1000))  # Convert to milliseconds
  199.  
  200.     def format_time(self, time_seconds):
  201.         minutes = int(time_seconds // 60)
  202.         seconds = int(time_seconds % 60)
  203.         return f"{minutes:02}:{seconds:02}"
  204.  
  205. root = tk.Tk()
  206. root.geometry("800x600+300+50")
  207. root.title("Najeeb VLC Player")
  208. root.configure(bg="#336699")
  209. video_player = VideoPlayer(root)
  210. root.mainloop()
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement