2019-08-25 00:40:39 +02:00
|
|
|
import logging
|
2016-03-22 22:36:39 +01:00
|
|
|
import os
|
2017-01-15 12:29:40 +01:00
|
|
|
import platform
|
2018-03-03 16:11:23 +01:00
|
|
|
import re
|
2019-08-25 00:40:39 +02:00
|
|
|
from json import dumps
|
|
|
|
from random import sample
|
2018-01-13 21:17:12 +01:00
|
|
|
from re import match
|
2019-08-25 00:40:39 +02:00
|
|
|
from shutil import which
|
2016-03-22 22:36:39 +01:00
|
|
|
|
2019-08-25 00:40:39 +02:00
|
|
|
from requests import codes
|
|
|
|
from requests import post
|
|
|
|
from requests import Timeout
|
2018-05-13 01:44:21 +02:00
|
|
|
from svtplay_dl.utils.output import formatname
|
2018-06-24 23:17:02 +02:00
|
|
|
from svtplay_dl.utils.proc import run_program
|
2016-03-22 22:36:39 +01:00
|
|
|
|
|
|
|
|
2019-08-25 00:33:51 +02:00
|
|
|
class postprocess:
|
2018-05-13 01:44:00 +02:00
|
|
|
def __init__(self, stream, config, subfixes=None):
|
2016-03-22 22:36:39 +01:00
|
|
|
self.stream = stream
|
2018-05-13 01:44:00 +02:00
|
|
|
self.config = config
|
2016-06-20 18:33:42 +02:00
|
|
|
self.subfixes = subfixes
|
2016-03-30 18:18:43 +02:00
|
|
|
self.detect = None
|
2016-03-22 23:00:50 +01:00
|
|
|
for i in ["ffmpeg", "avconv"]:
|
|
|
|
self.detect = which(i)
|
|
|
|
if self.detect:
|
|
|
|
break
|
|
|
|
|
2016-06-20 18:33:42 +02:00
|
|
|
def sublanguage(self):
|
|
|
|
# parse() function partly borrowed from a guy on github. /thanks!
|
|
|
|
# https://github.com/riobard/srt.py/blob/master/srt.py
|
|
|
|
def parse(self):
|
|
|
|
def parse_block(block):
|
2019-08-25 00:27:31 +02:00
|
|
|
lines = block.strip("-").split("\n")
|
|
|
|
txt = "\r\n".join(lines[2:])
|
2016-06-20 18:33:42 +02:00
|
|
|
return txt
|
2017-01-15 12:29:40 +01:00
|
|
|
|
2018-01-13 20:27:40 +01:00
|
|
|
if platform.system() == "Windows":
|
2017-01-15 12:29:40 +01:00
|
|
|
fd = open(self, encoding="utf8")
|
|
|
|
else:
|
|
|
|
fd = open(self)
|
2019-09-06 22:49:49 +02:00
|
|
|
return list(map(parse_block, fd.read().strip().replace("\r", "").split("\n\n")))
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2016-06-20 18:33:42 +02:00
|
|
|
def query(self):
|
2017-12-08 23:08:56 +01:00
|
|
|
_ = parse(self)
|
2019-09-06 22:49:49 +02:00
|
|
|
random_sentences = " ".join(sample(_, len(_) if len(_) < 8 else 8)).replace("\r\n", "")
|
2019-08-25 00:27:31 +02:00
|
|
|
url = "https://whatlanguage.herokuapp.com"
|
2017-02-15 23:15:50 +01:00
|
|
|
payload = {"query": random_sentences}
|
2018-01-30 20:11:37 +01:00
|
|
|
# Note: requests handles json from version 2.4.2 and onwards so i use json.dumps for now.
|
2019-08-25 00:27:31 +02:00
|
|
|
headers = {"content-type": "application/json"}
|
2016-06-20 18:33:42 +02:00
|
|
|
try:
|
2018-01-30 20:11:37 +01:00
|
|
|
# Note: reasonable timeout i guess? svtplay-dl is mainly used while multitasking i presume,
|
|
|
|
# and it is heroku after all (fast enough)
|
|
|
|
r = post(url, data=dumps(payload), headers=headers, timeout=30)
|
2016-06-20 18:33:42 +02:00
|
|
|
if r.status_code == codes.ok:
|
2016-11-27 19:20:41 +01:00
|
|
|
try:
|
|
|
|
response = r.json()
|
2019-08-25 00:27:31 +02:00
|
|
|
return response["language"]
|
2016-11-27 19:20:41 +01:00
|
|
|
except TypeError:
|
2019-08-25 00:27:31 +02:00
|
|
|
return "und"
|
2016-06-20 18:33:42 +02:00
|
|
|
else:
|
2019-09-06 22:49:49 +02:00
|
|
|
logging.error("Server error appeared. Setting language as undetermined.")
|
2019-08-25 00:27:31 +02:00
|
|
|
return "und"
|
2016-06-20 18:33:42 +02:00
|
|
|
except Timeout:
|
2019-09-06 22:49:49 +02:00
|
|
|
logging.error("30 seconds server timeout reached. Setting language as undetermined.")
|
2019-08-25 00:27:31 +02:00
|
|
|
return "und"
|
2016-06-20 18:33:42 +02:00
|
|
|
|
|
|
|
langs = []
|
2019-08-25 00:27:31 +02:00
|
|
|
exceptions = {"lulesamiska": "smj", "meankieli": "fit", "jiddisch": "yid"}
|
2018-01-13 21:17:12 +01:00
|
|
|
if self.subfixes and len(self.subfixes) >= 2:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Determining the languages of the subtitles.")
|
2018-01-30 20:11:37 +01:00
|
|
|
else:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Determining the language of the subtitle.")
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("get_all_subtitles"):
|
2016-06-20 18:33:42 +02:00
|
|
|
for subfix in self.subfixes:
|
2019-09-06 22:49:49 +02:00
|
|
|
if [exceptions[key] for key in exceptions.keys() if match(key, subfix.strip("-"))]:
|
2019-08-25 00:27:31 +02:00
|
|
|
if "oversattning" in subfix.strip("-"):
|
|
|
|
subfix = subfix.strip("-").split(".")[0]
|
2016-06-20 18:33:42 +02:00
|
|
|
else:
|
2019-08-25 00:27:31 +02:00
|
|
|
subfix = subfix.strip("-")
|
2016-06-20 18:33:42 +02:00
|
|
|
langs += [exceptions[subfix]]
|
|
|
|
continue
|
2019-09-06 22:49:49 +02:00
|
|
|
subfile = "{}.srt".format(os.path.splitext(formatname(self.stream.output, self.config, self.stream.output_extention))[0] + subfix)
|
2016-06-20 18:33:42 +02:00
|
|
|
langs += [query(subfile)]
|
|
|
|
else:
|
2019-09-06 22:49:49 +02:00
|
|
|
subfile = "{}.srt".format(os.path.splitext(formatname(self.stream.output, self.config, self.stream.output_extention))[0])
|
2016-06-20 18:33:42 +02:00
|
|
|
langs += [query(subfile)]
|
|
|
|
if len(langs) >= 2:
|
2019-08-25 00:27:31 +02:00
|
|
|
logging.info("Language codes: " + ", ".join(langs))
|
2018-01-30 20:11:37 +01:00
|
|
|
else:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Language code: " + langs[0])
|
2016-06-20 18:33:42 +02:00
|
|
|
return langs
|
|
|
|
|
2016-03-26 21:38:31 +01:00
|
|
|
def remux(self):
|
2016-03-30 18:18:43 +02:00
|
|
|
if self.detect is None:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.error("Cant detect ffmpeg or avconv. Cant mux files without it.")
|
2016-03-22 22:36:39 +01:00
|
|
|
return
|
|
|
|
if self.stream.finished is False:
|
|
|
|
return
|
2016-06-20 18:33:42 +02:00
|
|
|
|
2019-09-06 22:49:49 +02:00
|
|
|
if formatname(self.stream.output, self.config, self.stream.output_extention).endswith(".mp4") is False:
|
|
|
|
orig_filename = formatname(self.stream.output, self.config, self.stream.output_extention)
|
2016-06-02 01:47:25 +02:00
|
|
|
name, ext = os.path.splitext(orig_filename)
|
2019-08-25 00:33:51 +02:00
|
|
|
new_name = "{}.mp4".format(name)
|
2016-06-20 18:33:42 +02:00
|
|
|
|
2018-03-03 16:11:23 +01:00
|
|
|
cmd = [self.detect, "-i", orig_filename]
|
2018-03-13 00:44:34 +01:00
|
|
|
_, stdout, stderr = run_program(cmd, False) # return 1 is good here.
|
2019-09-15 17:31:49 +02:00
|
|
|
streams = _streams(stderr)
|
|
|
|
videotrack, audiotrack = _checktracks(streams)
|
2018-03-03 16:11:23 +01:00
|
|
|
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle"):
|
2019-09-06 22:49:49 +02:00
|
|
|
logging.info("Muxing {} and merging its subtitle into {}".format(orig_filename, new_name))
|
2016-06-20 18:33:42 +02:00
|
|
|
else:
|
2019-08-25 00:33:51 +02:00
|
|
|
logging.info("Muxing {} into {}".format(orig_filename, new_name))
|
2017-01-02 21:09:35 +01:00
|
|
|
|
2019-08-25 00:33:51 +02:00
|
|
|
tempfile = "{}.temp".format(orig_filename)
|
2019-09-14 14:17:45 +02:00
|
|
|
arguments = []
|
|
|
|
if videotrack:
|
2019-09-14 22:44:44 +02:00
|
|
|
arguments += ["-map", "{}".format(videotrack)]
|
2019-09-14 14:17:45 +02:00
|
|
|
if audiotrack:
|
2019-09-14 22:44:44 +02:00
|
|
|
arguments += ["-map", "{}".format(audiotrack)]
|
2019-09-14 14:17:45 +02:00
|
|
|
arguments += ["-c", "copy", "-f", "mp4"]
|
2019-09-15 17:31:49 +02:00
|
|
|
if ext == ".ts" and "aac" in _getcodec(streams, audiotrack):
|
2016-06-02 01:47:25 +02:00
|
|
|
arguments += ["-bsf:a", "aac_adtstoasc"]
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle"):
|
2016-06-20 18:33:42 +02:00
|
|
|
langs = self.sublanguage()
|
|
|
|
for stream_num, language in enumerate(langs):
|
2019-08-25 00:27:31 +02:00
|
|
|
arguments += [
|
|
|
|
"-map",
|
|
|
|
str(stream_num + 1),
|
|
|
|
"-c:s:" + str(stream_num),
|
|
|
|
"mov_text",
|
|
|
|
"-metadata:s:s:" + str(stream_num),
|
|
|
|
"language=" + language,
|
|
|
|
]
|
2018-01-13 21:21:49 +01:00
|
|
|
if self.subfixes and len(self.subfixes) >= 2:
|
2016-06-20 18:33:42 +02:00
|
|
|
for subfix in self.subfixes:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name + subfix)
|
2016-06-20 18:33:42 +02:00
|
|
|
cmd += ["-i", subfile]
|
|
|
|
else:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name)
|
2016-06-20 18:33:42 +02:00
|
|
|
cmd += ["-i", subfile]
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2016-06-20 18:33:42 +02:00
|
|
|
arguments += ["-y", tempfile]
|
2016-06-02 01:47:25 +02:00
|
|
|
cmd += arguments
|
2018-03-03 16:11:23 +01:00
|
|
|
returncode, stdout, stderr = run_program(cmd)
|
|
|
|
if returncode != 0:
|
2016-06-02 01:47:25 +02:00
|
|
|
return
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle") and not self.config.get("subtitle"):
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Muxing done, removing the old files.")
|
2018-01-13 21:17:12 +01:00
|
|
|
if self.subfixes and len(self.subfixes) >= 2:
|
2016-06-20 18:33:42 +02:00
|
|
|
for subfix in self.subfixes:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name + subfix)
|
2016-06-20 18:33:42 +02:00
|
|
|
os.remove(subfile)
|
2018-01-30 20:11:37 +01:00
|
|
|
else:
|
|
|
|
os.remove(subfile)
|
|
|
|
else:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Muxing done, removing the old file.")
|
2016-06-20 18:33:42 +02:00
|
|
|
os.remove(orig_filename)
|
2016-06-02 01:47:25 +02:00
|
|
|
os.rename(tempfile, new_name)
|
2016-03-22 22:36:39 +01:00
|
|
|
|
2016-03-26 21:38:31 +01:00
|
|
|
def merge(self):
|
2016-03-30 18:18:43 +02:00
|
|
|
if self.detect is None:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.error("Cant detect ffmpeg or avconv. Cant mux files without it.")
|
2016-03-26 21:38:31 +01:00
|
|
|
return
|
|
|
|
if self.stream.finished is False:
|
|
|
|
return
|
2016-06-20 18:33:42 +02:00
|
|
|
|
2019-09-06 22:49:49 +02:00
|
|
|
orig_filename = formatname(self.stream.output, self.config, self.stream.output_extention)
|
2019-09-14 22:44:44 +02:00
|
|
|
name, ext = os.path.splitext(orig_filename)
|
|
|
|
audio_filename = "{}.audio.ts".format(name)
|
|
|
|
cmd = [self.detect, "-i", orig_filename, "-i", audio_filename]
|
2018-03-03 16:11:23 +01:00
|
|
|
_, stdout, stderr = run_program(cmd, False) # return 1 is good here.
|
2019-09-15 17:31:49 +02:00
|
|
|
streams = _streams(stderr)
|
|
|
|
videotrack, audiotrack = _checktracks(streams)
|
2018-03-03 16:11:23 +01:00
|
|
|
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle"):
|
2019-09-06 22:49:49 +02:00
|
|
|
logging.info("Merge audio, video and subtitle into {}".format(orig_filename))
|
2016-06-20 18:33:42 +02:00
|
|
|
else:
|
2019-08-25 00:33:51 +02:00
|
|
|
logging.info("Merge audio and video into {}".format(orig_filename))
|
2017-01-02 21:09:35 +01:00
|
|
|
|
2019-08-25 00:33:51 +02:00
|
|
|
tempfile = "{}.temp".format(orig_filename)
|
2016-06-20 18:33:42 +02:00
|
|
|
arguments = ["-c:v", "copy", "-c:a", "copy", "-f", "mp4"]
|
2018-03-06 21:48:37 +01:00
|
|
|
if ext == ".ts":
|
2019-09-15 17:31:49 +02:00
|
|
|
if audiotrack and "aac" in _getcodec(streams, audiotrack):
|
2019-09-14 14:17:45 +02:00
|
|
|
arguments += ["-bsf:a", "aac_adtstoasc"]
|
2018-03-06 21:48:37 +01:00
|
|
|
else:
|
2019-08-25 00:33:51 +02:00
|
|
|
audio_filename = "{}.m4a".format(name)
|
2016-03-26 21:38:31 +01:00
|
|
|
cmd = [self.detect, "-i", orig_filename, "-i", audio_filename]
|
2019-09-14 14:17:45 +02:00
|
|
|
if videotrack:
|
2019-09-14 22:44:44 +02:00
|
|
|
arguments += ["-map", "{}".format(videotrack)]
|
2019-09-14 14:17:45 +02:00
|
|
|
if audiotrack:
|
2019-09-14 22:44:44 +02:00
|
|
|
arguments += ["-map", "{}".format(audiotrack)]
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle"):
|
2016-06-20 18:33:42 +02:00
|
|
|
langs = self.sublanguage()
|
2019-09-14 22:44:44 +02:00
|
|
|
tracks = [x for x in [videotrack, audiotrack] if x]
|
|
|
|
for stream_num, language in enumerate(langs, start=len(tracks)):
|
2019-08-25 00:27:31 +02:00
|
|
|
arguments += [
|
|
|
|
"-map",
|
|
|
|
str(stream_num),
|
|
|
|
"-c:s:" + str(stream_num - 2),
|
|
|
|
"mov_text",
|
|
|
|
"-metadata:s:s:" + str(stream_num - 2),
|
|
|
|
"language=" + language,
|
|
|
|
]
|
2018-01-13 21:17:12 +01:00
|
|
|
if self.subfixes and len(self.subfixes) >= 2:
|
2016-06-20 18:33:42 +02:00
|
|
|
for subfix in self.subfixes:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name + subfix)
|
2016-06-20 18:33:42 +02:00
|
|
|
cmd += ["-i", subfile]
|
|
|
|
else:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name)
|
2016-06-20 18:33:42 +02:00
|
|
|
cmd += ["-i", subfile]
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2016-06-20 18:33:42 +02:00
|
|
|
arguments += ["-y", tempfile]
|
2016-03-26 21:38:31 +01:00
|
|
|
cmd += arguments
|
2018-03-03 16:11:23 +01:00
|
|
|
returncode, stdout, stderr = run_program(cmd)
|
2018-03-09 22:19:27 +01:00
|
|
|
if returncode != 0:
|
2016-03-26 21:38:31 +01:00
|
|
|
return
|
2016-07-01 00:30:55 +02:00
|
|
|
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.info("Merging done, removing old files.")
|
2016-06-20 18:33:42 +02:00
|
|
|
os.remove(orig_filename)
|
2016-03-26 21:38:31 +01:00
|
|
|
os.remove(audio_filename)
|
2018-05-13 01:44:00 +02:00
|
|
|
if self.config.get("merge_subtitle") and not self.config.get("subtitle"):
|
2018-01-13 21:17:12 +01:00
|
|
|
if self.subfixes and len(self.subfixes) >= 2:
|
2016-06-20 18:33:42 +02:00
|
|
|
for subfix in self.subfixes:
|
2019-08-25 00:33:51 +02:00
|
|
|
subfile = "{}.srt".format(name + subfix)
|
2016-06-20 18:33:42 +02:00
|
|
|
os.remove(subfile)
|
2018-01-30 20:11:37 +01:00
|
|
|
else:
|
|
|
|
os.remove(subfile)
|
2016-07-01 00:30:55 +02:00
|
|
|
os.rename(tempfile, orig_filename)
|
2018-03-03 16:11:23 +01:00
|
|
|
|
|
|
|
|
2019-09-15 17:31:49 +02:00
|
|
|
def _streams(output):
|
|
|
|
return re.findall(r"Stream \#(\d:\d)([\[\(][^\[]+[\]\)])?([\(\)\w]+)?: (Video|Audio): (.*)", output)
|
|
|
|
|
|
|
|
|
|
|
|
def _getcodec(streams, number):
|
|
|
|
for stream in streams:
|
|
|
|
if stream[0] == number:
|
|
|
|
return stream[4]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _checktracks(streams):
|
|
|
|
videotrack = None
|
|
|
|
audiotrack = None
|
|
|
|
for stream in streams:
|
|
|
|
if stream[3] == "Video":
|
|
|
|
videotrack = stream[0]
|
|
|
|
if stream[3] == "Audio":
|
|
|
|
if stream[4] == "mp3, 0 channels":
|
|
|
|
continue
|
|
|
|
audiotrack = stream[0]
|
|
|
|
|
|
|
|
return videotrack, audiotrack
|