From 4218dd5a5bde58b1547c2d780a2349911efc8302 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Tue, 22 Mar 2016 22:36:39 +0100 Subject: [PATCH] postprocess: basic remux files to .mp4 with ffmpeg/avconv fixes #121 fixes #36 --- lib/svtplay_dl/__init__.py | 9 ++++++ lib/svtplay_dl/postprocess/__init__.py | 42 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 lib/svtplay_dl/postprocess/__init__.py diff --git a/lib/svtplay_dl/__init__.py b/lib/svtplay_dl/__init__.py index da02f48..58c292c 100644 --- a/lib/svtplay_dl/__init__.py +++ b/lib/svtplay_dl/__init__.py @@ -14,6 +14,7 @@ from svtplay_dl.service import service_handler, Generic from svtplay_dl.fetcher import VideoRetriever from svtplay_dl.subtitle import subtitle from svtplay_dl.output import filename +from svtplay_dl.postprocess import postprocess from svtplay_dl.service.aftonbladet import Aftonbladet from svtplay_dl.service.bambuser import Bambuser @@ -132,6 +133,7 @@ class Options(object): self.ssl_verify = True self.http_headers = None self.stream_prio = None + self.remux = False def get_media(url, options): @@ -246,6 +248,10 @@ def get_one_media(stream, options): else: log.warning("Can not get thumbnail when fetching to stdout") + if options.remux: + post = postprocess(stream) + post.mux() + def setup_log(silent, verbose=False): fmt = logging.Formatter('%(levelname)s: %(message)s') @@ -327,6 +333,8 @@ def main(): help="A header to add to each HTTP request.") parser.add_option("--stream-priority", dest="stream_prio", default=None, metavar="hls,hds,http,rtmp", help="If two streams have the same quality, choose the one you prefer") + parser.add_option("--remux", dest="remux", default=False, action="store_true", + help="Remux from one container to mp4 using ffmpeg or avconv") (options, args) = parser.parse_args() if not args: parser.print_help() @@ -378,4 +386,5 @@ def mergeParserOption(options, parser): options.ssl_verify = parser.ssl_verify options.http_headers = parser.http_headers options.stream_prio = parser.stream_prio + options.remux = parser.remux return options diff --git a/lib/svtplay_dl/postprocess/__init__.py b/lib/svtplay_dl/postprocess/__init__.py new file mode 100644 index 0000000..0ade17c --- /dev/null +++ b/lib/svtplay_dl/postprocess/__init__.py @@ -0,0 +1,42 @@ +import subprocess +import os + + +from svtplay_dl.log import log +from svtplay_dl.utils import which + + +class postprocess(object): + def __init__(self, stream): + self.stream = stream + self.detect = which("avconv") + + def mux(self): + if self.detect is None: + log.error("Cant detect ffmpeg or avconv. cant mux files without it") + return + if self.stream.finished is False: + return + orig_filename = self.stream.options.output + new_name = "{0}.mp4".format(os.path.splitext(self.stream.options.output)[0]) + + log.info("Muxing {0} into {1}".format(orig_filename, new_name)) + tempfile = "{0}.temp".format(self.stream.options.output) + name, ext = os.path.splitext(orig_filename) + arguments = ["-c", "copy", "-f", "mp4"] + if ext == "ts": + arguments += ["-bsf:a", "aac_adtstoasc"] + arguments += ["-y", tempfile] + cmd = ["avconv", "-i", orig_filename] + cmd += arguments + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + stdout, stderr = p.communicate() + if p.returncode != 0: + stderr = stderr.decode('utf-8', 'replace') + msg = stderr.strip().split('\n')[-1] + log.error("Muxing went wrong: {0}".format(msg)) + return + log.info("Muxing done. removing the old file.") + os.remove(self.stream.options.output) + os.rename(tempfile, new_name) +