Skip to content

Commit

Permalink
Update youtube-dl tracking and final file identification
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaker authored Aug 26, 2020
1 parent c189806 commit 3783904
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions youtubeDLService.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,56 @@ def __init__(self,globalStatusCallback=print()):

def downloadFunc():
while 1:
url,callback = self.downloadRequestQueue.get()
tempPathname='tempVideoFiles'
os.path.exists(tempPathname) or os.mkdir(tempPathname)
outfolder = os.path.join(tempPathname,'%(title)s.%(ext)s')
proc = sp.Popen(['youtube-dl',url,'-o',outfolder,'--merge-output-format','mp4'],stdout=sp.PIPE)
l = b''
self.globalStatusCallback('Downloading {}'.format(url),0)
finalName = b''
fileOutout=False
while 1:
c=proc.stdout.read(1)
l+=c
if len(c)==0:
print(c,l)
break
if c in (b'\n',b'\r'):
print(l)

if b'[download] Destination:' in l:
finalName = l.replace(b'[download] Destination: ',b'').strip()
print(finalName)
if b'[ffmpeg] Merging formats into' in l:
finalName = l.split(b'"')[-2].strip()
self.globalStatusCallback('Download complete {}'.format(finalName),1.0)
if not fileOutout:
callback(finalName)
fileOutout = True
print('Done',finalName)
if b'[download]' in l and b'%' in l:
pc = b'0'
for tc in l.split(b' '):
if b'%' in tc:
pc = tc.replace(b'%',b'')
desc = l.decode('utf8').replace('[download]','').strip()
self.globalStatusCallback('Downloading {} {}'.format(url,desc),float(pc)/100)

print(finalName,int(float(pc)) == 100)
if int(float(pc)) == 100 and len(finalName)>0:
try:
url,callback = self.downloadRequestQueue.get()
tempPathname='tempVideoFiles'
os.path.exists(tempPathname) or os.mkdir(tempPathname)
outfolder = os.path.join(tempPathname,'%(title)s.%(ext)s')
proc = sp.Popen(['youtube-dl',url,'-o',outfolder,'--merge-output-format','mp4'],stdout=sp.PIPE)
l = b''
self.globalStatusCallback('Downloading {}'.format(url),0)
finalName = b''
while 1:
c=proc.stdout.read(1)
l+=c
if len(c)==0:
print(c,l)
break
if c in (b'\n',b'\r'):
print(l)

if b'[download] Destination:' in l:
finalName = l.replace(b'[download] Destination: ',b'').strip()
print(finalName)
if b'[ffmpeg] Merging formats into' in l:
finalName = l.split(b'"')[-2].strip()
self.globalStatusCallback('Download complete {}'.format(finalName),1.0)
if not fileOutout:
callback(finalName)
fileOutout = True
print('Done',finalName)
if b'[download]' in l and b' has already been downloaded and merged' in l:
finalName = l.replace(b' has already been downloaded and merged',b'').replace(b'[download] ',b'').strip()
self.globalStatusCallback('Download already complete {}'.format(finalName),1.0)

if b'[download]' in l and b'%' in l:
pc = b'0'
for tc in l.split(b' '):
if b'%' in tc:
pc = tc.replace(b'%',b'')
desc = l.decode('utf8').replace('[download]','').strip()
self.globalStatusCallback('Downloading {} {}'.format(url,desc),float(pc)/100)

print(finalName,int(float(pc)) == 100)
if int(float(pc)) == 100 and len(finalName)>0:
self.globalStatusCallback('Download complete {}'.format(finalName),1.0)

l=b''
if len(finalName)>0:
callback(finalName)
else:
self.globalStatusCallback('Download failed {}'.format(url),1.0)
except Exception as e:
print(e)
self.globalStatusCallback('Download failed {}'.format(url),1.0)

l=b''


self.downloadWorkerThread = threading.Thread(target=downloadFunc,daemon=True)
Expand Down

0 comments on commit 3783904

Please sign in to comment.