dddd
import logging
import youtube_dl
# Configure logger
logging.basicConfig(filename='youtube_downloader.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
class YouTubeDownloader:
def __init__(self):
self.ydl_opts = {
'format': 'best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
def download_video(self, video_url, format='mp4'):
try:
logging.info(f"Downloading video: {video_url}")
ydl_opts = {} if format == 'mp4' else self.ydl_opts
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video_url, download=True)
logging.info(f"Downloaded video: {info_dict['title']} - Format: {format}")
except Exception as e:
logging.error(f"Error downloading video: {video_url} - {e}")
# Example usage
if __name__ == "__main__":
downloader = YouTubeDownloader()
video_url = input("Enter YouTube video URL: ")
format_choice = input("Enter 'mp4' or 'mp3' to choose format: ")
if format_choice == 'mp4':
downloader.download_video(video_url, format='mp4')
elif format_choice == 'mp3':
downloader.download_video(video_url, format='mp3')
else:
print("Invalid format choice. Please enter 'mp4' or 'mp3'.")
0 Commentaires