Skip to content

Commit

Permalink
develop #comment Move metadata fetching inside of the download process.
Browse files Browse the repository at this point in the history
 - This has a main consequence to use the ressource of the downloading thread instead of using the one of the main thread (which is a good thing).
  • Loading branch information
Luc Sinet committed Jan 11, 2019
1 parent 000417f commit f3d4a62
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions RaspberryCast/video_downloader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import time
from collections import deque
from threading import (
Condition,
Expand Down Expand Up @@ -32,8 +31,21 @@ def __del__(self):
self._thread.join()

def queue(self, videos, dl_callback, first=False):
Thread(target=self._queue_downloads,
args=(videos, dl_callback, first,)).start()
with self._cv:
for video in videos:
# Position the video with the videos of the same playlist.
index = 0 if first else len(self._queue)
if first and video.playlist_id is not None:
for i, v in enumerate(reversed(self._queue)):
if v[0].playlist_id == video.playlist_id:
index = len(self._queue) - i
break
logger.info("[downloader] queue video {}".format(video))
self._queue.insert(index, (video, dl_callback))
self._cv.notify()

def list(self):
return list(self._queue)

def extract_playlist(self, url):
ydl_opts = {
Expand All @@ -50,41 +62,6 @@ def extract_playlist(self, url):
for entry in data['entries']]
return urls

def list_queue(self):
return list(self._queue)

def _fetch_metadata(self, video):
logger.debug("[downloader] fetching metadata")
ydl = youtube_dl.YoutubeDL(
{
'noplaylist': True,
'debug_printtraffic': self._log_debug,
'logger': logger
})
with ydl: # Download the video data without downloading it.
data = ydl.extract_info(video.url, download=False)
if data is not None:
video.title = data['title']
return True
return False

def _queue_downloads(self, videos, dl_callback, first):
for video in videos:
with self._cv:
if not self._fetch_metadata(video):
continue
# Position the video with the videos of the same playlist.
index = 0 if first else len(self._queue)
if first and video.playlist_id is not None:
for i, v in enumerate(reversed(self._queue)):
if v[0].playlist_id == video.playlist_id:
index = len(self._queue) - i
break
logger.info("[downloader] queue video {}".format(video))
self._queue.insert(index, (video, dl_callback))
self._cv.notify()
time.sleep(0.1)

def _download_queued_videos(self):
while not self._stopped:
video, dl_callback = (None, None)
Expand All @@ -97,6 +74,9 @@ def _download_queued_videos(self):
self._download(video, dl_callback)

def _download(self, video, dl_callback):
if not self._fetch_metadata(video):
return

video.path = config.output_directory + '/' + video.title + '.mp4'

def download_hook(d):
Expand All @@ -118,6 +98,22 @@ def download_hook(d):
logger.debug("[downloader] video downloaded: {}".format(video))
dl_callback(video)

def _fetch_metadata(self, video):
logger.debug("[downloader] fetching metadata")
ydl = youtube_dl.YoutubeDL(
{
'noplaylist': True,
'debug_printtraffic': self._log_debug,
'logger': logger
})
with ydl: # Download the video data without downloading it.
data = ydl.extract_info(video.url, download=False)
if data is None:
return False

video.title = data['title']
return True


def make_video_downloader():
downloader = VideoDownloader()
Expand Down

0 comments on commit f3d4a62

Please sign in to comment.