Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- import os
- import vlc
- class VideoPlayer:
- def __init__(self, root):
- self.root = root
- self.instance = None
- self.player = None
- # Color Palette
- self.bg_color = "#336699"
- self.button_color = "#4CAF50"
- self.text_color = "white"
- self.main_frame = tk.Frame(root, bg=self.bg_color)
- self.main_frame.pack(fill="both", expand=True)
- self.title_label = tk.Label(self.main_frame, text="NAJEEB VIDEO PLAYER", font=("Arial", 24, "bold"), fg=self.text_color, bg=self.bg_color)
- self.title_label.pack(pady=10)
- self.search_frame = tk.Frame(self.main_frame, bg=self.bg_color)
- self.search_frame.pack(fill="x")
- self.extension_label = tk.Label(self.search_frame, text="File Extension:", bg=self.bg_color, fg=self.text_color)
- self.extension_label.pack(side="left", padx=5, pady=5)
- self.extension_entry = tk.Entry(self.search_frame)
- self.extension_entry.pack(side="left", fill="x", expand=True, padx=5, pady=5)
- self.search_button = tk.Button(self.search_frame, text="Search", command=self.search_and_save_videos, bg=self.button_color, fg=self.text_color)
- self.search_button.pack(side="left", padx=5, pady=5)
- self.list_frame = tk.Frame(self.main_frame, bg=self.bg_color)
- self.list_frame.pack(side="left", fill="both", expand=False)
- self.button_frame = tk.Frame(self.main_frame, bg=self.bg_color)
- self.button_frame.pack(side="bottom", fill="x")
- self.lst = tk.Listbox(self.list_frame, width=30, height=20, bg=self.bg_color, fg=self.text_color)
- self.lst.pack(side="left", fill="both", expand=True)
- self.scrollbar = tk.Scrollbar(self.list_frame, orient="vertical", command=self.lst.yview)
- self.scrollbar.pack(side="right", fill="y")
- self.lst.config(yscrollcommand=self.scrollbar.set)
- self.play_button = tk.Button(self.button_frame, text="Play", command=self.play_video, bg=self.button_color, fg=self.text_color)
- self.play_button.pack(side="left", padx=5, pady=5)
- self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop_video, bg=self.button_color, fg=self.text_color)
- self.stop_button.pack(side="left", padx=5, pady=5)
- self.prev_button = tk.Button(self.button_frame, text="Prev", command=self.prev_video, bg=self.button_color, fg=self.text_color)
- self.prev_button.pack(side="left", padx=5, pady=5)
- self.next_button = tk.Button(self.button_frame, text="Next", command=self.next_video, bg=self.button_color, fg=self.text_color)
- self.next_button.pack(side="left", padx=5, pady=5)
- self.player_position = tk.Scale(self.button_frame, from_=0, to=100, orient="horizontal", command=self.set_position, length=400)
- self.player_position.pack(side="left", fill="x", padx=5, pady=5)
- self.current_video_label = tk.Label(root, text="No video selected", bg=self.bg_color, fg=self.text_color)
- self.current_video_label.pack(pady=5)
- self.player_frame = tk.Frame(self.main_frame)
- self.player_frame.pack(side="right", fill="both", expand=True)
- self.lst.bind("<<ListboxSelect>>", self.show_video)
- self.load_video_list()
- def load_video_list(self):
- try:
- with open('VIDEOS.txt', 'r', encoding='utf-8') as file:
- title = None # Initialize title variable
- for line in file:
- line = line.strip()
- if line.startswith("Title:"):
- title = line.split(": ")[1]
- self.lst.insert(tk.END, title)
- setattr(self, title, None)
- elif line and title: # Check if title is defined
- setattr(self, title, line)
- except FileNotFoundError:
- print("VIDEOS.txt not found.")
- self.search_and_save_videos()
- def search_and_save_videos(self):
- video_list = []
- extension = self.extension_entry.get().strip()
- if not extension:
- extension = '.mp4'
- 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:']:
- if os.path.exists(drive):
- for root, _, files in os.walk(drive):
- for file in files:
- if file.lower().endswith(extension.lower()):
- filepath = os.path.join(root, file)
- title = os.path.splitext(file)[0] # Extract title from file name
- # Correct drive letter format with backslash
- filepath = filepath.replace("/", "\\")
- video_list.append((title, filepath))
- # Append the new video list to the existing VIDEOS.txt file
- with open('VIDEOS.txt', 'a', encoding='utf-8') as file:
- for title, filepath in video_list:
- # Add backslash after the drive letter
- filepath_with_drive = filepath[:2] + "\\" + filepath[2:]
- file.write(f"Title: {title}\n{filepath_with_drive}\n\n")
- print(f"New videos appended to VIDEOS.txt for files with extension {extension}")
- self.load_video_list() # Reload the updated list
- def play_video(self):
- if self.player:
- if self.player.get_state() == vlc.State.Ended:
- self.player.stop()
- self.player.play()
- self.current_video_label.config(text="Video playing")
- else:
- print("No video selected.")
- def show_video(self, event):
- n = self.lst.curselection()
- if n:
- title = self.lst.get(n[0])
- filepath = getattr(self, title, None)
- if filepath:
- try:
- file_extension = os.path.splitext(filepath)[1].lower()
- if file_extension == ".mp3":
- self.display_gif("Mp3.gif")
- self.play_mp3(filepath, title)
- else:
- self.play_video_file(filepath, title)
- except Exception as e:
- print("Error showing video:", str(e))
- self.current_video_label.config(text="Error showing video")
- def play_mp3(self, filepath, title):
- self.current_video_label.config(text="Current audio: " + title)
- if self.player:
- self.player.stop()
- self.instance = vlc.Instance('--no-xlib')
- media = self.instance.media_new(filepath)
- if self.player is None:
- self.player = self.instance.media_player_new()
- self.player.set_fullscreen(True)
- self.player.set_media(media)
- self.player.play()
- def play_video_file(self, filepath, title):
- self.current_video_label.config(text="Current video: " + title)
- if self.player:
- self.player.stop()
- self.instance = vlc.Instance('--no-xlib')
- media = self.instance.media_new(filepath)
- if self.player is None:
- self.player = self.instance.media_player_new()
- self.player.set_fullscreen(True)
- self.player.set_media(media)
- if self.player_frame.winfo_children():
- self.player_frame.winfo_children()[0].destroy()
- self.player_frame.update()
- self.player.set_hwnd(self.player_frame.winfo_id())
- self.play_video()
- def stop_video(self):
- if self.player:
- self.player.stop()
- self.current_video_label.config(text="Video stopped")
- else:
- print("No video selected.")
- def prev_video(self):
- if self.lst.curselection():
- current_index = self.lst.curselection()[0]
- prev_index = current_index - 1 if current_index > 0 else self.lst.size() - 1
- self.lst.selection_clear(0, tk.END)
- self.lst.selection_set(prev_index)
- self.lst.activate(prev_index)
- self.show_video(None)
- else:
- print("No video selected.")
- def next_video(self):
- if self.lst.curselection():
- current_index = self.lst.curselection()[0]
- next_index = (current_index + 1) % self.lst.size()
- self.lst.selection_clear(0, tk.END)
- self.lst.selection_set(next_index)
- self.lst.activate(next_index)
- self.show_video(None)
- else:
- print("No video selected.")
- def set_position(self, value):
- if self.player:
- self.player.set_position(float(value) / 100)
- else:
- print("No video selected.")
- def display_gif(self, gif_path):
- # Load the gif image
- gif_image = tk.PhotoImage(file=gif_path)
- # Create a label to display the gif
- gif_label = tk.Label(self.main_frame, image=gif_image)
- gif_label.image = gif_image # Keep a reference to prevent garbage collection
- gif_label.pack()
- # You may need to adjust the positioning or size of the label based on your UI layout
- root = tk.Tk()
- root.geometry("800x600+300+50")
- root.title("Najeeb VLC Player")
- root.configure(bg="#336699")
- video_player = VideoPlayer(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement