Example #1
0
 def test_formatnameOutput(self):
     config = setup_defaults()
     config.set("output", "/tmp")
     service = Service(config, "http://localhost")
     if platform.system() == "Windows":
         assert formatname(service.output, config) == "/tmp.mp4"
     else:
         assert formatname(service.output, config) == "/tmp/-service.mp4"
Example #2
0
    def remux(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

        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)
            name, ext = os.path.splitext(orig_filename)
            new_name = u"{0}.mp4".format(name)

            cmd = [self.detect, "-i", orig_filename]
            _, stdout, stderr = run_program(cmd, False)  # return 1 is good here.
            videotrack, audiotrack = self._checktracks(stderr)

            if self.config.get("merge_subtitle"):
                log.info(u"Muxing {0} and merging its subtitle into {1}".format(orig_filename, new_name))
            else:
                log.info(u"Muxing {0} into {1}".format(orig_filename, new_name))

            tempfile = u"{0}.temp".format(orig_filename)
            arguments = ["-map", "0:{}".format(videotrack), "-map", "0:{}".format(audiotrack), "-c", "copy", "-f", "mp4"]
            if ext == ".ts":
                arguments += ["-bsf:a", "aac_adtstoasc"]

            if self.config.get("merge_subtitle"):
                langs = self.sublanguage()
                for stream_num, language in enumerate(langs):
                    arguments += ["-map", str(stream_num + 1), "-c:s:" + str(stream_num), "mov_text",
                                  "-metadata:s:s:" + str(stream_num), "language=" + language]
                if self.subfixes and len(self.subfixes) >= 2:
                    for subfix in self.subfixes:
                        subfile = "{0}.srt".format(name + subfix)
                        cmd += ["-i", subfile]
                else:
                    subfile = "{0}.srt".format(name)
                    cmd += ["-i", subfile]

            arguments += ["-y", tempfile]
            cmd += arguments
            returncode, stdout, stderr = run_program(cmd)
            if returncode != 0:
                return

            if self.config.get("merge_subtitle") and not self.config.get("subtitle"):
                log.info("Muxing done, removing the old files.")
                if self.subfixes and len(self.subfixes) >= 2:
                    for subfix in self.subfixes:
                        subfile = "{0}.srt".format(name + subfix)
                        os.remove(subfile)
                else:
                    os.remove(subfile)
            else:
                log.info("Muxing done, removing the old file.")
            os.remove(orig_filename)
            os.rename(tempfile, new_name)
Example #3
0
    def remux(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

        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)
            name, ext = os.path.splitext(orig_filename)
            new_name = u"{0}.mp4".format(name)

            cmd = [self.detect, "-i", orig_filename]
            _, stdout, stderr = run_program(cmd, False)  # return 1 is good here.
            videotrack, audiotrack = self._checktracks(stderr)

            if self.config.get("merge_subtitle"):
                log.info(u"Muxing {0} and merging its subtitle into {1}".format(orig_filename, new_name))
            else:
                log.info(u"Muxing {0} into {1}".format(orig_filename, new_name))

            tempfile = u"{0}.temp".format(orig_filename)
            arguments = ["-map", "0:{}".format(videotrack), "-map", "0:{}".format(audiotrack), "-c", "copy", "-f", "mp4"]
            if ext == ".ts":
                arguments += ["-bsf:a", "aac_adtstoasc"]

            if self.config.get("merge_subtitle"):
                langs = self.sublanguage()
                for stream_num, language in enumerate(langs):
                    arguments += ["-map", str(stream_num + 1), "-c:s:" + str(stream_num), "mov_text",
                                  "-metadata:s:s:" + str(stream_num), "language=" + language]
                if self.subfixes and len(self.subfixes) >= 2:
                    for subfix in self.subfixes:
                        subfile = "{0}.srt".format(name + subfix)
                        cmd += ["-i", subfile]
                else:
                    subfile = "{0}.srt".format(name)
                    cmd += ["-i", subfile]

            arguments += ["-y", tempfile]
            cmd += arguments
            returncode, stdout, stderr = run_program(cmd)
            if returncode != 0:
                return

            if self.config.get("merge_subtitle") and not self.config.get("subtitle"):
                log.info("Muxing done, removing the old files.")
                if self.subfixes and len(self.subfixes) >= 2:
                    for subfix in self.subfixes:
                        subfile = "{0}.srt".format(name + subfix)
                        os.remove(subfile)
                else:
                    os.remove(subfile)
            else:
                log.info("Muxing done, removing the old file.")
            os.remove(orig_filename)
            os.rename(tempfile, new_name)
Example #4
0
 def test_formatnameTvshowSubfolderMovie(self):
     config = setup_defaults()
     config.set("subfolder", True)
     service = Service(config, "http://localhost")
     service.output["tvshow"] = False
     service.output["title"] = "kalle"
     service.output["season"] = 2
     service.output["episode"] = 2
     if platform.system() == "Windows":
         assert formatname(service.output, config) == r"movies\kalle.s02e02-service.mp4"
     else:
         assert formatname(service.output, config) == "movies/kalle.s02e02-service.mp4"
Example #5
0
 def test_formatnameTvshowPath(self):
     config = setup_defaults()
     if platform.system() == "Windows":
         config.set("path", "c:")
     else:
         config.set("path", "/tmp")
     service = Service(config, "http://localhost")
     service.output["title"] = "kalle"
     service.output["season"] = 2
     service.output["episode"] = 2
     if platform.system() == "Windows":
         assert formatname(service.output, config) == r"c:kalle.s02e02-service.mp4"
     else:
         assert formatname(service.output, config) == "/tmp/kalle.s02e02-service.mp4"
Example #6
0
def get_all_episodes(stream, url):
    name = os.path.dirname(formatname({"basedir": True}, stream.config))

    if name and os.path.isfile(name):
        logging.error("Output must be a directory if used with --all-episodes")
        sys.exit(2)
    elif name and not os.path.exists(name):
        try:
            os.makedirs(name)
        except OSError as e:
            logging.error("%s: %s", e.strerror, e.filename)
            return

    episodes = stream.find_all_episodes(stream.config)
    if episodes is None:
        return
    for idx, o in enumerate(episodes):
        if o == url:
            substream = stream
        else:
            substream = service_handler(sites, copy.copy(stream.config), o)

        logging.info("Episode %d of %d", idx + 1, len(episodes))
        logging.info("Url: %s", o)

        # get_one_media overwrites options.output...
        get_one_media(substream)
Example #7
0
def get_all_episodes(stream, url, options):
    name = os.path.dirname(formatname({"basedir": True}, stream.config))

    if name and os.path.isfile(name):
        logging.error("Output must be a directory if used with --all-episodes")
        sys.exit(2)
    elif name and not os.path.exists(name):
        try:
            os.makedirs(name)
        except OSError as e:
            logging.error("%s: %s", e.strerror, e.filename)
            return

    episodes = stream.find_all_episodes(stream.config)
    if episodes is None:
        return
    for idx, o in enumerate(episodes):
        if o == url:
            substream = stream
        else:
            substream = service_handler(sites, copy.copy(stream.config), o)

        logging.info("Episode %d of %d", idx + 1, len(episodes))
        logging.info("Url: %s", o)

        if not (options.get("get_url")
                and options.get("get_only_episode_url")):
            # get_one_media overwrites options.output...
            get_one_media(substream)
Example #8
0
def write_nfo_tvshow(output, config):
    # Config for tvshow nfo file
    if not output["title_nice"]:
        # If we don't even have the title, skip the NFO
        return
    root = ET.Element("tvshow")
    ET.SubElement(root, "title").text = output["title_nice"]
    if output["episodedescription"]:
        ET.SubElement(root, "plot").text = output["episodedescription"]
    if config.get("thumbnail"):
        # Set the thumbnail relative path to downloaded thumbnail
        ET.SubElement(root, "thumb").text = "{}.tvshow.tbn".format(output["title"])
    elif output["episodethumbnailurl"]:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["episodethumbnailurl"]

    cconfig = Options()
    cconfig.set("output", config.get("output"))
    cconfig.set("path", config.get("path"))
    cconfig.set("subfolder", config.get("subfolder"))
    cconfig.set("filename", "tvshow.{ext}")
    filename = formatname(output.copy(), cconfig, extension="nfo")
    logging.info("NFO show: %s", filename)

    tree = ET.ElementTree(root)
    tree.write(filename, encoding="UTF-8", xml_declaration=True)
Example #9
0
    def download(self):
        """ Get the stream from RTMP """
        self.output_extention = "flv"
        args = []
        if self.config.get("live"):
            args.append("-v")

        if self.config.get("resume"):
            args.append("-e")

        file_d = output(self.output, self.config, "flv", False)
        if file_d is None:
            return
        args += ["-o", formatname(self.output, self.config, "flv")]
        if self.config.get("silent"):
            args.append("-q")
        if self.kwargs.get("other"):
            args += shlex.split(self.kwargs.pop("other"))

        if self.config.get("verbose"):
            args.append("-V")

        command = ["rtmpdump", "-r", self.url] + args
        log.debug("Running: {0}".format(" ".join(command)))
        try:
            subprocess.call(command)
        except OSError as e:
            log.error("Could not execute rtmpdump: {0}".format(e.strerror))
            return
        self.finished = True
Example #10
0
def write_nfo_episode(output, config):
    if not output["title_nice"]:
        # If we don't even have the title, skip the NFO
        return
    root = ET.Element("episodedetails")
    ET.SubElement(root, "showtitle").text = output["title_nice"]
    if output["episodename"]:
        ET.SubElement(root, "title").text = output["episodename"]
    if output["season"]:
        ET.SubElement(root, "season").text = str(output["season"])
    if output["episode"]:
        ET.SubElement(root, "episode").text = str(output["episode"])
    ET.SubElement(root, "plot").text = output["episodedescription"]
    if output["publishing_datetime"] is not None:
        ET.SubElement(root, "aired").text = datetime.fromtimestamp(
            output["publishing_datetime"]).isoformat()
    if not config.get("thumbnail") and output["showthumbnailurl"]:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["episodethumbnailurl"]

    filename = formatname(output.copy(), config, extension="nfo")
    logging.info("NFO episode: %s", filename)

    tree = ET.ElementTree(root)
    tree.write(filename, encoding="UTF-8", xml_declaration=True)
Example #11
0
 def test_formatnameTvshow(self):
     config = setup_defaults()
     service = Service(config, "http://localhost")
     service.output["tvshow"] = True
     service.output["title"] = "kalle"
     service.output["season"] = 2
     service.output["episode"] = 2
     assert formatname(service.output, config) == "kalle.s02e02-service.mp4"
Example #12
0
def download_thumbnails(output, config, urls):
    for show, url in urls:
        data = Session().get(url).content

        if show:
            # Config for downloading show thumbnail
            cconfig = Options()
            cconfig.set("output", config.get("output"))
            cconfig.set("path", config.get("path"))
            cconfig.set("subfolder", config.get("subfolder"))
            cconfig.set("filename", "{title}.tvshow.{ext}")
        else:
            cconfig = config

        filename = formatname(output.copy(), cconfig, extension="tbn")
        logging.info("Thumbnail: %s", filename)

        fd = open(filename, "wb")
        fd.write(data)
        fd.close()
Example #13
0
def download_thumbnails(output, config, urls):
    for show, url in urls:
        data = Session().get(url).content

        if show:
            # Config for downloading show thumbnail
            cconfig = Options()
            cconfig.set("output", config.get("output"))
            cconfig.set("path", config.get("path"))
            cconfig.set("subfolder", config.get("subfolder"))
            cconfig.set("filename", "{title}.tvshow.{ext}")
        else:
            cconfig = config

        filename = formatname(output.copy(), cconfig, extension="tbn")
        logging.info("Thumbnail: %s", filename)

        fd = open(filename, "wb")
        fd.write(data)
        fd.close()
Example #14
0
def write_nfo_episode(output, config):
    if not output["title_nice"]:
        # If we don't even have the title, skip the NFO
        return
    root = ET.Element("episodedetails")
    ET.SubElement(root, "title").text = output["title_nice"]
    if output["episodename"]:
        ET.SubElement(root, "showtitle").text = output["episodename"]
    if output["season"]:
        ET.SubElement(root, "season").text = str(output["season"])
    if output["episode"]:
        ET.SubElement(root, "episode").text = str(output["episode"])
    ET.SubElement(root, "plot").text = output["showdescription"]
    if output["publishing_datetime"] is not None:
        ET.SubElement(root, "aired").text = datetime.fromtimestamp(output["publishing_datetime"]).isoformat()
    if not config.get("thumbnail") and output["showthumbnailurl"]:
        # Set the thumbnail path to download link if not thumbnail downloaded
        ET.SubElement(root, "thumb").text = output["showthumbnailurl"]

    filename = formatname(output.copy(), config, extension="nfo")
    logging.info("NFO episode: %s", filename)

    tree = ET.ElementTree(root)
    tree.write(filename, encoding="UTF-8", xml_declaration=True)
Example #15
0
def get_one_media(stream):
    # Make an automagic filename
    if not filename(stream):
        return

    if stream.config.get("merge_subtitle"):
        if not which("ffmpeg"):
            logging.error(
                "--merge-subtitle needs ffmpeg. Please install ffmpeg.")
            logging.info("https://ffmpeg.org/download.html")
            sys.exit(2)

    videos = []
    subs = []
    subfixes = []
    error = []
    streams = stream.get()
    try:
        for i in streams:
            if isinstance(i, Exception):
                error.append(i)
            elif not exclude(stream.config, formatname(i.output,
                                                       stream.config)):
                if isinstance(i, VideoRetriever):
                    if stream.config.get("preferred"):
                        if stream.config.get("preferred").lower() == i.name:
                            videos.append(i)
                    else:
                        videos.append(i)
                if isinstance(i, subtitle):
                    subs.append(i)
    except Exception:
        if stream.config.get("verbose"):
            raise
        else:
            logging.error("svtplay-dl crashed")
            logging.error(
                "Run again and add --verbose as an argument, to get more information"
            )
            logging.error(
                "If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues"
            )
            logging.error(
                "Include the URL used, the stack trace and the output of svtplay-dl --version in the issue"
            )
        return

    try:
        after_date = datetime.strptime(stream.config.get("after_date"),
                                       "%Y-%m-%d")
    except (ValueError, TypeError, KeyError,
            AttributeError):  # gotta catch em all..
        after_date = None
    try:
        pub_date = datetime.fromtimestamp(stream.output["publishing_datetime"])
    except (ValueError, TypeError, KeyError):
        pub_date = None
    if after_date is not None and pub_date is not None and pub_date.date(
    ) < after_date.date():
        logging.info(
            "Video {}S{}E{} skipped since published {} before {}. ".format(
                stream.output["title"], stream.output["season"],
                stream.output["episode"], pub_date.date(), after_date.date()))
        return

    if stream.config.get("require_subtitle") and not subs:
        logging.info("No subtitles available")
        return

    if stream.config.get("subtitle") and stream.config.get("get_url"):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    print(sub.url)
            else:
                print(subs[0].url)
        if stream.config.get("force_subtitle"):
            return

    def options_subs_dl(subfixes):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    sub.download()
                    if stream.config.get("merge_subtitle"):
                        if sub.subfix:
                            subfixes += [sub.subfix]
                        else:
                            stream.config.set("get_all_subtitles", False)
            else:
                subs[0].download()
        elif stream.config.get("merge_subtitle"):
            stream.config.set("merge_subtitle", False)

    if stream.config.get("subtitle") and not stream.config.get("get_url"):
        options_subs_dl(subfixes)
        if stream.config.get("force_subtitle"):
            if not subs:
                logging.info("No subtitles available")
            return

    if stream.config.get(
            "merge_subtitle") and not stream.config.get("subtitle"):
        options_subs_dl(subfixes)
    if not videos:
        errormsg = None
        for exc in error:
            if errormsg:
                errormsg = "{}. {}".format(errormsg, str(exc))
            else:
                errormsg = str(exc)
        if errormsg:
            logging.error("No videos found. {}".format(errormsg))
        else:
            logging.error("No videos found.")
    else:
        if stream.config.get("list_quality"):
            list_quality(videos)
            return
        if stream.config.get("nfo"):
            # Create NFO files
            write_nfo_episode(stream.output, stream.config)
            write_nfo_tvshow(stream.output, stream.config)
            if stream.config.get("force_nfo"):
                return
        try:
            fstream = select_quality(stream.config, videos)
            if fstream.config.get("get_url"):
                print(fstream.url)
                return
            logging.info("Selected to download %s, bitrate: %s", fstream.name,
                         fstream.bitrate)
            fstream.download()
        except UIException as e:
            if fstream.config.get("verbose"):
                raise e
            logging.error(e)
            sys.exit(2)

        if fstream.config.get("thumbnail") and hasattr(stream,
                                                       "get_thumbnail"):
            stream.get_thumbnail(stream.config)

        post = postprocess(fstream, fstream.config, subfixes)
        if fstream.audio and post.detect:
            post.merge()
        if fstream.audio and not post.detect and fstream.finished:
            logging.warning(
                "Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds"
            )
        if fstream.name == "hls" or fstream.config.get("remux"):
            post.remux()
        if fstream.config.get("silent_semi") and fstream.finished:
            logging.log(
                25, "Download of %s was completed" % fstream.options.output)
Example #16
0
def get_one_media(stream):
    # Make an automagic filename
    if not filename(stream):
        return

    if stream.config.get("merge_subtitle"):
        if not which('ffmpeg'):
            log.error("--merge-subtitle needs ffmpeg. Please install ffmpeg.")
            log.info("https://ffmpeg.org/download.html")
            sys.exit(2)

    videos = []
    subs = []
    subfixes = []
    error = []
    streams = stream.get()
    try:
        for i in streams:
            if isinstance(i, Exception):
                error.append(i)
            elif not exclude(stream.config, formatname(i.output,
                                                       stream.config)):
                if isinstance(i, VideoRetriever):
                    if stream.config.get("preferred"):
                        if stream.config.get("preferred").lower() == i.name:
                            videos.append(i)
                    else:
                        videos.append(i)
                if isinstance(i, subtitle):
                    subs.append(i)
    except Exception as e:
        if stream.config.get("verbose"):
            raise
        else:
            logging.error("svtplay-dl crashed")
            logging.error(
                "Run again and add --verbose as an argument, to get more information"
            )
            logging.error(
                "If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues"
            )
            logging.error(
                "Include the URL used, the stack trace and the output of svtplay-dl --version in the issue"
            )
        return

    if stream.config.get("require_subtitle") and not subs:
        logging.info("No subtitles available")
        return

    if stream.config.get("subtitle") and stream.config.get("get_url"):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    print(sub.url)
            else:
                print(subs[0].url)
        if stream.config.get("force_subtitle"):
            return

    def options_subs_dl(subfixes):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    sub.download()
                    if stream.config.get("merge_subtitle"):
                        if sub.subfix:
                            subfixes += [sub.subfix]
                        else:
                            stream.config.set("get_all_subtitles", False)
            else:
                subs[0].download()
        elif stream.config.get("merge_subtitle"):
            stream.config.set("merge_subtitle", False)

    if stream.config.get("subtitle") and not stream.config.get("get_url"):
        options_subs_dl(subfixes)
        if stream.config.get("force_subtitle"):
            return

    if stream.config.get(
            "merge_subtitle") and not stream.config.get("subtitle"):
        options_subs_dl(subfixes)

    if not videos:
        errormsg = None
        for exc in error:
            if errormsg:
                errormsg = "{}. {}".format(errormsg, str(exc))
            else:
                errormsg = str(exc)
        logging.error("No videos found. {}".format(errormsg))
    else:
        if stream.config.get("list_quality"):
            list_quality(videos)
            return
        try:
            stream = select_quality(stream.config, videos)
            if stream.config.get("get_url"):
                print(stream.url)
                return
            logging.info("Selected to download %s, bitrate: %s", stream.name,
                         stream.bitrate)
            stream.download()
        except UIException as e:
            if stream.config.get("verbose"):
                raise e
            log.error(e)
            sys.exit(2)

        if stream.config.get("thumbnail") and hasattr(stream, "get_thumbnail"):
            stream.get_thumbnail(stream.config)
        post = postprocess(stream, stream.config, subfixes)
        if stream.audio and post.detect:
            post.merge()
        if stream.audio and not post.detect and stream.finished:
            logging.warning(
                "Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds"
            )
        if stream.name == "hls" or stream.config.get("remux"):
            post.remux()
        if stream.config.get("silent_semi") and stream.finished:
            logging.log(25,
                        "Download of %s was completed" % stream.options.output)
Example #17
0
def get_one_media(stream):
    # Make an automagic filename
    if not filename(stream):
        return

    if stream.config.get("merge_subtitle"):
        if not which('ffmpeg'):
            logging.error("--merge-subtitle needs ffmpeg. Please install ffmpeg.")
            logging.info("https://ffmpeg.org/download.html")
            sys.exit(2)

    videos = []
    subs = []
    subfixes = []
    error = []
    streams = stream.get()
    try:
        for i in streams:
            if isinstance(i, Exception):
                error.append(i)
            elif not exclude(stream.config, formatname(i.output, stream.config)):
                if isinstance(i, VideoRetriever):
                    if stream.config.get("preferred"):
                        if stream.config.get("preferred").lower() == i.name:
                            videos.append(i)
                    else:
                        videos.append(i)
                if isinstance(i, subtitle):
                    subs.append(i)
    except Exception:
        if stream.config.get("verbose"):
            raise
        else:
            logging.error("svtplay-dl crashed")
            logging.error("Run again and add --verbose as an argument, to get more information")
            logging.error("If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues")
            logging.error("Include the URL used, the stack trace and the output of svtplay-dl --version in the issue")
        return

    try:
        after_date = datetime.strptime(stream.config.get("after_date"), "%Y-%m-%d")
    except (ValueError, TypeError, KeyError, AttributeError):  # gotta catch em all..
        after_date = None
    try:
        pub_date = datetime.fromtimestamp(stream.output["publishing_datetime"])
    except (ValueError, TypeError, KeyError):
        pub_date = None
    if after_date is not None and pub_date is not None and pub_date.date() < after_date.date():
        logging.info("Video {}S{}E{} skipped since published {} before {}. ".format(
            stream.output["title"],
            stream.output["season"],
            stream.output["episode"],
            pub_date.date(),
            after_date.date()))
        return

    if stream.config.get("require_subtitle") and not subs:
        logging.info("No subtitles available")
        return

    if stream.config.get("subtitle") and stream.config.get("get_url"):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    print(sub.url)
            else:
                print(subs[0].url)
        if stream.config.get("force_subtitle"):
            return

    def options_subs_dl(subfixes):
        if subs:
            if stream.config.get("get_all_subtitles"):
                for sub in subs:
                    sub.download()
                    if stream.config.get("merge_subtitle"):
                        if sub.subfix:
                            subfixes += [sub.subfix]
                        else:
                            stream.config.set("get_all_subtitles", False)
            else:
                subs[0].download()
        elif stream.config.get("merge_subtitle"):
            stream.config.set("merge_subtitle", False)

    if stream.config.get("subtitle") and not stream.config.get("get_url"):
        options_subs_dl(subfixes)
        if stream.config.get("force_subtitle"):
            return

    if stream.config.get("merge_subtitle") and not stream.config.get("subtitle"):
        options_subs_dl(subfixes)
    if not videos:
        errormsg = None
        for exc in error:
            if errormsg:
                errormsg = "{}. {}".format(errormsg, str(exc))
            else:
                errormsg = str(exc)
        if errormsg:
            logging.error("No videos found. {}".format(errormsg))
        else:
            logging.error("No videos found.")
    else:
        if stream.config.get("list_quality"):
            list_quality(videos)
            return
        if stream.config.get("nfo"):
            # Create NFO files
            write_nfo_episode(stream.output, stream.config)
            write_nfo_tvshow(stream.output, stream.config)
            if stream.config.get("force_nfo"):
                return
        try:
            fstream = select_quality(stream.config, videos)
            if fstream.config.get("get_url"):
                print(fstream.url)
                return
            logging.info("Selected to download %s, bitrate: %s", fstream.name, fstream.bitrate)
            fstream.download()
        except UIException as e:
            if fstream.config.get("verbose"):
                raise e
            logging.error(e)
            sys.exit(2)

        if fstream.config.get("thumbnail") and hasattr(stream, "get_thumbnail"):
            stream.get_thumbnail(stream.config)

        post = postprocess(fstream, fstream.config, subfixes)
        if fstream.audio and post.detect:
            post.merge()
        if fstream.audio and not post.detect and fstream.finished:
            logging.warning("Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds")
        if fstream.name == "hls" or fstream.config.get("remux"):
            post.remux()
        if fstream.config.get("silent_semi") and fstream.finished:
            logging.log(25, "Download of %s was completed" % fstream.options.output)
Example #18
0
 def test_formatnameEmpty(self):
     config = setup_defaults()
     service = Service(config, "http://localhost")
     assert formatname(service.output, config) == "-service.mp4"
Example #19
0
def _sublanguage(stream, config, subfixes):
    # 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):
            lines = block.strip("-").split("\n")
            txt = "\r\n".join(lines[2:])
            return txt

        if platform.system() == "Windows":
            fd = open(self, encoding="utf8")
        else:
            fd = open(self)
        return list(
            map(parse_block,
                fd.read().strip().replace("\r", "").split("\n\n")))

    def query(self):
        _ = parse(self)
        random_sentences = " ".join(sample(
            _,
            len(_) if len(_) < 8 else 8)).replace("\r\n", "")
        url = "https://whatlanguage.herokuapp.com"
        payload = {"query": random_sentences}
        # Note: requests handles json from version 2.4.2 and onwards so i use json.dumps for now.
        headers = {"content-type": "application/json"}
        try:
            # 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)
            if r.status_code == codes.ok:
                try:
                    response = r.json()
                    return response["language"]
                except TypeError:
                    return "und"
            else:
                logging.error(
                    "Server error appeared. Setting language as undetermined.")
                return "und"
        except Timeout:
            logging.error(
                "30 seconds server timeout reached. Setting language as undetermined."
            )
            return "und"

    langs = []
    exceptions = {"lulesamiska": "smj", "meankieli": "fit", "jiddisch": "yid"}
    if subfixes and len(subfixes) >= 2:
        logging.info("Determining the languages of the subtitles.")
    else:
        logging.info("Determining the language of the subtitle.")
    if config.get("get_all_subtitles"):
        for subfix in subfixes:
            if [
                    exceptions[key] for key in exceptions.keys()
                    if match(key, subfix.strip("-"))
            ]:
                if "oversattning" in subfix.strip("-"):
                    subfix = subfix.strip("-").split(".")[0]
                else:
                    subfix = subfix.strip("-")
                langs += [exceptions[subfix]]
                continue
            subfile = "{}.srt".format(
                os.path.splitext(
                    formatname(stream.output, config, stream.output_extention))
                [0] + subfix)
            langs += [query(subfile)]
    else:
        subfile = "{}.srt".format(
            os.path.splitext(
                formatname(stream.output, config, stream.output_extention))[0])
        langs += [query(subfile)]
    if len(langs) >= 2:
        logging.info("Language codes: " + ", ".join(langs))
    else:
        logging.info("Language code: " + langs[0])
    return langs
