Skip to content

Initial Commit of FFMPEG settings. #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ffmpeg
import srt
from PIL import ImageFont, ImageDraw, Image
import yaml


class CountsPerSec:
Expand Down Expand Up @@ -363,6 +364,7 @@ def overlay_srt_line_fast(img, line, font_size, left_offset):
cv2.putText(img, line, pos_calc, cv2.FONT_HERSHEY_COMPLEX, 1/40 * font_size, (255, 255, 255, 255), 1)

return img

@staticmethod
def to_numpy(im):
im.load()
Expand All @@ -374,8 +376,7 @@ def to_numpy(im):
shape, typestr = Image._conv_type_shape(im)
data = np.empty(shape, dtype=np.dtype(typestr))
mem = data.data.cast('B', (data.data.nbytes,))

bufsize, s, offset = 65536, 0, 0

while not s:
l, s, d = e.encode(bufsize)
mem[offset:offset + len(d)] = d
Expand All @@ -384,6 +385,38 @@ def to_numpy(im):
raise RuntimeError("encoder error %d in tobytes" % s)
return data



def load_encoder_defaults_yaml(encoder_name_string: str) -> dict:
"""
Loads an encoder settings yaml with some 'nice' presets that should
work. Modifying encoder_settings.yaml allows tuning of ffmpeg parameters
for the output video.
:param: enco
"""
default_encoder_settings = {
'crf': 0,
'b:v': '40M',
'preset': 'fast'
}
fname = 'encoder_settings.yaml'
if not os.path.isfile(fname):
folder, _ = os.path.split(__file__)
fname = f"{folder}/resources/encoder_settings.yaml"

try:
with open(fname, 'r') as fp:
settings = yaml.load(fp.read(), yaml.SafeLoader)
except FileNotFoundError:
logging.warning("No encoder_settings.yaml found, using defaults")
return default_encoder_settings
try:
return settings[encoder_name_string]
except KeyError:
logging.warning(f'No encoder settings found for '
f'{encoder_name_string}, using defaults')
return default_encoder_settings

@staticmethod
def concatenate_output_files(output_files: list, final_path: str) -> None:
"""
Expand All @@ -408,7 +441,6 @@ def concatenate_output_files(output_files: list, final_path: str) -> None:
finally:
os.remove('file_list.txt')


class OsdPreview:

def __init__(self, config: OsdGenConfig):
Expand Down Expand Up @@ -634,13 +666,13 @@ def render(self):
.filter("scale", **ff_size, force_original_aspect_ratio=1, )
)
encoder_name = self.get_working_encoder()
encoder_settings = Utils.load_encoder_defaults_yaml(encoder_name)
output_args = {
"c:v": encoder_name,
"preset": "fast",
"crf": 0,
"b:v": "40M",
"acodec": "copy"
}
for pair in encoder_settings:
output_args[pair['key']] = pair['value']
output_args['acodec'] = 'copy'
self.render_done = False
process = (
video
Expand Down
Binary file modified requirements-noui.txt
Binary file not shown.
Binary file modified requirements.txt
Binary file not shown.
19 changes: 19 additions & 0 deletions resources/encoder_settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
h264_videotoolbox:
- key: 'q:v'
value: '65'

h265_videotoolbox:
- key: 'q:v'
value: '65'

libx264:
- key: 'crf'
value: '23'
- key: 'preset'
value: 'medium'

libx265:
- key: 'crf'
value: '23'
- key: 'preset'
value: 'medium'