from PyQt5 import QtWidgets, QtCore
from pytube import YouTube
from bs4 import BeautifulSoup
import requests
import sys
import pandas as pd
class YouTubeDownloader(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# Layout setup
self.url_label = QtWidgets.QLabel('YouTube URL:')
self.url_input = QtWidgets.QLineEdit(self)
self.fetch_btn = QtWidgets.QPushButton('Fetch Video Info', self)
self.video_info = QtWidgets.QTextBrowser(self)
self.download_btn = QtWidgets.QPushButton('Download Video', self)
# Format and Resolution Selection
self.format_label = QtWidgets.QLabel('Select Format:')
self.format_combo = QtWidgets.QComboBox(self)
self.format_combo.addItems(['mp4', 'webm'])
self.resolution_label = QtWidgets.QLabel('Select Resolution:')
self.resolution_combo = QtWidgets.QComboBox(self)
# Layout positioning
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.url_label)
layout.addWidget(self.url_input)
layout.addWidget(self.fetch_btn)
layout.addWidget(self.video_info)
layout.addWidget(self.format_label)
layout.addWidget(self.format_combo)
layout.addWidget(self.resolution_label)
layout.addWidget(self.resolution_combo)
layout.addWidget(self.download_btn)
self.setLayout(layout)
# Connect buttons to functions
self.fetch_btn.clicked.connect(self.fetch_video_info)
self.download_btn.clicked.connect(self.download_video)
self.setWindowTitle('YouTube Video Downloader')
self.show()
def fetch_video_info(self):
url = self.url_input.text()
if not url:
self.video_info.setText("Please enter a valid YouTube URL.")
return
try:
# Fetch video data
yt = YouTube(url)
metadata = {
"Title": yt.title,
"Description": yt.description,
"Views": yt.views,
"Length (seconds)": yt.length,
"Author": yt.author,
"Publish Date": yt.publish_date.strftime("%Y-%m-%d")
}
metadata_df = pd.DataFrame(metadata, index=[0])
# Display video metadata
self.video_info.setText(metadata_df.to_string(index=False))
# Populate resolution combo box
self.resolution_combo.clear()
streams = yt.streams.filter(progressive=True, file_extension=self.format_combo.currentText()).all()
resolutions = sorted(set([stream.resolution for stream in streams if stream.resolution]))
self.resolution_combo.addItems(resolutions)
except Exception as e:
self.video_info.setText(f"Error fetching video info: {str(e)}")
def download_video(self):
url = self.url_input.text()
if not url:
self.video_info.setText("Please enter a valid YouTube URL.")
return
try:
# Fetch video data
yt = YouTube(url)
selected_format = self.format_combo.currentText()
selected_resolution = self.resolution_combo.currentText()
# Filter and download video
video_stream = yt.streams.filter(progressive=True, file_extension=selected_format, resolution=selected_resolution).first()
if video_stream:
self.video_info.setText(f"Downloading '{yt.title}' in {selected_resolution}...")
video_stream.download()
self.video_info.setText("Download complete!")
else:
self.video_info.setText("Selected resolution not available.")
except Exception as e:
self.video_info.setText(f"Error downloading video: {str(e)}")
def main():
app = QtWidgets.QApplication(sys.argv)
downloader = YouTubeDownloader()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Comments
Post a Comment