Example #20
0
    def merge(self):
        if self.detect is None:
            logging.error(
                "Cant detect ffmpeg or avconv. Cant mux files without it.")
            return
        if self.stream.finished is False:
            return

        orig_filename = formatname(self.stream.output, self.config,
                                   self.stream.output_extention)
        name, ext = os.path.splitext(orig_filename)
        audio_filename = "{}.audio.ts".format(name)
        cmd = [self.detect, "-i", orig_filename, "-i", audio_filename]
        _, stdout, stderr = run_program(cmd, False)  # return 1 is good here.
        streams = _streams(stderr)
        videotrack, audiotrack = _checktracks(streams)

        if self.config.get("merge_subtitle"):
            logging.info("Merge audio, video and subtitle into {}".format(
                orig_filename))
        else:
            logging.info("Merge audio and video into {}".format(orig_filename))

        tempfile = "{}.temp".format(orig_filename)
        arguments = ["-c:v", "copy", "-c:a", "copy", "-f", "mp4"]
        if ext == ".ts":
            if audiotrack and "aac" in _getcodec(streams, audiotrack):
                arguments += ["-bsf:a", "aac_adtstoasc"]
        else:
            audio_filename = "{}.m4a".format(name)
        cmd = [self.detect, "-i", orig_filename, "-i", audio_filename]
        if videotrack:
            arguments += ["-map", "{}".format(videotrack)]
        if audiotrack:
            arguments += ["-map", "{}".format(audiotrack)]
        if self.config.get("merge_subtitle"):
            langs = _sublanguage(self.stream, self.config, self.subfixes)
            tracks = [x for x in [videotrack, audiotrack] if x]
            for stream_num, language in enumerate(langs, start=len(tracks)):
                arguments += [
                    "-map",
                    str(stream_num),
                    "-c:s:" + str(stream_num - 2),
                    "mov_text",
                    "-metadata:s:s:" + str(stream_num - 2),
                    "language=" + language,
                ]
            if self.subfixes and len(self.subfixes) >= 2:
                for subfix in self.subfixes:
                    subfile = "{}.srt".format(name + subfix)
                    cmd += ["-i", subfile]
            else:
                subfile = "{}.srt".format(name)
                cmd += ["-i", subfile]

        arguments += ["-y", tempfile]
        cmd += arguments
        returncode, stdout, stderr = run_program(cmd)
        if returncode != 0:
            return

        logging.info("Merging done, removing old files.")
        os.remove(orig_filename)
        os.remove(audio_filename)
        if self.config.get(
                "merge_subtitle") and not self.config.get("subtitle"):
            if self.subfixes and len(self.subfixes) >= 2:
                for subfix in self.subfixes:
                    subfile = "{}.srt".format(name + subfix)
                    os.remove(subfile)
            else:
                os.remove(subfile)
        os.rename(tempfile, orig_filename)
Example #21
0
 def test_formatnameBasedir(self):
     config = setup_defaults()
     service = Service(config, "http://localhost")
     service.output["basedir"] = True
     assert formatname(service.output, config) == "-service.mp4"
Example #22
0
    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):
                lines = block.strip('-').split('\n')
                txt = '\r\n'.join(lines[2:])
                return txt

            if platform.system() == "Windows":
                fd = open(self, encoding="utf8")
            else:
                fd = open(self)
            return list(map(parse_block,
                            fd.read().strip().replace('\r', '').split('\n\n')))

        def query(self):
            _ = parse(self)
            random_sentences = ' '.join(sample(_, len(_) if len(_) < 8 else 8)).replace('\r\n', '')
            url = 'https://whatlanguage.herokuapp.com'
            payload = {"query": random_sentences}
            # Note: requests handles json from version 2.4.2 and onwards so i use json.dumps for now.
            headers = {'content-type': 'application/json'}
            try:
                # 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)
                if r.status_code == codes.ok:
                    try:
                        response = r.json()
                        return response['language']
                    except TypeError:
                        return 'und'
                else:
                    log.error("Server error appeared. Setting language as undetermined.")
                    return 'und'
            except Timeout:
                log.error("30 seconds server timeout reached. Setting language as undetermined.")
                return 'und'

        langs = []
        exceptions = {
            'lulesamiska': 'smj',
            'meankieli': 'fit',
            'jiddisch': 'yid'
        }
        if self.subfixes and len(self.subfixes) >= 2:
            log.info("Determining the languages of the subtitles.")
        else:
            log.info("Determining the language of the subtitle.")
        if self.config.get("get_all_subtitles"):
            for subfix in self.subfixes:
                if [exceptions[key] for key in exceptions.keys() if match(key, subfix.strip('-'))]:
                    if 'oversattning' in subfix.strip('-'):
                        subfix = subfix.strip('-').split('.')[0]
                    else:
                        subfix = subfix.strip('-')
                    langs += [exceptions[subfix]]
                    continue
                subfile = "{0}.srt".format(os.path.splitext(formatname(self.stream.output, self.config,
                                                                       self.stream.output_extention))[0] + subfix)
                langs += [query(subfile)]
        else:
            subfile = "{0}.srt".format(os.path.splitext(formatname(self.stream.output, self.config,
                                                                   self.stream.output_extention))[0])
            langs += [query(subfile)]
        if len(langs) >= 2:
            log.info("Language codes: " + ', '.join(langs))
        else:
            log.info("Language code: " + langs[0])
        return langs