-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from kadirnar/delete-pytube-library
Add yt-dlp instead of pytube to download youtube video
- Loading branch information
Showing
6 changed files
with
117 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
torch>=2.0.0 | ||
torchvision>=0.15.0 | ||
moviepy>=1.0.3 | ||
pytube>=15.0.0 | ||
yt_dlp | ||
Requests>=2.31.0 | ||
accelerate | ||
bitsandbytes | ||
hqq | ||
ffmpeg | ||
ffmpeg-python | ||
pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,91 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from moviepy.editor import AudioFileClip | ||
from pytube import YouTube | ||
import yt_dlp | ||
|
||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") | ||
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s') | ||
|
||
|
||
def download_and_convert_to_mp3(url: str, | ||
output_path: str = "output", | ||
filename: str = "test") -> Optional[str]: | ||
def download_youtube_to_mp3(url, output_dir='./', filename="test"): | ||
""" | ||
Downloads a YouTube video as an MP3 file. | ||
Parameters: | ||
url (str): The URL of the YouTube video to download. | ||
output_dir (str, optional): The directory to save the MP3 file. Defaults to the current working directory. | ||
filename (str, optional): The filename for the MP3 file. If not provided, the video title will be used. | ||
Returns: | ||
pathlib.Path: The path to the downloaded MP3 file. | ||
""" | ||
# Create the output directory if it doesn't exist | ||
output_dir = Path(output_dir) | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
# Set the output filename | ||
if filename is None: | ||
filename = "%(title)s.%(ext)s" | ||
else: | ||
filename = f"{filename}.%(ext)s" | ||
|
||
# Download the video using yt_dlp | ||
ydl_opts = { | ||
'outtmpl': str(output_dir / filename), | ||
'format': 'bestaudio/best', | ||
'postprocessors': [{ | ||
'key': 'FFmpegExtractAudio', | ||
'preferredcodec': 'mp3', | ||
'preferredquality': '192', | ||
}], | ||
} | ||
|
||
try: | ||
yt = YouTube(url) | ||
audio_stream = yt.streams.filter(only_audio=True).first() | ||
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
ydl.download([url]) | ||
except yt_dlp.utils.DownloadError as e: | ||
logging.error(f"Error downloading video: {e}") | ||
return None | ||
|
||
if audio_stream is None: | ||
logging.warning("No audio streams found") | ||
return None | ||
output_path = output_dir / filename.replace('%(ext)s', 'mp3') | ||
logging.info(f"Downloaded {output_path} as MP3") | ||
return str(output_path) | ||
|
||
Path(output_path).mkdir(parents=True, exist_ok=True) | ||
|
||
mp3_file_path = os.path.join(output_path, filename + ".mp3") | ||
logging.info(f"Downloading started... {mp3_file_path}") | ||
def download_youtube_to_mp4(url, output_dir='./', filename="test"): | ||
""" | ||
Downloads a YouTube video as an MP4 file. | ||
downloaded_file_path = audio_stream.download(output_path) | ||
Parameters: | ||
url (str): The URL of the YouTube video to download. | ||
output_dir (str, optional): The directory to save the MP4 file. Defaults to the current working directory. | ||
filename (str, optional): The filename for the MP4 file. If not provided, the video title will be used. | ||
audio_clip = AudioFileClip(downloaded_file_path) | ||
audio_clip.write_audiofile(mp3_file_path, codec="libmp3lame", verbose=False, logger=None) | ||
audio_clip.close() | ||
Returns: | ||
str: The path to the downloaded MP4 file. | ||
""" | ||
# Create the output directory if it doesn't exist | ||
output_dir = Path(output_dir) | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
if Path(downloaded_file_path).suffix != ".mp3": | ||
os.remove(downloaded_file_path) | ||
# Set the output filename | ||
if filename is None: | ||
filename = "%(title)s.%(ext)s" | ||
else: | ||
filename = f"{filename}.%(ext)s" | ||
|
||
logging.info(f"Download and conversion successful. File saved at: {mp3_file_path}") | ||
return str(mp3_file_path) | ||
# Download the video using yt_dlp | ||
ydl_opts = { | ||
'outtmpl': str(output_dir / filename), | ||
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', | ||
} | ||
|
||
except Exception as e: | ||
logging.error(f"An error occurred: {e}") | ||
try: | ||
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
ydl.download([url]) | ||
except yt_dlp.utils.DownloadError as e: | ||
logging.error(f"Error downloading video: {e}") | ||
return None | ||
|
||
output_path = output_dir / filename.replace('%(ext)s', 'mp4') | ||
logging.info(f"Downloaded {output_path} as MP4") | ||
return str(output_path) |