<filename>src/run.py
from PyQt5 import QtWidgets, uic, QtGui
from PyQt5.QtCore import QDir, QBasicTimer
from PyQt5.QtGui import QPixmap
from PIL import Image

# from video_app import Main, LoadVideo, basedir


import os
import cv2

basedir = os.path.abspath(os.path.dirname(__file__))


class LoadVideo:
    def __init__(self, path_to_file):

        self.path = path_to_file #path to file 

        self.input_path = os.path.join(self.path) #path to file 
        self.video_file = cv2.VideoCapture(self.input_path) #Capture video from path

        self.video_length = int(self.video_file.get(cv2.CAP_PROP_FRAME_COUNT)) #get video length
        self.cap_video_fps = int(self.video_file.get(cv2.CAP_PROP_FPS)) #get fps
        self.cap_video_width = int(self.video_file.get(cv2.CAP_PROP_FRAME_WIDTH)) #get width
        self.cap_video_height = int(self.video_file.get(cv2.CAP_PROP_FRAME_HEIGHT))# get height
        

    def preview(self): # get preview image of video
        self.i = 0
        while self.i != 6:
            self.i += 1
            if self.i == 5:
                _, frame = self.video_file.read()
                img = Image.fromarray(frame)
                img.save("temp/preview.jpeg")
                pixmap = QPixmap("temp/preview.jpeg")
                pixmap = pixmap.scaled(dlg.ImageLbl.frameGeometry().width(), dlg.ImageLbl.frameGeometry().height())
                dlg.ImageLbl.setPixmap(pixmap)
            else:
                pass

        


class Main:
    def __init__(self, fps, color, weight, height, length, path):
        self.output_path = os.path.join(basedir, "output/out_hc.avi") #output path
        self.file = LoadVideo(path) 
        self.video_width = int(weight)
        self.video_height = int(height)
        self.video_length = int(length)

        self.video_fps = (self.file.cap_video_fps * float(fps))
        self.video_color = int(color)

        self.fourcc = cv2.VideoWriter_fourcc(*'DIVX')
        self.out = cv2.VideoWriter(self.output_path,
                                   self.fourcc,
                                   self.video_fps,
                                   (self.video_width, self.video_height),
                                   self.video_color
                                   )
        self.timer = QBasicTimer()
        self.step = 0

    def modify(self):
        while True:
            ret, frame = self.file.video_file.read()
            self.step = self.step + 1
            try:
                gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
            except cv2.error:
                break
            if ret:
                if self.video_color == 0:
                    self.file.color_value = gray
                elif self.video_color == 1:
                    self.file.color_value = frame
                else:
                    self.file.color_value = frame
                frame_number = self.step
                if frame_number <= self.video_length:
                    resized = cv2.resize(self.file.color_value, (self.video_width, self.video_height))
                    completed_percent = round(self.step * 100 / self.video_length)
                    dlg.progressBar.setProperty('value', completed_percent)
                    img = Image.fromarray(resized)
                    self.out.write(resized)
                else:
                    break
            else:
                break
            # cv2.imshow('frame', gray)
            if cv2.waitKey(0) & 0xFF == ord('q'):
                break

        self.out.release()


app = QtWidgets.QApplication([])
dlg = uic.loadUi('run.ui')


def loaded_video_data(path):
    load_file = LoadVideo(path)
    preview = LoadVideo(path).preview() 
    #displaying loaded video data 
    dlg.fpsVideo.setText(str(load_file.cap_video_fps)) 
    dlg.lengthVideo.setText(str(load_file.video_length))
    dlg.widthVideo.setText(str(load_file.cap_video_width))
    dlg.heightVideo.setText(str(load_file.cap_video_height))

    #displaying date to convert 
    dlg.widthConvert.setText(str(load_file.cap_video_width))
    dlg.heightConvert.setText(str(load_file.cap_video_height))

    #set max video length 
    dlg.videoLenSlider.setProperty('maximum',load_file.video_length)

    dlg.lengthConvert.setText(f'{str(dlg.videoLenSlider.value()/load_file.cap_video_fps)} sec')

    #set label of videoLenSlider
    dlg.label_min_len.setText(str(0))
    dlg.label_max_len.setText(f'{str(load_file.video_length/load_file.cap_video_fps)} sec')
    
    #set carrent value of videoLenSlider
    dlg.progressBar.setProperty('value', 0)
    dlg.ComlitedLbl.setText(str(""))


def converting():
    #get values from input
    path_to_file = dlg.filePathWindow.text()
    fps = dlg.fpsSpin.value()
    width = dlg.widthConvert.text()
    height = dlg.heightConvert.text()
    length = dlg.videoLenSlider.value()
    if dlg.colorRadio.isChecked():
        color = 1 #color
    else:
        color = 0 #gray
    main = Main(fps, color, width, height, length, path_to_file)
    main.modify()
    dlg.ComlitedLbl.setText(str("Completed"))


def get_file():
    file_name = QtWidgets.QFileDialog.getOpenFileName(None, 'Open file', QDir.homePath())[0]
    dlg.filePathWindow.setText(file_name)
    loaded_video_data(file_name)




dlg.browseButton.clicked.connect(get_file)
dlg.convertVideo.clicked.connect(converting)


def update_value():
    video_fps = int(dlg.fpsVideo.text())
    new_value = str(round(dlg.videoLenSlider.value()/video_fps, 1))
    display = dlg.lengthConvert.setText(f'{new_value} sec')
dlg.videoLenSlider.valueChanged.connect(update_value)


if __name__ == "__main__":

    dlg.show()
    app.exec()

<filename>src/run.py
from PyQt5 import QtWidgets, uic, QtGui
from PyQt5.QtCore import QDir, QBasicTimer
from PyQt5.QtGui import QPixmap
from PIL import Image

# from video_app import Main, LoadVideo, basedir


import os
import cv2

basedir = os.path.abspath(os.path.dirname(__file__))


class LoadVideo:
    def __init__(self, path_to_file):

        self.path = path_to_file #path to file 

        self.input_path = os.path.join(self.path) #path to file 
        self.video_file = cv2.VideoCapture(self.input_path) #Capture video from path

        self.video_length = int(self.video_file.get(cv2.CAP_PROP_FRAME_COUNT)) #get video length
        self.cap_video_fps = int(self.video_file.get(cv2.CAP_PROP_FPS)) #get fps
        self.cap_video_width = int(self.video_file.get(cv2.CAP_PROP_FRAME_WIDTH)) #get width
        self.cap_video_height = int(self.video_file.get(cv2.CAP_PROP_FRAME_HEIGHT))# get height
        

    def preview(self): # get preview image of video
        self.i = 0
        while self.i != 6:
            self.i += 1
            if self.i == 5:
                _, frame = self.video_file.read()
                img = Image.fromarray(frame)
                img.save("temp/preview.jpeg")
                pixmap = QPixmap("temp/preview.jpeg")
                pixmap = pixmap.scaled(dlg.ImageLbl.frameGeometry().width(), dlg.ImageLbl.frameGeometry().height())
                dlg.ImageLbl.setPixmap(pixmap)
            else:
                pass

        


class Main:
    def __init__(self, fps, color, weight, height, length, path):
        self.output_path = os.path.join(basedir, "output/out_hc.avi") #output path
        self.file = LoadVideo(path) 
        self.video_width = int(weight)
        self.video_height = int(height)
        self.video_length = int(length)

        self.video_fps = (self.file.cap_video_fps * float(fps))
        self.video_color = int(color)

        self.fourcc = cv2.VideoWriter_fourcc(*'DIVX')
        self.out = cv2.VideoWriter(self.output_path,
                                   self.fourcc,
                                   self.video_fps,
                                   (self.video_width, self.video_height),
                                   self.video_color
                                   )
        self.timer = QBasicTimer()
        self.step = 0

    def modify(self):
        while True:
            ret, frame = self.file.video_file.read()
            self.step = self.step + 1
            try:
                gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
            except cv2.error:
                break
            if ret:
                if self.video_color == 0:
                    self.file.color_value = gray
                elif self.video_color == 1:
                    self.file.color_value = frame
                else:
                    self.file.color_value = frame
                frame_number = self.step
                if frame_number <= self.video_length:
                    resized = cv2.resize(self.file.color_value, (self.video_width, self.video_height))
                    completed_percent = round(self.step * 100 / self.video_length)
                    dlg.progressBar.setProperty('value', completed_percent)
                    img = Image.fromarray(resized)
                    self.out.write(resized)
                else:
                    break
            else:
                break
            # cv2.imshow('frame', gray)
            if cv2.waitKey(0) & 0xFF == ord('q'):
                break

        self.out.release()


app = QtWidgets.QApplication([])
dlg = uic.loadUi('run.ui')


def loaded_video_data(path):
    load_file = LoadVideo(path)
    preview = LoadVideo(path).preview() 
    #displaying loaded video data 
    dlg.fpsVideo.setText(str(load_file.cap_video_fps)) 
    dlg.lengthVideo.setText(str(load_file.video_length))
    dlg.widthVideo.setText(str(load_file.cap_video_width))
    dlg.heightVideo.setText(str(load_file.cap_video_height))

    #displaying date to convert 
    dlg.widthConvert.setText(str(load_file.cap_video_width))
    dlg.heightConvert.setText(str(load_file.cap_video_height))

    #set max video length 
    dlg.videoLenSlider.setProperty('maximum',load_file.video_length)

    dlg.lengthConvert.setText(f'{str(dlg.videoLenSlider.value()/load_file.cap_video_fps)} sec')

    #set label of videoLenSlider
    dlg.label_min_len.setText(str(0))
    dlg.label_max_len.setText(f'{str(load_file.video_length/load_file.cap_video_fps)} sec')
    
    #set carrent value of videoLenSlider
    dlg.progressBar.setProperty('value', 0)
    dlg.ComlitedLbl.setText(str(""))


def converting():
    #get values from input
    path_to_file = dlg.filePathWindow.text()
    fps = dlg.fpsSpin.value()
    width = dlg.widthConvert.text()
    height = dlg.heightConvert.text()
    length = dlg.videoLenSlider.value()
    if dlg.colorRadio.isChecked():
        color = 1 #color
    else:
        color = 0 #gray
    main = Main(fps, color, width, height, length, path_to_file)
    main.modify()
    dlg.ComlitedLbl.setText(str("Completed"))


def get_file():
    file_name = QtWidgets.QFileDialog.getOpenFileName(None, 'Open file', QDir.homePath())[0]
    dlg.filePathWindow.setText(file_name)
    loaded_video_data(file_name)




dlg.browseButton.clicked.connect(get_file)
dlg.convertVideo.clicked.connect(converting)


def update_value():
    video_fps = int(dlg.fpsVideo.text())
    new_value = str(round(dlg.videoLenSlider.value()/video_fps, 1))
    display = dlg.lengthConvert.setText(f'{new_value} sec')
dlg.videoLenSlider.valueChanged.connect(update_value)


if __name__ == "__main__":

    dlg.show()
    app.exec()

# from video_app import Main, LoadVideo, basedir #path to file #path to file #Capture video from path #get video length #get fps #get width # get height # get preview image of video #output path # cv2.imshow('frame', gray) #displaying loaded video data #displaying date to convert #set max video length #set label of videoLenSlider #set carrent value of videoLenSlider #get values from input #color #gray