-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathx264_encode.py
More file actions
157 lines (124 loc) · 4.61 KB
/
x264_encode.py
File metadata and controls
157 lines (124 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python -W ignore::Warning
# coding=utf8
################################################################################
# x264_encode.py
# A simple script to encode Video to h264/mp3/mkv
#
# Required:
# python3
# media-video/mkvtoolnix
# media-video/mplayer
# media-libs/x264
# dev-python/pexpect
import os
import sys
import getopt
import pexpect
# Colors
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
NORMAL = "\033[0m"
BOLD = "\033[1m"
def get_videos_from_file(src_file):
src_videos = [line.strip() for line in open(src_file, "r") if line.strip() != ""]
return src_videos
def make_output_file_name(src_file, output_path, extension):
if "/" in src_file: src_file = src_file.rsplit("/",1)[1]
if "." in src_file: src_file = src_file.rsplit(".",1)[0]
output_file = output_path + src_file + extension
return output_file
def print_ticks(event):
line = event["child_result_list"].pop().decode("utf-8").strip().rsplit("\r",1)[1]
print(CYAN + line + NORMAL, end="\r")
def main():
# Check if arguments are available.
if len(sys.argv[1:]) == 0:
print()
print(RED + "No arguments given. Exiting..." + NORMAL)
usage()
sys.exit(1)
# Find the base directory.
base = sys.argv[0]
base_dir = base[0:base.rfind("/") + 1]
# Settings
video_bitrate = 5000
video_scaling = "" # e.g. 1248:702 or empty
audio_bitrate = 192
audio_language = "eng"
output_path = base_dir + "encoded/"
# Check if output path exists, if not, create it.
if not os.path.isdir(output_path):
os.mkdir(output_path)
# Arguments
try:
options, src_videos = getopt.getopt(sys.argv[1:], "v:s:f:l:")
except getopt.GetoptError as e:
print()
print(RED + "Parameter error. Exiting..." + NORMAL)
print(RED + str(e) + NORMAL)
usage()
sys.exit(1)
for o, a in options:
if o in "-v": video_bitrate = a
if o in "-s": video_scaling = a
if o in "-l": audio_language = a
if o in "-f": src_videos = get_videos_from_file(a)
# Build the command string.
for src_file in src_videos:
command = "mencoder "
command += src_file + " "
command += "-ovc x264 -x264encopts bitrate=" + str(video_bitrate) + " "
if video_scaling: command += "-vf scale=" + video_scaling + " "
command += "-alang " + audio_language + " "
command += "-oac mp3lame -lameopts abr:br=" + str(audio_bitrate) + " "
command += "-af volnorm=1 "
mencoder_output_file = make_output_file_name(src_file, output_path, ".avi")
command += "-o " + mencoder_output_file
print(command)
# Encode the video
print(BLUE + "Encoding file "+ YELLOW + src_file + NORMAL)
(out, mencoder_exit) = pexpect.run(command, events={pexpect.TIMEOUT:print_ticks}, timeout=3, withexitstatus=1)
print()
# Build the mkv command string.
mkv_command = "mkvmerge "
mkv_command += mencoder_output_file + " "
mkv_command += " -o " + make_output_file_name(mencoder_output_file, output_path, ".mkv")
# Convert to mkv
print(BLUE + "Converting file "+ YELLOW + mencoder_output_file + NORMAL)
(out, mkv_exit) = pexpect.run(mkv_command, events={pexpect.TIMEOUT:print_ticks}, timeout=1, withexitstatus=1)
print()
# Remove mencoder temporary video file aka. mencoder_output_file.
print(BLUE + "Cleaning up..." + NORMAL)
try:
os.remove(mencoder_output_file)
except OSError as e:
print(RED + str(e) + NORMAL)
except IOError as e:
print(RED + str(e) + NORMAL)
# Finished.
if mencoder_exit !=0 or mkv_exit !=0:
print(RED + "Something went wrong." + NORMAL)
print(RED + "Finished with exit codes: " + str(mencoder_exit) + " and " + str(mkv_exit))
else:
print(GREEN + "Finished with clean exit codes." + NORMAL)
def usage():
print(GREEN)
print("Usage:")
print("./x264_encode.py [options] <file> : Encode <file> with options.")
print("./x264_encode.py -f <file> : Encode files defined in <file>")
print()
print("Options:")
print("-v <value> : Videobitrate will be set to <value>. Default 5000.")
print("-s X:Y : Scale video to X:Y.")
print("-l <language> : Sets language to <language>. Use like ger or eng here.")
print(NORMAL)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
print(RED + "Exiting..." + NORMAL)