Skip to content

Commit

Permalink
Merge pull request #55 from akamhy/speed-up
Browse files Browse the repository at this point in the history
Use yt-dlp as the default downloader to speed up the downloads and also increased code coverage to 99
  • Loading branch information
akamhy authored Nov 4, 2021
2 parents 8518d6f + b0cb916 commit 0bffaa6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
11 changes: 10 additions & 1 deletion tests/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
DownloadFailed,
YouTubeDLNotFound,
DownloadOutPutDirDoesNotExits,
DownloadSoftwareError,
)
from videohash.utils import (
create_and_return_temporary_directory,
Expand All @@ -17,6 +18,7 @@ def test_all():
_dir = os.path.join(
create_and_return_temporary_directory(), "/gduisgf73r7guai7td7g38yisif7si7/"
)

with pytest.raises(DownloadOutPutDirDoesNotExits):
Download(url="https://example.com", output_dir=_dir, youtube_dl_path=None)

Expand All @@ -40,6 +42,11 @@ def test_all():
url=url, output_dir=_dir, youtube_dl_path="/home/akamhy/ytdlfake"
) # non existant youtube_dl path

with pytest.raises(YouTubeDLNotFound):
Download(
url=url, output_dir=_dir, default_dl="unknown-dl"
) # non existant youtube_dl path

if which("yt-dlp"):
url = "https://www.youtube.com/watch?v=3NOmU06Vs6o" # video : A Step Toward Sustainable Lunar Exploration
_dir = create_and_return_temporary_directory()
Expand All @@ -52,7 +59,9 @@ def test_all():
if which("youtube-dl"):
url = "https://www.youtube.com/watch?v=4fdbfLJYYgI" # Video : Why Icy Moons are So Juicy
_dir = create_and_return_temporary_directory()
Download(url=url, output_dir=_dir, youtube_dl_path=str(which("youtube-dl")))
Download(
url=url, output_dir=_dir, youtube_dl_path=None, default_dl="youtube-dl"
)
file_list = get_list_of_all_files_in_dir(_dir)
total_files = len(file_list)
if total_files == 0:
Expand Down
61 changes: 31 additions & 30 deletions videohash/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from subprocess import check_output, Popen, PIPE

from .utils import does_path_exists
from .exceptions import YouTubeDLNotFound, DownloadOutPutDirDoesNotExits, DownloadFailed
from .exceptions import (
YouTubeDLNotFound,
DownloadOutPutDirDoesNotExits,
DownloadFailed,
DownloadSoftwareError,
)

from typing import Optional

Expand All @@ -31,6 +36,7 @@ def __init__(
output_dir: str,
youtube_dl_path: Optional[str] = None,
worst: bool = True,
default_dl: str = "yt-dlp",
) -> None:
"""
Check if output_dir exists and is a directory, must end with "/".
Expand All @@ -51,6 +57,9 @@ def __init__(
of the downloader. The downloaders are yt-dlp and youtube_dl.
Default worst is True
:param default_dl: The the default downloader, only used if youtube_dl_path
is not provided.
:return: None
:rtype: NoneType
Expand All @@ -61,6 +70,8 @@ def __init__(
if youtube_dl_path:
self.youtube_dl_path = youtube_dl_path

self.default_dl = default_dl.strip().lower().replace("_", "-")

self.worst = worst

if not does_path_exists(self.output_dir):
Expand All @@ -80,41 +91,31 @@ def _check_youtube_dl(self) -> None:
:rtype: NoneType
"""
if not self.youtube_dl_path:

if not which("yt-dlp"):

if not which("youtube-dl"):
def find_dl(dl_name):
if not which(dl_name):
raise YouTubeDLNotFound(f"{dl_name} not found on your system path.")
self.youtube_dl_path = str(which(dl_name))

raise YouTubeDLNotFound(
"youtube-dl and yt-dlp not found on path."
+ " Install one of the two and add them to the path."
+ " Or you can also pass the path to 'youtube_dl_path' param"
)

else:
self.youtube_dl_path = str(which("youtube-dl"))

else:
self.youtube_dl_path = str(which("yt-dlp"))
if not self.youtube_dl_path:
find_dl(self.default_dl)

try:

output = check_output([str(self.youtube_dl_path), "--version"]).decode()

if not re.search(
r"[0-9]{4}\.[0-9]{2}\.[0-9]{2}", output
): # raise Exception is youtube is not installed.
# check by youtube-dl --version

raise FileNotFoundError(
f"The output is not matching the expected '{self.youtube_dl_path} --version' output."
)

except FileNotFoundError:

raise YouTubeDLNotFound(
f"Youtube-dl/yt-dlp is not found at '{self.youtube_dl_path}'."
f"Video downloader can not be found at '{self.youtube_dl_path}'."
)

if not re.search(
r"[0-9]{4}\.[0-9]{2}\.[0-9]{2}", output
): # raise Exception is youtube is not installed.
# check by youtube-dl --version

raise DownloadSoftwareError(
f"The output is not matching the expected '{self.youtube_dl_path} --version' output."
+ " Please check your youtube_dl/yt-dlp installation. If the problem persist after "
+ "re-install open an issue at https://github.com/akamhy/videohash/issues"
)

def download_video(self) -> None:
Expand Down Expand Up @@ -157,6 +158,6 @@ def download_video(self) -> None:

if len(os.listdir(self.output_dir)) == 0:
raise DownloadFailed(
f"{self.youtube_dl_path} failed to download the video at"
f"'{self.youtube_dl_path}' failed to download the video at"
+ f" '{self.url}'.\n{youtube_dl_output}\n{youtube_dl_error}"
)
7 changes: 4 additions & 3 deletions videohash/framesextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .utils import does_path_exists
from .exceptions import (
FramesExtractorOutPutDirDoesNotExits,
FFmpegError,
FFmpegNotFound,
FFmpegFailedToExtractFrames,
)
Expand Down Expand Up @@ -89,8 +90,8 @@ def _check_ffmpeg(self) -> None:
if not which("ffmpeg"):

raise FFmpegNotFound(
"FFmpeg is not on the path. Install FFmpeg and add it to the path."
+ "Or you can also pass the path via the 'ffmpeg_path' param."
"FFmpeg is not on the system path. Install FFmpeg and add it to the path."
+ "Or you can also pass the path via the 'ffmpeg_path' parameter."
)
else:

Expand All @@ -107,7 +108,7 @@ def _check_ffmpeg(self) -> None:
else:

if "ffmpeg version" not in output:
raise FFmpegNotFound(
raise FFmpegError(
f"ffmpeg at '{self.ffmpeg_path}' is not really ffmpeg. Output of ffmpeg -version is \n'{output}'."
)

Expand Down

0 comments on commit 0bffaa6

Please sign in to comment.