can you give me code python design with QT design check file if video length more than 90seconds move to new folder that generate automatic create with name "90s"
Assistant
User
Can give code with QT design
Assistant
Certainly! Below is an example of a Python script that makes use of PyQt5 for the GUI and the moviepy library to handle video processing. This script will allow users to select a directory, check for videos longer than 90 seconds, and then move these videos to a new folder named "90s".
First, ensure you have the necessary libraries installed:
sh
pip install PyQt5 moviepy
Here's the Python code:
python
import os
import shutil
from moviepy.video.io.VideoFileClip import VideoFileClip
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QVBoxLayout, QLabel
class VideoLengthChecker(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(200, 200, 400, 200)
self.setWindowTitle('Video Length Checker')
self.layout = QVBoxLayout()
self.label = QLabel("Select a directory to check video files:", self)
self.layout.addWidget(self.label)
self.btn_select_dir = QPushButton('Select Directory', self)
self.btn_select_dir.clicked.connect(self.select_directory)
self.layout.addWidget(self.btn_select_dir)
self.results_label = QLabel('', self)
self.layout.addWidget(self.results_label)
self.setLayout(self.layout)
def select_directory(self):
dir_path = QFileDialog.getExistingDirectory(self, 'Select Directory')
if dir_path:
self.process_videos(dir_path)
def process_videos(self, dir_path):
dst_folder = os.path.join(dir_path, '90s')
os.makedirs(dst_folder, exist_ok=True)
moved_files = []
for filename in os.listdir(dir_path):
if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.flv')):
file_path = os.path.join(dir_path, filename)
with VideoFileClip(file_path) as video:
if video.duration > 90:
shutil.move(file_path, os.path.join(dst_folder, filename))
moved_files.append(filename)
self.display_results(moved_files)
def display_results(self, moved_files):
if moved_files:
result_text = 'Moved the following files to "90s" folder:\n' + '\n'.join(moved_files)
else:
result_text = 'No videos longer than 90 seconds were found.'
self.results_label.setText(result_text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = VideoLengthChecker()
ex.show()
sys.exit(app.exec_())
How to Run the Script
Copy the code into a file named video_length_checker.py.
Make sure you have installed the necessary libraries (PyQt5 and moviepy).
Run the script using Python:
sh
python video_length_checker.py
Explanation
PyQt5: We use PyQt5 for creating the GUI. QFileDialog is used to allow the user to select a directory.
moviepy: We use this library to inspect the duration of the video files.
os & shutil: These standard libraries are used to handle file operations, such as moving files to the new folder.
When you run the script, it will open a window with a button to select a directory. The script then checks all video files in the directory, and if any video is longer than 90 seconds, it moves the video to a newly created folder named "90s" inside the selected directory. A results message will provide feedback whether any files were moved.
Certainly! To incorporate a design created with Qt Designer, you need to create a .ui file which defines the layout, and then load this .ui file into your Python script using PyQt5's uic module.
Now, modify your Python script to load this UI layout and connect the functionality.
Here's the complete Python script:
python
import os
import shutil
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog
from moviepy.video.io.VideoFileClip import VideoFileClip
class VideoLengthChecker(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('design.ui', self)
# Connect the button click event to the function
self.btn_select_dir.clicked.connect(self.select_directory)
def select_directory(self):
dir_path = QFileDialog.getExistingDirectory(self, 'Select Directory')
if dir_path:
self.process_videos(dir_path)
def process_videos(self, dir_path):
dst_folder = os.path.join(dir_path, '90s')
os.makedirs(dst_folder, exist_ok=True)
moved_files = []
for filename in os.listdir(dir_path):
if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.flv')):
file_path = os.path.join(dir_path, filename)
with VideoFileClip(file_path) as video:
if video.duration > 90:
shutil.move(file_path, os.path.join(dst_folder, filename))
moved_files.append(filename)
self.display_results(moved_files)
def display_results(self, moved_files):
if moved_files:
result_text = 'Moved the following files to "90s" folder:\n' + '\n'.join(moved_files)
else:
result_text = 'No videos longer than 90 seconds were found.'
self.results_label.setText(result_text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = VideoLengthChecker()
ex.show()
sys.exit(app.exec_())
How to Run the Script
Save your Qt Designer file as design.ui.
Ensure you have the necessary libraries installed (PyQt5 and moviepy).
Save your Python script, for example, as video_length_checker.py.
Run the script using Python:
sh
python video_length_checker.py
Explanation
uic.loadUi: This function loads the .ui file and sets up the GUI as defined.
Self.btn_select_dir.clicked.connect: Connects the button click to the select_directory method.
The rest of the functionality: The code remains similar to the first example, handling the directory selection, video length checking, and moving files.
With this setup, you can use Qt Designer to make the layout and PyQt5 to handle the logic, providing a separation of concerns that makes the code easier to maintain and the UI easier to design.