Advertisement
Najeebsk

VIDEOS-SEARCH-PLAY.py

Mar 3rd, 2024
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.40 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import os
  4. import vlc
  5.  
  6. class VideoPlayer:
  7.     def __init__(self, root):
  8.         self.root = root
  9.         self.instance = None
  10.         self.player = None
  11.  
  12.         # Color Palette
  13.         self.bg_color = "#336699"
  14.         self.button_color = "#4CAF50"
  15.         self.text_color = "white"
  16.  
  17.         self.main_frame = tk.Frame(root, bg=self.bg_color)
  18.         self.main_frame.pack(fill="both", expand=True)
  19.  
  20.         self.title_label = tk.Label(self.main_frame, text="NAJEEB VIDEO PLAYER", font=("Arial", 24, "bold"), fg=self.text_color, bg=self.bg_color)
  21.         self.title_label.pack(pady=10)
  22.  
  23.         self.search_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  24.         self.search_frame.pack(fill="x")
  25.  
  26.         self.extension_label = tk.Label(self.search_frame, text="File Extension:", bg=self.bg_color, fg=self.text_color)
  27.         self.extension_label.pack(side="left", padx=5, pady=5)
  28.  
  29.         self.extension_entry = tk.Entry(self.search_frame)
  30.         self.extension_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
  31.  
  32.         self.search_button = tk.Button(self.search_frame, text="Search", command=self.search_and_save_videos, bg=self.button_color, fg=self.text_color)
  33.         self.search_button.pack(side="left", padx=5, pady=5)
  34.  
  35.         self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  36.         self.list_frame.pack(side="left", fill="both", expand=False)
  37.  
  38.         self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
  39.         self.button_frame.pack(side="bottom", fill="x")
  40.  
  41.         self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
  42.         self.lst.pack(side="left", fill="both", expand=True)
  43.  
  44.         self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
  45.         self.scrollbar.pack(side="right", fill="y")
  46.         self.lst.config(yscrollcommand=self.scrollbar.set)
  47.  
  48.         self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
  49.         self.play_button.pack(side="left", padx=5, pady=5)
  50.  
  51.         self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
  52.         self.stop_button.pack(side="left", padx=5, pady=5)
  53.  
  54.         self.prev_button = tk.Button(self.button_frame, text="Prev", command=self.prev_video, 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_video, bg=self.button_color, fg=self.text_color)
  58.         self.next_button.pack(side="left", padx=5, pady=5)
  59.  
  60.         self.player_position = tk.Scale(self.button_frame, from_=0, to=100, orient="horizontal", command=self.set_position, length=400)
  61.         self.player_position.pack(side="left", fill="x", padx=5, pady=5)
  62.  
  63.         self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
  64.         self.current_video_label.pack(pady=5)
  65.  
  66.         self.player_frame = tk.Frame(self.main_frame)
  67.         self.player_frame.pack(side="right", fill="both", expand=True)
  68.  
  69.         self.lst.bind("<<ListboxSelect>>", self.show_video)
  70.  
  71.         self.load_video_list()
  72.  
  73.     def load_video_list(self):
  74.         try:
  75.             with open('VIDEOS.txt', 'r', encoding='utf-8') as file:
  76.                 title = None  # Initialize title variable
  77.                 for line in file:
  78.                     line = line.strip()
  79.                     if line.startswith("Title:"):
  80.                         title = line.split(": ")[1]
  81.                         self.lst.insert(tk.END, title)
  82.                         setattr(self, title, None)
  83.                     elif line and title:  # Check if title is defined
  84.                         setattr(self, title, line)
  85.         except FileNotFoundError:
  86.             print("VIDEOS.txt not found.")
  87.             self.search_and_save_videos()
  88.  
  89.     def search_and_save_videos(self):
  90.         video_list = []
  91.         extension = self.extension_entry.get().strip()
  92.         if not extension:
  93.             extension = '.mp4'
  94.         for drive in ['A:', 'B:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:', 'M:', 'N:', 'O:', 'P:', 'Q:', 'R:', 'S:', 'T:', 'U:', 'V:', 'W:', 'X:', 'Y:', 'Z:']:
  95.             if os.path.exists(drive):
  96.                 for root, _, files in os.walk(drive):
  97.                     for file in files:
  98.                         if file.lower().endswith(extension.lower()):
  99.                             filepath = os.path.join(root, file)
  100.                             title = os.path.splitext(file)[0]  # Extract title from file name
  101.                             # Correct drive letter format with backslash
  102.                             filepath = filepath.replace("/", "\\")
  103.                             video_list.append((title, filepath))
  104.  
  105.         # Append the new video list to the existing VIDEOS.txt file
  106.         with open('VIDEOS.txt', 'a', encoding='utf-8') as file:
  107.             for title, filepath in video_list:
  108.                 # Add backslash after the drive letter
  109.                 filepath_with_drive = filepath[:2] + "\\" + filepath[2:]
  110.                 file.write(f"Title: {title}\n{filepath_with_drive}\n\n")
  111.  
  112.         print(f"New videos appended to VIDEOS.txt for files with extension {extension}")
  113.         self.load_video_list()  # Reload the updated list
  114.  
  115.     def play_video(self):
  116.         if self.player:
  117.             if self.player.get_state() == vlc.State.Ended:
  118.                 self.player.stop()
  119.             self.player.play()
  120.             self.current_video_label.config(text="Video playing")
  121.         else:
  122.             print("No video selected.")
  123.  
  124.     def show_video(self, event):
  125.         n = self.lst.curselection()
  126.         if n:
  127.             title = self.lst.get(n[0])
  128.             filepath = getattr(self, title, None)
  129.             if filepath:
  130.                 try:
  131.                     file_extension = os.path.splitext(filepath)[1].lower()
  132.                     if file_extension == ".mp3":
  133.                         self.display_gif("Mp3.gif")
  134.                         self.play_mp3(filepath, title)
  135.                     else:
  136.                         self.play_video_file(filepath, title)
  137.                 except Exception as e:
  138.                     print("Error showing video:", str(e))
  139.                     self.current_video_label.config(text="Error showing video")
  140.  
  141.     def play_mp3(self, filepath, title):
  142.         self.current_video_label.config(text="Current audio: " + title)
  143.         if self.player:
  144.             self.player.stop()
  145.         self.instance = vlc.Instance('--no-xlib')
  146.         media = self.instance.media_new(filepath)
  147.         if self.player is None:
  148.             self.player = self.instance.media_player_new()
  149.             self.player.set_fullscreen(True)
  150.         self.player.set_media(media)
  151.         self.player.play()
  152.  
  153.     def play_video_file(self, filepath, title):
  154.         self.current_video_label.config(text="Current video: " + title)
  155.         if self.player:
  156.             self.player.stop()
  157.         self.instance = vlc.Instance('--no-xlib')
  158.         media = self.instance.media_new(filepath)
  159.         if self.player is None:
  160.             self.player = self.instance.media_player_new()
  161.             self.player.set_fullscreen(True)
  162.         self.player.set_media(media)
  163.  
  164.         if self.player_frame.winfo_children():
  165.             self.player_frame.winfo_children()[0].destroy()
  166.  
  167.         self.player_frame.update()
  168.         self.player.set_hwnd(self.player_frame.winfo_id())
  169.         self.play_video()
  170.  
  171.     def stop_video(self):
  172.         if self.player:
  173.             self.player.stop()
  174.             self.current_video_label.config(text="Video stopped")
  175.         else:
  176.             print("No video selected.")
  177.  
  178.     def prev_video(self):
  179.         if self.lst.curselection():
  180.             current_index = self.lst.curselection()[0]
  181.             prev_index = current_index - 1 if current_index > 0 else self.lst.size() - 1
  182.             self.lst.selection_clear(0, tk.END)
  183.             self.lst.selection_set(prev_index)
  184.             self.lst.activate(prev_index)
  185.             self.show_video(None)
  186.         else:
  187.             print("No video selected.")
  188.  
  189.     def next_video(self):
  190.         if self.lst.curselection():
  191.             current_index = self.lst.curselection()[0]
  192.             next_index = (current_index + 1) % self.lst.size()
  193.             self.lst.selection_clear(0, tk.END)
  194.             self.lst.selection_set(next_index)
  195.             self.lst.activate(next_index)
  196.             self.show_video(None)
  197.         else:
  198.             print("No video selected.")
  199.  
  200.     def set_position(self, value):
  201.         if self.player:
  202.             self.player.set_position(float(value) / 100)
  203.         else:
  204.             print("No video selected.")
  205.  
  206.     def display_gif(self, gif_path):
  207.         # Load the gif image
  208.         gif_image = tk.PhotoImage(file=gif_path)
  209.  
  210.         # Create a label to display the gif
  211.         gif_label = tk.Label(self.main_frame, image=gif_image)
  212.         gif_label.image = gif_image  # Keep a reference to prevent garbage collection
  213.         gif_label.pack()
  214.  
  215.         # You may need to adjust the positioning or size of the label based on your UI layout
  216.  
  217. root = tk.Tk()
  218. root.geometry("800x600+300+50")
  219. root.title("Najeeb VLC Player")
  220. root.configure(bg="#336699")
  221. video_player = VideoPlayer(root)
  222. root.mainloop()
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement