-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertMKV.py
170 lines (139 loc) · 5.99 KB
/
convertMKV.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys
import os
import subprocess
import logging
import media_work_queue_enums as enums
import config_helper as config
##############################
# Logging
##############################
logger = logging.getLogger('media_work_queue.convertMKV')
##############################
# Enums
##############################
VideoType = enums.VideoType
##############################
# Constants
##############################
handbrake_exe_path = config.ConfigSectionMap("HANDBRAKE")['exepath']
##############################
# Commands
##############################
preset = config.ConfigSectionMap("HANDBRAKE")['preset']
handbrake_command = '\"{}\" -i \"{}\" -o \"{}\" --preset=\"{}\"' # if you remove any variables, make sure to update convert_handbrake method
##############################
# Paths
##############################
MOVIE_DIR = '{}\\'.format(config.ConfigSectionMap("MEDIA_OUTPUT_PATHS")['moviedir'])
TV_DIR = '{}\\'.format(config.ConfigSectionMap("MEDIA_OUTPUT_PATHS")['tvdir'])
WORKOUT_DIR = '{}\\'.format(config.ConfigSectionMap("MEDIA_OUTPUT_PATHS")['workoutdir'])
ANIME_DIR = '{}\\'.format(config.ConfigSectionMap("MEDIA_OUTPUT_PATHS")['animedir'])
ANIMETV_DIR = '{}\\'.format(config.ConfigSectionMap("MEDIA_OUTPUT_PATHS")['animetvdir'])
##############################
# Keywords
##############################
FEATURETTE_KEYWORD = config.ConfigSectionMap("STRING_MATCH")['featurettefoldername'] # handles if you have the optional Featurette folder included in movie directories
##############################
# Use Handbrake to convert file
##############################
def convert_handbrake(input_path, output_path):
"""convert_handbrake(input_path, output_path)
Inputs:
input_path: fully qualified path to the file you wish to convert
output_path: fully qualified path to the location you wish to output
the converted file
"""
output_path = os.path.splitext(output_path)[0]
output_path = output_path + '.m4v'
logger.info("Converting: " + input_path)
subprocess.call(handbrake_command.format(handbrake_exe_path, input_path, output_path, preset))
logger.info("Conversion Complete!")
logger.info("=============================================================")
##########################################
# Convert all Files in a specified Directory
##########################################
def convert(video_type, path):
"""convert(video_type, path)
Inputs:
video_type: valid types are - {}, {}, {}, {}, {}
path: fully qualified path to the location where your files are that
should be converted
""".format(VideoType.movie, VideoType.tv, VideoType.workout, VideoType.anime, VideoType.animeTV)
if video_type is None:
logger.error('video_type cannot be null')
raise ValueError('video_type cannot be null')
if path is None:
logger.error('path cannot be null')
raise ValueError('path cannot be null')
dest_dir = ''
if video_type == VideoType.movie:
logger.debug("Matched type {}".format(VideoType.movie))
dest_dir = MOVIE_DIR
elif video_type == VideoType.tv:
logger.debug("Matched type {}".format(VideoType.tv))
dest_dir = TV_DIR
elif video_type == VideoType.workout:
logger.debug("Matched type {}".format(VideoType.workout))
dest_dir = WORKOUT_DIR
elif video_type == VideoType.anime:
logger.debug("Matched type {}".format(VideoType.anime))
dest_dir = ANIME_DIR
elif video_type == VideoType.animeTV:
logger.debug("Matched type {}".format(VideoType.animeTV))
dest_dir = ANIMETV_DIR
else:
logger.error("Must specify one of the following: {}, {}, {}, {}, {}".format(VideoType.movie, VideoType.tv, VideoType.workout, VideoType.anime, VideoType.animeTV))
logger.error("You provided: {}".format(video_type))
raise ValueError("Must specify one of the following: {}, {}, {}, {}, {}".format(VideoType.movie, VideoType.tv, VideoType.workout, VideoType.anime, VideoType.animeTV))
basename = os.path.basename(path)
logger.info("===========================================================")
logger.info("Processing: " + basename)
logger.debug("Full Path: {}".format(path))
if os.path.isdir(path):
input_path = path
dest = dest_dir + '\\' + basename + '\\'
logger.debug("Path is a valid directory")
logger.debug("Input Path: {}".format(input_path))
logger.debug("Destination Path: {}".format(dest))
if not os.path.exists(dest):
logger.info("Making Directory: " + dest)
os.makedirs(dest)
# TV Shows have an extra layer of folders to navigate through
# so this first block is for handling non-tv files
if video_type != VideoType.tv and video_type != VideoType.animeTV:
for file in os.listdir(path):
logger.debug("Processing file: {}".format(file))
if file == FEATURETTE_KEYWORD:
dest_feat = dest + '\\{}\\'.format(FEATURETTE_KEYWORD)
if not os.path.exists(dest_feat):
os.makedirs(dest_feat)
for f in os.listdir(path + '\\' + file):
convert_handbrake(input_path + '\\' + file + '\\' + f, dest_feat + f)
else:
convert_handbrake(input_path + '\\' + file, dest + file)
# TV Shows
else:
for season in os.listdir(path):
logger.debug("Processing season: {}".format(season))
dest_season = dest + '\\' + season + '\\'
input_season = input_path + '\\' + season + '\\'
if not os.path.exists(dest_season):
logger.info("Making Directory: {}".format(dest_season))
os.makedirs(dest_season)
for episode in os.listdir(input_season):
logger.debug("Processing episode: {}".format(episode))
convert_handbrake(input_season + '\\' + episode, dest_season + episode)
logger.info("FINISHED!!")
##########################################
# Convert all Files in Current Directory
##########################################
def convert_here(video_type):
"""convert_here(video_type, path)
Inputs:
video_type: valid types are - {}, {}, {}, {}, {}
""".format(VideoType.movie, VideoType.tv, VideoType.workout, VideoType.anime, VideoType.animeTV)
if video_type is None:
logger.error('video_type cannot be null')
raise ValueError('video_type cannot be null')
curr_dir = os.getcwd()
convert(video_type, curr_dir)