1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-24 04:05:39 +01:00

postprocess: basic remux files to .mp4 with ffmpeg/avconv

fixes #121
fixes #36
This commit is contained in:
Johan Andersson 2016-03-22 22:36:39 +01:00
parent 2d43702828
commit 4218dd5a5b
2 changed files with 51 additions and 0 deletions

View File

@ -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

View File

@ -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)