-
Notifications
You must be signed in to change notification settings - Fork 1
/
emby_eac3.py
executable file
·220 lines (181 loc) · 6.67 KB
/
emby_eac3.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
emby eac3 episode locator
"""
import os
import sys
import time
import subprocess
import logging
import shutil
import requests
_LOGGER = logging.getLogger(__name__)
logging.basicConfig(filename='/config/embyac3.log', filemode='a', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
DEFAULT_HEADERS = {
'Content-Type': "application/json",
'Accept': "application/json"
}
AOUTCODEC = "ac3" # Choose audio output type (ac3 only for now)
ASAMPLERATE = "640k" # Choose audio sample rate (default 640k)
CMIXLEVEL = "0.707" # Center channel mix level
AUDIOTRACKPREFIX = "audio ("
VIDEOTRACKPREFIX = "video ("
def convertEAC3(file_path):
"""Convert EAC3 audo to AC3 audio"""
_LOGGER.info("Converting: " + file_path)
name = os.path.splitext(os.path.basename(file_path))[0]
ac3file = "/config/" + name + ".ac3"
newfile = "/config/" + name + "_AC3.mkv"
cmd = ['mkvmerge', '-i', file_path]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
file_info = result.stdout.decode('utf-8')
audio_track_id = None
for line in file_info.split('\n'):
_LOGGER.info("Track found: " + line)
if AUDIOTRACKPREFIX + "AC3/EAC3)" in line:
audio_track_id = line.split(':')[0].split(' ')[2]
break
if not audio_track_id:
_LOGGER.error("No EAC3 audio tracks found.")
return
logline = "EAC3 Audio Track ID: " + audio_track_id
print(logline)
_LOGGER.info(logline)
cmd = ['mkvinfo', file_path]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
file_info2 = result.stdout.decode('utf-8')
# Extract Audio Track Section from Output
start_audio = False
audio_info = ""
for line in file_info2.split('\n'):
if "mkvextract: " + audio_track_id in line:
start_audio = True
if "mkvextract: " + str((int(audio_track_id) + 1)) in line:
start_audio = False
if start_audio:
audio_info += line + "\n"
# Confirm the track is EAC3
audio_track_type = None
for line in audio_info.split('\n'):
if "Codec ID" in line:
audio_track_type = line.split(':')[1]
break
# Try to get track language
audio_track_lang = "eng"
for line in audio_info.split('\n'):
if "Language" in line:
print("Language other than english detected.")
# audio_track_lang = line.split(':')[1]
break
print("Language: " + audio_track_lang)
# Generate AC3 Audio
if audio_track_type == " A_EAC3":
logline = "Generating AC3 Audio File..."
print(logline)
_LOGGER.info(logline)
cmd = ['ffmpeg',
'-drc_scale', '0',
'-i', file_path,
'-vn',
'-acodec', AOUTCODEC,
'-center_mixlev', CMIXLEVEL,
'-ab', ASAMPLERATE,
ac3file]
result = subprocess.run(cmd, stdout=subprocess.DEVNULL)
else:
print("EAC3 Audio Track Could Not Be Verified.")
return
# Get ID of video track
video_track_id = None
for line in file_info.split('\n'):
if VIDEOTRACKPREFIX in line:
video_track_id = line.split(':')[0].split(' ')[2]
break
logline = "Video Track ID: " + video_track_id
print(logline)
_LOGGER.info(logline)
# Merge files together
logline = "Merging files back together..."
print(logline)
_LOGGER.info(logline)
cmd = ['mkvmerge',
'-o', newfile,
'-A',
'--compression', video_track_id+':none', file_path,
'--language', '0:'+audio_track_lang,
'--compression', '0:none', ac3file]
result = subprocess.run(cmd, stdout=subprocess.DEVNULL)
# Remove AC3 file
logline = "Removing temporary AC3 file..."
print(logline)
_LOGGER.info(logline)
try:
os.remove(ac3file)
except FileNotFoundError:
_LOGGER.info("AC3 File doesn't exist, quitting.")
return
# Explicitly set new file permissions
os.chown(newfile, 99, 100)
# Overwrite existing with new file
shutil.move(newfile, file_path)
# We are done
logline = "Conversion of " + name + " is complete."
print(logline)
_LOGGER.info(logline)
return
def main():
"""Main function."""
api_key = sys.argv[1]
user_key = sys.argv[2]
server_url = sys.argv[3]
unc_path = sys.argv[4]
id_dict = []
audio_type = "EAC3"
emby_request = requests.Session()
emby_request.timeout = 5
emby_request.stream = False
emby_request.params = {'api_key': api_key}
emby_request.headers.update(DEFAULT_HEADERS)
list_url = server_url + "/Reports/Items?StartIndex=0&Limit=10000&IncludeItemTypes=Episode&HasQueryLimit=true&GroupBy=None&ReportView=ReportData&DisplayType=Screen&UserId=" \
+ user_key + "&SortOrder=Ascending&ReportColumns=CName%7CEpisodeSeries%7CSeason%7CEpisodeNumber%7CVideo%7CAudio%7CPath&ExportType=CSV"
_LOGGER.info("Starting Emby Update Fetch Loop...")
while True:
#Nested while loop to keep trying to connect rather than fail
while True:
try:
response = emby_request.get(list_url)
except requests.exceptions.RequestException as err:
_LOGGER.error('Requests error getting episode list: %s', err)
#Wait, then try again
time.sleep(60)
continue
else:
try:
episode_list = response.json()
except ValueError:
continue
else:
break
for episode in episode_list['Rows']:
if episode['Columns'][5]['Name'] == audio_type:
#print(episode['Columns'][0]['Name'] + " - " + episode['Id'] + " - " + episode['Columns'][5]['Name'])
id_dict.append(episode['Id'])
for epid in id_dict:
lookup_url = server_url + "/Users/" + user_key + "/Items/" + epid
try:
response = emby_request.get(lookup_url)
except requests.exceptions.RequestException as err:
_LOGGER.error('Requests error getting episode path: %s', err)
return
else:
info = response.json()
local_path = info['MediaSources'][0]['Path'].replace("\\", "/")
local_path = local_path.replace(unc_path, "/tv_mnt")
print("Found: " + local_path)
_LOGGER.info("Found: " + local_path)
convertEAC3(local_path)
# Clear dicts
id_dict = []
# Check every 10 minutes
time.sleep(600)
if __name__ == '__main__':
main()