Advertisement
Najeebsk

MP4DATA.pyw

Oct 30th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.91 KB | None | 0 0
  1. import os
  2. import sqlite3
  3. import tempfile
  4. from tkinter import *
  5. from tkinter import filedialog, messagebox
  6. import vlc
  7. import subprocess
  8.  
  9. class VideoPlayerApp:
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.root.title("Najeeb Advanced Video Player")
  13.         self.root.geometry("900x700")
  14.         self.root.config(bg="lightblue")
  15.  
  16.         # Path to VLC executable (update this if necessary)
  17.         self.vlc_path = "C:/Program Files/VideoLAN/VLC/vlc.exe"
  18.  
  19.         # Database Setup
  20.         self.db_connection = sqlite3.connect("SongVideo.db")
  21.         self.cursor = self.db_connection.cursor()
  22.         self.cursor.execute('''CREATE TABLE IF NOT EXISTS video_files
  23.                               (id INTEGER PRIMARY KEY, filename TEXT, filedata BLOB)''')
  24.         self.db_connection.commit()
  25.  
  26.         # Store temp files
  27.         self.temp_files = []
  28.  
  29.         # Browse and Listbox Frame with Scrollbar
  30.         browse_frame = Frame(self.root, bg="lightblue")
  31.         browse_frame.pack(side=LEFT, fill=Y, padx=10)
  32.  
  33.         # Listbox and Scrollbar for videos
  34.         scrollbar = Scrollbar(browse_frame)
  35.         self.listbox = Listbox(browse_frame, width=30, height=25, yscrollcommand=scrollbar.set)
  36.         scrollbar.config(command=self.listbox.yview)
  37.         scrollbar.pack(side=RIGHT, fill=Y)
  38.         self.listbox.pack(pady=10)
  39.         self.listbox.bind("<<ListboxSelect>>", self.play_selected_video)
  40.  
  41.         # Buttons for managing videos
  42.         browse_button = Button(browse_frame, text="Browse Folder", command=self.browse_folder)
  43.         browse_button.pack(pady=5)
  44.         add_button = Button(browse_frame, text="Add Video", command=self.add_video_file)
  45.         add_button.pack(pady=5)
  46.         delete_button = Button(browse_frame, text="Delete Video", command=self.delete_video_file)
  47.         delete_button.pack(pady=5)
  48.         save_button = Button(browse_frame, text="Save Selected Video", command=self.save_selected_video_to_folder)
  49.         save_button.pack(pady=5)
  50.         delete_temp_button = Button(browse_frame, text="Delete Temp", command=self.delete_temp_files)
  51.         delete_temp_button.pack(pady=5)
  52.  
  53.         # Video Display Canvas (for embedding VLC player)
  54.         video_frame = Frame(self.root, bg="black", width=640, height=480)
  55.         video_frame.pack(pady=10)
  56.  
  57.         # VLC instance and video panel ID
  58.         self.vlc_instance = vlc.Instance()
  59.         self.player = self.vlc_instance.media_player_new()
  60.         self.player.set_hwnd(video_frame.winfo_id())  # Embeds VLC in the Frame
  61.  
  62.         # Player Controls
  63.         control_frame = Frame(self.root, bg="lightblue")
  64.         control_frame.pack(pady=10)
  65.  
  66.         self.play_button = Button(control_frame, text="Play", command=self.play_selected_video)
  67.         self.play_button.grid(row=0, column=1, padx=5)
  68.         self.stop_button = Button(control_frame, text="Stop", command=self.stop_video)
  69.         self.stop_button.grid(row=0, column=2, padx=5)
  70.  
  71.         # Next and Previous Buttons
  72.         self.previous_button = Button(control_frame, text="Previous", command=self.play_previous_video)
  73.         self.previous_button.grid(row=0, column=0, padx=5)
  74.         self.next_button = Button(control_frame, text="Next", command=self.play_next_video)
  75.         self.next_button.grid(row=0, column=3, padx=5)
  76.  
  77.         # Full Preview Button
  78.         self.full_preview_button = Button(control_frame, text="Full Preview", command=self.full_preview_video)
  79.         self.full_preview_button.grid(row=0, column=4, padx=5)
  80.  
  81.         # Volume Control Slider
  82.         self.volume_slider = Scale(control_frame, from_=0, to=100, orient=HORIZONTAL, label="Volume",
  83.                                    command=self.set_volume)
  84.         self.volume_slider.set(50)  # Set default volume to 50%
  85.         self.volume_slider.grid(row=1, column=0, columnspan=4, pady=5)
  86.  
  87.         # Video Slider
  88.         self.video_slider = Scale(control_frame, from_=0, to=1000, orient=HORIZONTAL, length=500,
  89.                                   command=self.on_slider_move)
  90.         self.video_slider.grid(row=2, column=0, columnspan=4, pady=5)
  91.         self.slider_update_active = False  # To control auto-updating of the slider
  92.  
  93.         # Load Videos from Database on Start
  94.         self.load_videos_from_database()
  95.  
  96.     def browse_folder(self):
  97.         folder_path = filedialog.askdirectory()
  98.         if not folder_path:
  99.             return
  100.  
  101.         for filename in os.listdir(folder_path):
  102.             if filename.endswith(('.mp4', '.avi', '.mkv', '.mov')):
  103.                 file_path = os.path.join(folder_path, filename)
  104.                 with open(file_path, 'rb') as file:
  105.                     file_data = file.read()
  106.                 self.cursor.execute("SELECT COUNT(*) FROM video_files WHERE filename = ?", (filename,))
  107.                 if self.cursor.fetchone()[0] == 0:
  108.                     self.cursor.execute("INSERT INTO video_files (filename, filedata) VALUES (?, ?)", (filename, file_data))
  109.                     self.db_connection.commit()
  110.                     self.listbox.insert(END, filename)
  111.  
  112.     def load_videos_from_database(self):
  113.         self.listbox.delete(0, END)  # Clear any existing items in the Listbox
  114.         self.cursor.execute("SELECT filename FROM video_files")
  115.         video_files = self.cursor.fetchall()
  116.         for video_file in video_files:
  117.             self.listbox.insert(END, video_file[0])  # Insert each filename into the Listbox
  118.  
  119.     def add_video_file(self):
  120.         file_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4 *.avi *.mkv *.mov")])
  121.         if not file_path:
  122.             return
  123.         filename = os.path.basename(file_path)
  124.         with open(file_path, 'rb') as file:
  125.             file_data = file.read()
  126.         self.cursor.execute("INSERT INTO video_files (filename, filedata) VALUES (?, ?)", (filename, file_data))
  127.         self.db_connection.commit()
  128.         self.listbox.insert(END, filename)
  129.  
  130.     def delete_video_file(self):
  131.         selected_index = self.listbox.curselection()
  132.         if not selected_index:
  133.             return
  134.         video_name = self.listbox.get(selected_index)
  135.         self.cursor.execute("DELETE FROM video_files WHERE filename = ?", (video_name,))
  136.         self.db_connection.commit()
  137.         self.listbox.delete(selected_index)
  138.  
  139.     def save_selected_video_to_folder(self):
  140.         selected_index = self.listbox.curselection()
  141.         if not selected_index:
  142.             return
  143.         video_name = self.listbox.get(selected_index)
  144.         self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
  145.         video_data = self.cursor.fetchone()[0]
  146.  
  147.         save_path = filedialog.asksaveasfilename(defaultextension=".mp4", initialfile=video_name)
  148.         if save_path:
  149.             with open(save_path, 'wb') as file:
  150.                 file.write(video_data)
  151.             messagebox.showinfo("Save Video", f"Video saved to {save_path}")
  152.  
  153.     def play_selected_video(self, event=None):
  154.         selected_index = self.listbox.curselection()
  155.         if not selected_index:
  156.             return
  157.         video_name = self.listbox.get(selected_index)
  158.        
  159.         self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
  160.         video_data = self.cursor.fetchone()[0]
  161.  
  162.         with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
  163.             temp_file.write(video_data)
  164.             temp_file_path = temp_file.name
  165.             self.temp_files.append(temp_file_path)
  166.  
  167.         media = self.vlc_instance.media_new(temp_file_path)
  168.         self.player.set_media(media)
  169.         self.player.play()
  170.         self.update_slider()
  171.  
  172.     def play_next_video(self):
  173.         current_index = self.listbox.curselection()
  174.         if current_index:
  175.             next_index = (current_index[0] + 1) % self.listbox.size()
  176.             self.listbox.select_clear(current_index)
  177.             self.listbox.select_set(next_index)
  178.             self.play_selected_video()
  179.  
  180.     def play_previous_video(self):
  181.         current_index = self.listbox.curselection()
  182.         if current_index:
  183.             previous_index = (current_index[0] - 1) % self.listbox.size()
  184.             self.listbox.select_clear(current_index)
  185.             self.listbox.select_set(previous_index)
  186.             self.play_selected_video()
  187.  
  188.     def stop_video(self):
  189.         if self.player:
  190.             self.player.stop()
  191.  
  192.     def full_preview_video(self):
  193.         selected_index = self.listbox.curselection()
  194.         if not selected_index:
  195.             return
  196.         video_name = self.listbox.get(selected_index)
  197.        
  198.         self.cursor.execute("SELECT filedata FROM video_files WHERE filename = ?", (video_name,))
  199.         video_data = self.cursor.fetchone()[0]
  200.  
  201.         with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
  202.             temp_file.write(video_data)
  203.             temp_file_path = temp_file.name
  204.  
  205.         # Command to open the video in external VLC in fullscreen mode
  206.         subprocess.Popen([self.vlc_path, "--fullscreen", temp_file_path])
  207.  
  208.     def set_volume(self, volume):
  209.         self.player.audio_set_volume(int(volume))
  210.  
  211.     def update_slider(self):
  212.         if self.player.is_playing():
  213.             self.video_slider.config(to=self.player.get_length() // 1000)
  214.             self.video_slider.set(self.player.get_time() // 1000)
  215.             self.root.after(1000, self.update_slider)
  216.  
  217.     def on_slider_move(self, val):
  218.         if self.player.is_playing():
  219.             new_pos = int(val) * 1000
  220.             self.player.set_time(new_pos)
  221.  
  222.     def delete_temp_files(self):
  223.         for file_path in self.temp_files:
  224.             try:
  225.                 os.remove(file_path)
  226.             except FileNotFoundError:
  227.                 pass
  228.         self.temp_files.clear()
  229.  
  230.     def __del__(self):
  231.         self.db_connection.close()
  232.         self.delete_temp_files()
  233.  
  234. if __name__ == "__main__":
  235.     root = Tk()
  236.     app = VideoPlayerApp(root)
  237.     root.mainloop()
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement