Skip to content

Commit

Permalink
add remove empty folders
Browse files Browse the repository at this point in the history
  • Loading branch information
luewh committed Dec 4, 2024
1 parent b817ebb commit 8f7a4cf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ This project relies on the following software and projects.
- ✅ Skip video if "&" present in video path and name
- [N] Add FFmpeg visual quality metrics (PSNR, SSIM, VMAF)
- ✅ Better encoders parameters
- [N] Remove empty folders
- Remove empty folders
- [ ] Catch process error, and stop frame watch thread appropriately
- [ ] Check tools
- [ ] framesToVideo()
- framesToVideo()
- [ ] Running disable all new button
- [ ] Separate repository for WebUI
- [ ] Better upscale recovery
Expand Down
93 changes: 53 additions & 40 deletions VideoScripy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pathlib import Path
from datetime import timedelta
from shutil import rmtree
from os import walk, mkdir, remove, listdir, getcwd
from os import walk, mkdir, remove, listdir, getcwd, rmdir
from os.path import isdir
from time import time, sleep
from winsound import Beep
Expand Down Expand Up @@ -145,9 +145,9 @@ def __init__(self) -> None:

self.proc = None
self.killed = False


# get video related #####################
# get video related
def setPath(self, path:str="") -> bool:
"""
Set attributes path, return setting result
Expand Down Expand Up @@ -242,7 +242,7 @@ def getVideoInfo(self) -> None:
# get video probe
videoProbe = probe(self.vList[videoIndex]["path"])
# get first video stream info
# TODO
# TODO add multiple video stream waring
videoStreamTemp = [
streams for streams in videoProbe['streams']
if streams['codec_type'] == 'video'
Expand All @@ -262,7 +262,7 @@ def getVideoInfo(self) -> None:
self.vList.pop(videoIndex)


# ffmpeg encoder related #####################
# ffmpeg encoder related
def setEncoder(self, h265=True, gpu=True):
"""
Set encoder parameters according h265 and GPU usage
Expand Down Expand Up @@ -363,10 +363,16 @@ def _getFFmpegCommand(
' -hwaccel cuda -hwaccel_output_format cuda'
f' -i "{video["path"]}"'
' -hwaccel cuda -hwaccel_output_format cuda'
f' -c:v mjpeg_cuvid -r {video["fps"]}'
f' -c:v mjpeg_cuvid -r {video["interpolateFps"]}'
f' -i "{video["interpolateOutputPath"]}/frame%08d.jpg" '
' -map 1:v:0 -map 0:a? -map 0:s?'
f' -c:v {self.encoder} -c:a copy -c:s copy'
f' -b:v {video["optimizeBitRateParam"]}'
f' -r {video["interpolateFps"]}'
f' -y'
f' "{process}\\{video["name"]}" "'
)
return command

elif process == VideoProcess.merge.value:
command += (
Expand All @@ -378,6 +384,7 @@ def _getFFmpegCommand(
f' "{process}\\{video["name"]}" "'
)
return command

else:
print(f'Unknown video process "{process}"')
exit()
Expand All @@ -389,11 +396,10 @@ def _getFFmpegCommand(
f' -y'
f' "{process}\\{video["name"]}" "'
)

return command


# video process related #####################
# video process related
def killProc(self) -> None:
"""
Kill and stop running video process,
Expand Down Expand Up @@ -489,7 +495,7 @@ def pre_optimize(self, video:VideoInfo, width:int, height:int, quality:float) ->

video['optimizeBitRate'] = optimizeBitRate
video['optimizeBitRateParam'] = f'{optimizeBitRate} -maxrate:v {optimizeBitRate} -bufsize:v 800M '


def optimize(self, quality:float=3.0) -> None:
"""
Expand All @@ -515,12 +521,11 @@ def optimize(self, quality:float=3.0) -> None:
mkdir(outputFolder)

for index, video in enumerate(self.vList):
path = video['path']

name = video['name']
width = video['width']
height = video['height']
bitRate = video['bitRate']
frameRate = video['fps']

# show current optimizing video
print('--- {}/{} ---'.format(index+1,len(self.vList)))
Expand All @@ -540,8 +545,13 @@ def optimize(self, quality:float=3.0) -> None:

if self.killed:
return

# notice optimization end

# remove empty folder
try:
rmdir(outputFolder)
except:
pass

noticeProcessEnd()

def resize(self, setWidth:int, setHeight:int, quality:float=3.0) -> None:
Expand Down Expand Up @@ -571,13 +581,10 @@ def resize(self, setWidth:int, setHeight:int, quality:float=3.0) -> None:
mkdir(outputFolder)

for index, video in enumerate(self.vList):

path = video['path']

name = video['name']
width = video['width']
height = video['height']
bitRate = video['bitRate']
frameRate = video['fps']

# show current resizing video
print('--- {}/{} ---'.format(index+1,len(self.vList)))
Expand Down Expand Up @@ -611,13 +618,7 @@ def resize(self, setWidth:int, setHeight:int, quality:float=3.0) -> None:
if newWidth/newHeight != width/height:
print('Warning, rize ratio will be changed')

# save and show size change
resizedSizeText = (
'{}x{} --> {}x{}'
.format(width, height, newWidth, newHeight)
)
self.vList[index]['resizedSize'] = resizedSizeText
print(resizedSizeText)
print(f'{width}x{height} --> {newWidth}x{newHeight}')

# check if resize needed
if newWidth == width and newHeight == height:
Expand All @@ -636,10 +637,13 @@ def resize(self, setWidth:int, setHeight:int, quality:float=3.0) -> None:

if self.killed:
return

print("Done")

# notice resize end
# remove empty folder
try:
rmdir(outputFolder)
except:
pass

noticeProcessEnd()

def upscale(self, upscaleFactor:int=2, quality:float=3) -> None:
Expand Down Expand Up @@ -671,13 +675,10 @@ def upscale(self, upscaleFactor:int=2, quality:float=3) -> None:
mkdir(outputFolder)

for index, video in enumerate(self.vList):

path = video['path']

name = video['name']
width = video['width']
height = video['height']
bitRate = video['bitRate']
frameRate = video['fps']

# show current upscaling video
print('--- {}/{} ---'.format(index+1,len(self.vList)))
Expand Down Expand Up @@ -766,7 +767,12 @@ def upscale(self, upscaleFactor:int=2, quality:float=3) -> None:
# remove upscaled frames
rmtree(upscaleOutputPath)

# notice upscale end
# remove empty folder
try:
rmdir(outputFolder)
except:
pass

noticeProcessEnd()

def interpolate(self, fps:float=30.0, quality:float=3) -> None:
Expand Down Expand Up @@ -797,14 +803,11 @@ def interpolate(self, fps:float=30.0, quality:float=3) -> None:
if not isdir(outputFolder):
mkdir(outputFolder)


for index, video in enumerate(self.vList):

path = video['path']
name = video['name']
width = video['width']
height = video['height']
bitRate = video['bitRate']
frameRate = video['fps']
duration = video['duration']

Expand All @@ -821,7 +824,7 @@ def interpolate(self, fps:float=30.0, quality:float=3) -> None:
# save and show interpolate change
interpolateFrame = ceil(duration.total_seconds() * fps)

print('{frameRate}fps --> {fps}fps')
print(f'{frameRate}fps --> {fps}fps')

self.pre_optimize(video, width, height, quality)

Expand Down Expand Up @@ -874,7 +877,7 @@ def interpolate(self, fps:float=30.0, quality:float=3) -> None:
# remove frames
rmtree(getFramesOutputPath)

video['fps'] = fps
video['interpolateFps'] = fps
video["interpolateOutputPath"] = interpolateOutputPath

# interpolate frames to video
Expand All @@ -889,7 +892,12 @@ def interpolate(self, fps:float=30.0, quality:float=3) -> None:
# remove upscaled frames
rmtree(interpolateOutputPath)

# notice interpolate end
# remove empty folder
try:
rmdir(outputFolder)
except:
pass

noticeProcessEnd()

def merge(self, allVideo:bool=True, allAudio:bool=False, allSubtitle:bool=False) -> None:
Expand Down Expand Up @@ -970,7 +978,12 @@ def merge(self, allVideo:bool=True, allAudio:bool=False, allSubtitle:bool=False)
if self.killed:
return

# notice merging end
# remove empty folder
try:
rmdir(outputFolder)
except:
pass

noticeProcessEnd()


Expand Down

0 comments on commit 8f7a4cf

Please sign in to comment.