Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sqlite3
- import tempfile
- from tkinter import *
- from tkinter import filedialog, messagebox
- import vlc
- import subprocess
- class VideoPlayerApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Advanced Video Player")
- self.root.geometry("900x700")
- self.root.config(bg="lightblue")
- # Path to VLC executable (update this if necessary)
- self.vlc_path = "C:/Program Files/VideoLAN/VLC/vlc.exe"
- # Database Setup
- self.db_connection = sqlite3.connect("SongVideo.db")
- self.cursor = self.db_connection.cursor()
- self.cursor.execute('''CREATE TABLE IF NOT EXISTS video_files
- (id INTEGER PRIMARY KEY, filename TEXT, filedata BLOB)''')
- self.db_connection.commit()
- # Store temp files
- self.temp_files = []
- # Browse and Listbox Frame with Scrollbar
- browse_frame = Frame(self.root, bg="lightblue")
- browse_frame.pack(side=LEFT, fill=Y, padx=10)
- # Listbox and Scrollbar for videos
- scrollbar = Scrollbar(browse_frame)
- self.listbox = Listbox(browse_frame, width=30, height=25, yscrollcommand=scrollbar.set)
- scrollbar.config(command=self.listbox.yview)
- scrollbar.pack(side=RIGHT, fill=Y)
- self.listbox.pack(pady=10)
- self.listbox.bind("<<ListboxSelect>>", self.play_selected_video)
- # Buttons for managing videos
- browse_button = Button(browse_frame, text="Browse Folder", command=self.browse_folder)
- browse_button.pack(pady=5)
- add_button = Button(browse_frame, text="Add Video", command=self.add_video_file)
- add_button.pack(pady=5)
- delete_button = Button(browse_frame, text="Delete Video", command=self.delete_video_file)
- delete_button.pack(pady=5)
- save_button = Button(browse_frame, text="Save Selected Video", command=self.save_selected_video_to_folder)
- save_button.pack(pady=5)
- delete_temp_button = Button(browse_frame, text="Delete Temp", command=self.delete_temp_files)
- delete_temp_button.pack(pady=5)
- # Video Display Canvas (for embedding VLC player)
- video_frame = Frame(self.root, bg="black", width=640, height=480)
- video_frame.pack(pady=10)
- # VLC instance and video panel ID
- self.vlc_instance = vlc.Instance()
- self.player = self.vlc_instance.media_player_new()
- self.player.set_hwnd(video_frame.winfo_id()) # Embeds VLC in the Frame
- # Player Controls
- control_frame = Frame(self.root, bg="lightblue")
- control_frame.pack(pady=10)
- self.play_button = Button(control_frame, text="Play", command=self.play_selected_video)
- self.play_button.grid(row=0, column=1, padx=5)
- self.stop_button = Button(control_frame, text="Stop", command=self.stop_video)
- self.stop_button.grid(row=0, column=2, padx=5)
- # Next and Previous Buttons
- self.previous_button = Button(control_frame, text="Previous", command=self.play_previous_video)
- self.previous_button.grid(row=0, column=0, padx=5)
- self.next_button = Button(control_frame, text="Next", command=self.play_next_video)
- self.next_button.grid(row=0, column=3, padx=5)
- # Full Preview Button
- self.full_preview_button = Button(control_frame, text="Full Preview", command=self.full_preview_video)
- self.full_preview_button.grid(row=0, column=4, padx=5)
- # Volume Control Slider
- self.volume_slider = Scale(control_frame, from_=0, to=100, orient=HORIZONTAL, label="Volume",
- command=self.set_volume)
- self.volume_slider.set(50) # Set default volume to 50%
- self.volume_slider.grid(row=1, column=0, columnspan=4, pady=5)
- # Video Slider
- self.video_slider = Scale(control_frame, from_=0, to=1000, orient=HORIZONTAL, length=500,
- command=self.on_slider_move)
- self.video_slider.grid(row=2, column=0, columnspan=4, pady=5)
- self.slider_update_active = False # To control auto-updating of the slider
- # Load Videos from Database on Start
- self.load_videos_from_database()
- def browse_folder(self):
- folder_path = filedialog.askdirectory()
- if not folder_path:
- return
- for filename in os.listdir(folder_path):
- if filename.endswith(('.mp4', '.avi', '.mkv', '.mov')):
- file_path = os.path.join(folder_path, filename)
- with open(file_path, 'rb') as file:
- file_data = file.read()
- self.cursor.execute("SELECT COUNT(*) FROM video_files WHERE filename = ?", (filename,))
- if self.cursor.fetchone()[0] == 0:
- self.cursor.execute("INSERT INTO video_files (filename, filedata) VALUES (?, ?)", (filename, file_data))
- self.db_connection.commit()
- self.listbox.insert(END, filename)
- def load_videos_from_database(self):
- self.listbox.delete(0, END) # Clear any existing items in the Listbox
- self.cursor.execute("SELECT filename FROM video_files")
- video_files = self.cursor.fetchall()
- for video_file in video_files:
- self.listbox.insert(END, video_file[0]) # Insert each filename into the Listbox
- def add_video_file(self):
- file_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4 *.avi *.mkv *.mov")])
- if not file_path:
- return
- filename = os.path.basename(file_path)
- with open(file_path, 'rb') as file:
- file_data = file.read()
- self.cursor.execute("INSERT INTO video_files (filename, filedata) VALUES (?, ?)", (filename, file_data))
- self.db_connection.commit()
- self.listbox.insert(END, filename)
- def delete_video_file(self):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- video_name = self.listbox.get(selected_index)
- self.cursor.execute("DELETE FROM video_files WHERE filename = ?", (video_name,))
- self.db_connection.commit()
- self.listbox.delete(selected_index)
- def save_selected_video_to_folder(self):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- video_name = self.listbox.get(selected_index)
- self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
- video_data = self.cursor.fetchone()[0]
- save_path = filedialog.asksaveasfilename(defaultextension=".mp4", initialfile=video_name)
- if save_path:
- with open(save_path, 'wb') as file:
- file.write(video_data)
- messagebox.showinfo("Save Video", f"Video saved to {save_path}")
- def play_selected_video(self, event=None):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- video_name = self.listbox.get(selected_index)
- self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
- video_data = self.cursor.fetchone()[0]
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
- temp_file.write(video_data)
- temp_file_path = temp_file.name
- self.temp_files.append(temp_file_path)
- media = self.vlc_instance.media_new(temp_file_path)
- self.player.set_media(media)
- self.player.play()
- self.update_slider()
- def play_next_video(self):
- current_index = self.listbox.curselection()
- if current_index:
- next_index = (current_index[0] + 1) % self.listbox.size()
- self.listbox.select_clear(current_index)
- self.listbox.select_set(next_index)
- self.play_selected_video()
- def play_previous_video(self):
- current_index = self.listbox.curselection()
- if current_index:
- previous_index = (current_index[0] - 1) % self.listbox.size()
- self.listbox.select_clear(current_index)
- self.listbox.select_set(previous_index)
- self.play_selected_video()
- def stop_video(self):
- if self.player:
- self.player.stop()
- def full_preview_video(self):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- video_name = self.listbox.get(selected_index)
- self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
- video_data = self.cursor.fetchone()[0]
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
- temp_file.write(video_data)
- temp_file_path = temp_file.name
- # Command to open the video in external VLC in fullscreen mode
- subprocess.Popen([self.vlc_path, "--fullscreen", temp_file_path])
- def set_volume(self, volume):
- self.player.audio_set_volume(int(volume))
- def update_slider(self):
- if self.player.is_playing():
- self.video_slider.config(to=self.player.get_length() // 1000)
- self.video_slider.set(self.player.get_time() // 1000)
- self.root.after(1000, self.update_slider)
- def on_slider_move(self, val):
- if self.player.is_playing():
- new_pos = int(val) * 1000
- self.player.set_time(new_pos)
- def delete_temp_files(self):
- for file_path in self.temp_files:
- try:
- os.remove(file_path)
- except FileNotFoundError:
- pass
- self.temp_files.clear()
- def __del__(self):
- self.db_connection.close()
- self.delete_temp_files()
- if __name__ == "__main__":
- root = Tk()
- app = VideoPlayerApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement