move to yt-dlp

This commit is contained in:
zuckerberg 2021-09-03 18:09:52 -04:00
parent d0a08a202b
commit 86f35f4996

View File

@ -1,5 +1,5 @@
# #
# Downloads the video/audio as a stream from a provided link using youtube-dl # Downloads the video/audio as a stream from a provided link using yt-dlp
# does not save the file, only the most recent fragment is held. Thus, this is # does not save the file, only the most recent fragment is held. Thus, this is
# ideal for devices with little memory # ideal for devices with little memory
# TODO gather video metadata before download # TODO gather video metadata before download
@ -16,24 +16,24 @@ from threading import Thread, main_thread
from time import sleep from time import sleep
from stream import StreamSource from stream import StreamSource
def updateYoutubeDL(): def updateYtdlp():
pip.main(['install', '--target=' + dirpath, '--upgrade', 'youtube_dl']) pip.main(['install', '--target=' + dirpath, '--upgrade', 'yt-dlp'])
def importYoutubeDL(): def importYoutubeDL():
return __import__('youtube_dl') return __import__('yt-dlp')
dirpath = tempfile.mkdtemp() dirpath = tempfile.mkdtemp()
sys.path.append(dirpath) sys.path.append(dirpath)
updateYoutubeDL() updateYtdlp()
class Downloader(Thread, StreamSource): class Downloader(Thread, StreamSource):
def __init__(self, url, cb): def __init__(self, url, cb):
Thread.__init__(self) Thread.__init__(self)
# update youtube-dl # update yt-dlp
# TODO: do this only every once in a while # TODO: do this only every once in a while
# updateYoutubeDL() # updateYtdlp()
self.cb = cb self.cb = cb
self.exit = False self.exit = False
@ -42,20 +42,23 @@ class Downloader(Thread, StreamSource):
env["PYTHONPATH"] = dirpath env["PYTHONPATH"] = dirpath
cmd = [ cmd = [
sys.executable, sys.executable,
dirpath + "/bin/youtube-dl", dirpath + "/bin/yt-dlp",
"-o", "-", "-o", "-", # output stream to stdout
"-f", "bestaudio/best", "-f", "bestaudio/best", # select for best audio
# "--audio-format", "mp3", "-x", # cannot do because it requires a tmp file to re-encode # "--audio-format", "mp3", "-x", # cannot do because it requires a tmp file to re-encode
"--prefer-ffmpeg", "--prefer-ffmpeg",
"--no-mark-watched", "--no-mark-watched",
"--geo-bypass", "--geo-bypass",
"--no-playlist", "--no-playlist",
"--retries", "100", "--retries", "100",
"--extractor-retries", "100",
"--throttled-rate", "100K", # get around youtube throttling; probably not needed anymore
"--no-call-home", "--no-call-home",
"--sponsorblock-remove", "sponsor,intro,selfpromo,interaction,preview,music_offtopic",
url url
] ]
self.popen = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid) self.popen = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
logger.add(self.popen.stderr, "youtube-dl.log") logger.add(self.popen.stderr, "yt-dlp.log")
self.start() self.start()
def isAlive(self): def isAlive(self):