Skip to content

Commit 78a6a56

Browse files
committed
[HEVC-12] #comment Refactored if/else statements to save 19 lines and improve readability
1 parent fe27c2e commit 78a6a56

File tree

2 files changed

+42
-61
lines changed

2 files changed

+42
-61
lines changed

src/TranscodeSession.py

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@
99

1010
class Session():
1111

12-
# Default encoder settings
13-
14-
class Settings:
15-
class RF:
16-
SD = 21
17-
HD = 22
18-
FHD = 23
19-
UHD = 26
20-
21-
class ENCOPTS:
22-
SD = "ctu=32:qg-size=16"
23-
HD = "ctu=32:qg-size=32"
24-
FHD = "ctu=64:qg-size=64"
25-
UHD = "ctu=64:qg-size=64"
26-
2712
# Object lifecycle methods
2813

2914
def __init__(self, file, args):
@@ -37,28 +22,36 @@ def __init__(self, file, args):
3722

3823
# Populate metadata-based attributes
3924
self.path = {"source": os.path.relpath(file)}
40-
self.source = {"height": int(metadata["height"]), "width": int(metadata["width"]), "duration": float(metadata["duration"]), "filename": os.path.splitext(os.path.relpath(self.path["source"], "source"))[0], "filesize": os.path.getsize(self.path["source"]), "bitrate": int(metadata["bit_rate"]), "frames": int(metadata["nb_frames"]), "codec": metadata["codec_name"]}
41-
height = self.source["height"]
42-
if height < 720:
43-
resolution = "SD"
44-
elif 720 <= height < 1080:
45-
resolution = "HD"
46-
elif 1080 <= height < 2160:
47-
resolution = "FHD"
48-
elif 2160 <= height:
49-
resolution = "UHD"
50-
51-
self.source["resolution"] = resolution
25+
self.source = {
26+
"height": int(metadata["height"]),
27+
"width": int(metadata["width"]),
28+
"duration": float(metadata["duration"]),
29+
"filename": os.path.splitext(os.path.relpath(self.path["source"], "source"))[0],
30+
"filesize": os.path.getsize(self.path["source"]),
31+
"bitrate": int(metadata["bit_rate"]),
32+
"frames": int(metadata["nb_frames"]),
33+
"codec": metadata["codec_name"]
34+
}
35+
if self.source["height"] < 720:
36+
self.encoder_quality = 21
37+
self.encoder_options = "ctu=32:qg-size=16"
38+
elif 720 <= self.source["height"] < 1080:
39+
self.encoder_quality = 22
40+
self.encoder_options = "ctu=32:qg-size=32"
41+
elif 1080 <= self.source["height"] < 2160:
42+
self.encoder_quality = 23
43+
self.encoder_options = "ctu=64:qg-size=64"
44+
elif 2160 <= self.source["height"]:
45+
self.encoder_quality = 26
46+
self.encoder_options = "ctu=64:qg-size=64"
5247

5348
# Create empty attributes for dynamic session options
54-
self.encoder_quality = None
55-
self.encoder_preset = None
5649
self.preset_name = None
57-
self.encoder_options = None
5850

59-
# Construct session options and parameters
60-
self.map_options()
51+
# Construct session options and parameters based on command-line arguments
52+
self.options_for_args()
6153

54+
# Construct output parameters
6255
self.output["filename"] = self.source["filename"] + self.output["file_decorator"]
6356
self.path["output"] = os.path.join("hevc", self.output["filename"] + ".mp4")
6457
self.path["log"] = os.path.join("performance", self.output["filename"] + ".log")
@@ -80,39 +73,34 @@ def signal_handler(self, sig, frame):
8073

8174
# Object task methods
8275

83-
def map_options(self):
84-
""" Start with settings based on source resolution and then override defaults based on command-line arguments
76+
def options_for_args(self):
77+
""" Override defaults based on command-line arguments
8578
"""
86-
self.encoder_quality = getattr(self.Settings.RF, self.source["resolution"])
87-
self.encoder_options = getattr(self.Settings.ENCOPTS, self.source["resolution"])
88-
if self.args.best:
89-
self.preset_name = "Best"
90-
elif self.args.baseline:
91-
self.preset_name = "Baseline"
92-
else:
93-
self.preset_name = "Default"
79+
if self.args.quality:
80+
self.encoder_quality = self.args.quality
9481

9582
if self.args.preset:
9683
self.encoder_preset = self.args.preset.lower()
9784
else:
9885
self.encoder_preset = "slow"
9986

100-
if self.args.quality:
101-
self.encoder_quality = self.args.quality
102-
103-
if self.args.small:
104-
self.encoder_options += ":tu-intra-depth=3:tu-inter-depth=3"
105-
10687
self.output = {"file_decorator": "_RF" + str(self.encoder_quality)}
10788
self.output["file_decorator"] += "_{preset}".format(preset=self.encoder_preset.capitalize())
108-
if self.args.baseline:
109-
self.output["file_decorator"] += "_Baseline"
110-
elif self.args.best:
89+
90+
if self.args.best:
91+
self.preset_name = "Best"
11192
self.output["file_decorator"] += "_Best"
93+
elif self.args.baseline:
94+
self.preset_name = "Baseline"
95+
self.output["file_decorator"] += "_Baseline"
96+
else:
97+
self.preset_name = "Default"
11298

11399
if self.args.small:
100+
self.encoder_options += ":tu-intra-depth=3:tu-inter-depth=3"
114101
self.output["file_decorator"] += "_Small"
115102

103+
116104
def validate(self):
117105
""" Verifies that no session attributes are null
118106
"""

transcode.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,9 @@ def evaluate_args():
6464
if not proceed:
6565
sys.exit("Aborting invocation with --all and --quality options.\n")
6666

67-
if not os.path.isdir("performance"):
68-
try:
69-
os.mkdir("performance")
70-
except FileExistsError:
71-
sys.exit("\nFATAL: can't create directory \"performance\" because file with same name exists")
72-
if not os.path.isdir("hevc"):
73-
try:
74-
os.mkdir("hevc")
75-
except FileExistsError:
76-
sys.exit("\nFATAL: can't create directory \"hevc\" because file with same name exists")
67+
for directory in ["performance", "hevc"]:
68+
if not os.path.isdir(directory):
69+
os.mkdir(directory)
7770

7871
return args
7972

0 commit comments

Comments
 (0)