Skip to content

Instantly share code, notes, and snippets.

@piyh
Last active March 20, 2021 00:47
Show Gist options
  • Save piyh/c7bafb48a17f4b58ceb327000f934864 to your computer and use it in GitHub Desktop.
Save piyh/c7bafb48a17f4b58ceb327000f934864 to your computer and use it in GitHub Desktop.
#20210319 - youtube-dl -i <url> to ignore errors is the 100% easier approach over using this script.
#In future, import youtube-dl module and use documentation below to pass parameters
#https://github.com/ytdl-org/youtube-dl/blob/3e4cedf9e8cd3157df2457df7274d0c842421945/youtube_dl/YoutubeDL.py#L137-L312
import re
import os
import shlex, subprocess
from time import sleep
import sys
os.chdir('D:/kingcobrajfs')
global cachedProgress
def finished(process):
try:
if process.returncode != 0:
return False
else:
return True
except (AttributeError, NameError) as e:
return False
def main():
youtube_dl = ''
#fails on Vid 549, 357, 607,681, 789,841, 894, 914, 931, 936, 956, 1073,1096, 1212, 1319
startVid = 1
dlQueueLen = -1 #initalizing values with -1, should never be doing useful logic with this value
curVid = -1
sleepInterval = 3
erroredVids = []
while not finished(youtube_dl):
curVid = 1
youtubedlCmd = f'youtube-dl --newline --write-auto-sub -v --playlist-start {startVid} https://www.youtube.com/user/KingCobraJFS'
youtubedlCmd = shlex.split(youtubedlCmd)
youtube_dl = subprocess.Popen(youtubedlCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = youtube_dl.communicate()
out = out.decode(encoding='cp1252').split('\n') #encoding='cp1252' if on windows
err = err.decode(encoding='cp1252').split('\n') #taking stdout/stderr from bytes to list of strings
for line in out: #read stdout
if '[download] 100%' in line:
print(line)
matchObj = re.match(r'\[youtube\] \w{11}:', line)
if matchObj:
print(line)
matchObj = re.match(r'\[download\] Downloading video (\d+) of (\d+)', line) #look to see which video we're on
if matchObj:
curVid = int(matchObj.group(1)) #get current vid
dlQueueLen = int(matchObj.group(2)) #get total vids undownloaded in playlist
for line in err: #read stderr
print(line)
print(f'youtube_dl.returncode:{youtube_dl.returncode}')
startVid = startVid + curVid
#if youtube_dl.returncode !=0:
erroredVids.append(startVid-1)
global cachedProgress
cachedProgress = f'startVid:{startVid}, curVid:{curVid}, dlQueueLen:{dlQueueLen}, totalVids:{dlQueueLen + startVid}, erroredVids:{erroredVids}'
print(cachedProgress)
print(f'restarting youtube-dl at vid #{startVid}')
#sleepInterval = sleepInterval + 30
print(f'sleeping for {sleepInterval} seconds')
sleep(sleepInterval)
print ("Exiting Main")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print(cachedProgress)
try:
sys.exit(128) # not sure if 128 does anything, too scared to change
except SystemExit:
os._exit(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment