Skip to content

Commit

Permalink
better encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
luewh committed Dec 4, 2024
1 parent e4b0735 commit 232a44c
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions VideoScripy.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def getVideoInfo(self) -> None:
self.vList[videoIndex]['width'] = int(videoStreamTemp['width'])
self.vList[videoIndex]['height'] = int(videoStreamTemp['height'])
num, denom = videoStreamTemp['r_frame_rate'].split('/')
self.vList[videoIndex]['fps'] = float(num)/float(denom)
self.vList[videoIndex]['fps'] = round(float(num)/float(denom),2)
self.vList[videoIndex]['nbFrames'] = int(videoStreamTemp['nb_frames'])
except Exception as e:
print(e)
Expand Down Expand Up @@ -284,31 +284,27 @@ def setEncoder(self, h265=True, gpu=True):

if not gpu:
if not h265:
self.encoder = ' libx264 -crf 23'
self.encoder = ' libx264 -crf 1'
else:
self.encoder = ' libx265 -crf 28'
self.encoder = ' libx265 -crf 0'

self.encoder += (
' -preset medium'
)

else:
if not h265:
self.encoder = ' h264_nvenc'
self.encoder = ' h264_nvenc -b_ref_mode middle'
else:
self.encoder = ' hevc_nvenc'
self.encoder = ' hevc_nvenc -weighted_pred 1'

self.encoder += (
' -preset p4'+
' -tune hq'+
' -rc vbr'+
' -rc-lookahead 32'+
' -multipass qres'+
# ' -b_ref_mode middle'
' -spatial_aq 1'+
' -weighted_pred 1'+
' -bufsize:v 800M'+
' -maxrate:v 800M'+
' -preset p6'
' -tune hq'
' -rc vbr'
' -rc-lookahead 32'
' -multipass fullres'
' -spatial_aq 1'
' -cq 1'
)

Expand All @@ -323,23 +319,34 @@ def _getFFmpegCommand(
f' & cd {self.path}'
' & ffmpeg'
)

path = video['path']
name = video['name']
fps = video['fps']

if self.gpu:
haccel = ' -hwaccel cuda -hwaccel_output_format cuda'
else:
haccel = ''


if process == VideoProcess.optimize.value:
command += (
' -hwaccel cuda -hwaccel_output_format cuda'
f' {haccel}'
f' -i "{path}"'
' -map 0:v -map 0:a? -map 0:s?'
)

elif process == VideoProcess.resize.value:
if self.gpu:
resizeFilter = "scale_cuda"
else:
resizeFilter = "scale"
command += (
' -hwaccel cuda -hwaccel_output_format cuda'
f' {haccel}'
f' -i "{path}"'
' -map 0:v -map 0:a? -map 0:s?'
f' -vf scale_cuda={video["resizeWidth"]}:{video["resizeHeight"]}'
f' -vf {resizeFilter}={video["resizeWidth"]}:{video["resizeHeight"]}'
)

elif process == VideoProcess.getFrames.value:
Expand All @@ -361,9 +368,9 @@ def _getFFmpegCommand(
fps = video["interpolateFps"]

command += (
' -hwaccel cuda -hwaccel_output_format cuda'
f' {haccel}'
f' -i "{path}"'
' -hwaccel cuda -hwaccel_output_format cuda'
f' {haccel}'
f' -c:v mjpeg_cuvid -r {fps}'
f' -i "{processOutputPath}/frame%08d.jpg"'
' -map 1:v:0 -map 0:a? -map 0:s?'
Expand All @@ -385,8 +392,8 @@ def _getFFmpegCommand(
exit()

command += (
f' -c:v {self.encoder} -c:a copy -c:s copy'
f' -b:v {video["optimizeBitRateParam"]}'
f' -c:v copy -c:a copy -c:s copy'
f' -c:v:0 {self.encoder} {video["optimizeBitRateParam"]}'
f' -r {fps}'
f' -y'
f' "{process}\\{name}" "'
Expand Down Expand Up @@ -489,7 +496,10 @@ def pre_optimize(self, video:VideoInfo, width:int, height:int, quality:float) ->
print(f'{video["bitRate"]/1_000:_.0f} Kbits/s --> {optimizeBitRate/1_000:_.0f} Kbits/s')

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


def optimize(self, quality:float=3.0) -> None:
Expand Down

0 comments on commit 232a44c

Please sign in to comment.