1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-27 21:54:17 +01:00
svtplay-dl/lib/svtplay_dl/utils/getmedia.py

200 lines
6.8 KiB
Python
Raw Normal View History

2018-01-14 00:49:26 +01:00
import os
import sys
import copy
import logging
from shutil import which
2018-01-14 00:49:26 +01:00
from svtplay_dl.log import log
from svtplay_dl.service import service_handler, Generic
2018-01-30 21:45:54 +01:00
from svtplay_dl.service.services import sites, Raw
2018-01-14 00:49:26 +01:00
from svtplay_dl.fetcher import VideoRetriever
from svtplay_dl.subtitle import subtitle
from svtplay_dl.utils.output import filename, formatname
2018-01-14 00:49:26 +01:00
from svtplay_dl.postprocess import postprocess
from svtplay_dl.utils.stream import select_quality, list_quality
from svtplay_dl.utils.text import exclude
2018-01-14 00:49:26 +01:00
from svtplay_dl.error import UIException
def get_multiple_media(urls, options):
if options.output and os.path.isfile(options.output):
log.error("Output must be a directory if used with multiple URLs")
sys.exit(2)
elif options.output and not os.path.exists(options.output):
try:
os.makedirs(options.output)
except OSError as e:
log.error("%s: %s", e.strerror, e.filename)
return
for url in urls:
get_media(url, copy.copy(options))
def get_media(url, options, version="Unknown"):
if "http" not in url[:4]:
url = "http://%s" % url
2018-05-13 13:06:45 +02:00
if options.get("verbose"):
2018-01-14 00:49:26 +01:00
log.debug("version: {0}".format(version))
2018-05-13 02:35:14 +02:00
2018-01-14 00:49:26 +01:00
stream = service_handler(sites, options, url)
if not stream:
generic = Generic(options, url)
url, stream = generic.get(sites)
if not stream:
if url.find(".f4m") > 0 or url.find(".m3u8") > 0:
stream = Raw(options, url)
if not stream:
log.error("That site is not supported. Make a ticket or send a message")
sys.exit(2)
2018-05-13 13:06:45 +02:00
if options.get("all_episodes"):
2018-05-20 21:09:49 +02:00
get_all_episodes(stream, url)
2018-01-14 00:49:26 +01:00
else:
2018-05-20 21:09:49 +02:00
get_one_media(stream)
2018-01-14 00:49:26 +01:00
2018-05-20 21:09:49 +02:00
def get_all_episodes(stream, url):
name = os.path.dirname(formatname(dict(), stream.config))
if name and os.path.isfile(name):
2018-01-14 00:49:26 +01:00
log.error("Output must be a directory if used with --all-episodes")
sys.exit(2)
2018-05-20 21:09:49 +02:00
elif name and not os.path.exists(name):
2018-01-14 00:49:26 +01:00
try:
2018-05-20 21:09:49 +02:00
os.makedirs(name)
2018-01-14 00:49:26 +01:00
except OSError as e:
log.error("%s: %s", e.strerror, e.filename)
return
2018-05-20 21:09:49 +02:00
episodes = stream.find_all_episodes(stream.config)
2018-01-14 00:49:26 +01:00
if episodes is None:
return
for idx, o in enumerate(episodes):
if o == url:
substream = stream
else:
2018-05-20 21:09:49 +02:00
substream = service_handler(sites, copy.copy(stream.config), o)
2018-01-14 00:49:26 +01:00
log.info("Episode %d of %d", idx + 1, len(episodes))
2018-01-30 21:46:08 +01:00
log.info("Url: %s", o)
2018-01-14 00:49:26 +01:00
# get_one_media overwrites options.output...
2018-05-20 21:09:49 +02:00
get_one_media(substream)
2018-01-14 00:49:26 +01:00
2018-05-20 21:09:49 +02:00
def get_one_media(stream):
2018-01-14 00:49:26 +01:00
# Make an automagic filename
if not filename(stream):
return
2018-05-13 02:35:14 +02:00
2018-05-20 21:09:49 +02:00
if stream.config.get("merge_subtitle"):
2018-01-14 00:49:26 +01:00
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):
2018-05-20 21:09:49 +02:00
if stream.config.get("preferred"):
2018-05-25 22:47:26 +02:00
if stream.config.get("preferred").lower() == i.name:
videos.append(i)
else:
2018-01-14 00:49:26 +01:00
videos.append(i)
if isinstance(i, subtitle):
subs.append(i)
2018-01-14 00:49:26 +01:00
except Exception as e:
2018-05-20 21:09:49 +02:00
if stream.config.get("verbose"):
2018-01-14 00:49:26 +01:00
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
2018-01-14 00:49:26 +01:00
2018-05-20 21:09:49 +02:00
if stream.config.get("require_subtitle") and not subs:
logging.info("No subtitles available")
2018-01-14 00:49:26 +01:00
return
2018-05-20 21:09:49 +02:00
if stream.config.get("subtitle") and stream.config.get("get_url"):
2018-01-14 00:49:26 +01:00
if subs:
2018-05-20 21:09:49 +02:00
if stream.config.get("get_all_subtitles"):
2018-01-14 00:49:26 +01:00
for sub in subs:
print(sub.url)
else:
print(subs[0].url)
if stream.config.get("force_subtitle"):
2018-01-14 00:49:26 +01:00
return
def options_subs_dl(subfixes):
if subs:
2018-05-20 21:09:49 +02:00
if stream.config.get("get_all_subtitles"):
2018-01-14 00:49:26 +01:00
for sub in subs:
sub.download()
2018-05-20 21:09:49 +02:00
if stream.config.get("merge_subtitle"):
2018-01-14 00:49:26 +01:00
if sub.subfix:
subfixes += [sub.subfix]
else:
2018-05-20 21:09:49 +02:00
stream.config.set("get_all_subtitles", False)
2018-01-14 00:49:26 +01:00
else:
subs[0].download()
2018-05-20 21:09:49 +02:00
elif stream.config.get("merge_subtitle"):
stream.config.set("merge_subtitle", False)
2018-01-14 00:49:26 +01:00
2018-05-20 21:09:49 +02:00
if stream.config.get("subtitle") and not stream.config.get("get_url"):
2018-01-14 00:49:26 +01:00
options_subs_dl(subfixes)
2018-05-20 21:09:49 +02:00
if stream.config.get("force_subtitle"):
2018-01-14 00:49:26 +01:00
return
2018-05-20 21:09:49 +02:00
if stream.config.get("merge_subtitle") and not stream.config.get("subtitle"):
2018-01-14 00:49:26 +01:00
options_subs_dl(subfixes)
if not videos:
2018-06-24 16:14:57 +02:00
errormsg = None
2018-01-14 00:49:26 +01:00
for exc in error:
2018-06-24 16:14:57 +02:00
if errormsg:
errormsg = "{}. {}".format(errormsg, str(exc))
else:
errormsg = str(exc)
logging.error("No videos found. {}".format(errormsg))
2018-01-14 00:49:26 +01:00
else:
2018-05-20 21:09:49 +02:00
if stream.config.get("list_quality"):
2018-01-14 00:49:26 +01:00
list_quality(videos)
return
try:
2018-05-20 21:09:49 +02:00
stream = select_quality(stream.config, videos)
if stream.config.get("get_url"):
2018-01-14 00:49:26 +01:00
print(stream.url)
return
2018-06-24 16:14:57 +02:00
logging.info("Selected to download %s, bitrate: %s", stream.name, stream.bitrate)
2018-01-14 00:49:26 +01:00
stream.download()
except UIException as e:
2018-05-20 21:09:49 +02:00
if stream.config.get("verbose"):
2018-01-14 00:49:26 +01:00
raise e
log.error(e)
sys.exit(2)
2018-05-20 21:09:49 +02:00
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:
2018-01-14 00:49:26 +01:00
post.merge()
if stream.audio and not post.detect and stream.finished:
2018-06-24 16:14:57 +02:00
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"):
2018-01-14 00:49:26 +01:00
post.remux()
2018-05-20 21:09:49 +02:00
if stream.config.get("silent_semi") and stream.finished:
2018-06-24 16:14:57 +02:00
logging.log(25, "Download of %s was completed" % stream.options.output)