Skip to content

Commit

Permalink
move pytube to ytdlp
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Jul 6, 2023
1 parent 4fba49d commit 679ae23
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
32 changes: 19 additions & 13 deletions demo/youtube/octotube.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import time
from argparse import ArgumentParser
from threading import Thread
from typing import *

import pvoctopus
from pytube import YouTube
from yt_dlp import YoutubeDL


class ProgressAnimation(Thread):
Expand Down Expand Up @@ -48,17 +49,21 @@ def stop(self):
self._done = True


def download(url: str, folder: str) -> str:
webm_path = os.path.join(folder, '%s.webm' % url.split("watch?v=")[1])
if not os.path.exists(webm_path):
anime = ProgressAnimation('Downloading %s' % url)
anime.start()
youtube = YouTube(url)
audio_stream = youtube.streams.filter(only_audio=True, audio_codec='opus').order_by('bitrate').last()
audio_stream.download(output_path=folder, filename=os.path.basename(webm_path), skip_existing=True)
anime.stop()

return webm_path
def download_ytdlp(url: str, output_dir: str, options: Optional[Dict[str, Any]] = None) -> List[str]:
ydl_opts = {
'outtmpl': "%(id)s.%(ext)s",
'format': 'bestaudio',
'paths': {
'home': output_dir
},
'geo_bypass': True,
}
if options is not None:
ydl_opts.update(**options)
with YoutubeDL(ydl_opts) as ydl:
info = ydl.sanitize_info(ydl.extract_info(url, download=False))
ydl.download([url])
return os.path.join(output_dir, f"{info['id']}.webm"), info['duration']


def main():
Expand All @@ -70,7 +75,8 @@ def main():
parser.add_argument('--work-folder', default=os.path.expanduser('~/'))
args = parser.parse_args()

webm_path = download(url=args.url, folder=args.work_folder)
webm_path = download_ytdlp(url=args.url, output_dir=args.work_folder)[0]
print(webm_path)

o = pvoctopus.create(access_key=args.access_key)

Expand Down
2 changes: 1 addition & 1 deletion demo/youtube/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pvoctopus
pytube
yt-dlp
1 change: 0 additions & 1 deletion res/spell-check/dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ porcospino
pvbase
pvoctopus
pvoctopusdemo
pytube
recyclerview
reindex
wargv
Expand Down

0 comments on commit 679ae23

Please sign in to comment.