Skip to content

Instantly share code, notes, and snippets.

@wilfreddv
Created February 4, 2019 11:15
Show Gist options
  • Save wilfreddv/7de6bde2881046c100e0d1e3b701ed11 to your computer and use it in GitHub Desktop.
Save wilfreddv/7de6bde2881046c100e0d1e3b701ed11 to your computer and use it in GitHub Desktop.
GUI wrapper for pytube to download YouTube video's
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'home.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(352, 111)
self.downloadAudio = QtWidgets.QPushButton(Dialog)
self.downloadAudio.setGeometry(QtCore.QRect(0, 50, 351, 27))
self.downloadAudio.setObjectName("downloadAudio")
self.downloadVideo = QtWidgets.QPushButton(Dialog)
self.downloadVideo.setGeometry(QtCore.QRect(0, 80, 351, 27))
self.downloadVideo.setObjectName("downloadVideo")
self.linkInput = QtWidgets.QLineEdit(Dialog)
self.linkInput.setGeometry(QtCore.QRect(0, 0, 351, 27))
self.linkInput.setText("")
self.linkInput.setObjectName("linkInput")
self.linkOkLabel = QtWidgets.QLabel(Dialog)
self.linkOkLabel.setGeometry(QtCore.QRect(0, 30, 691, 17))
self.linkOkLabel.setText("")
self.linkOkLabel.setObjectName("linkOkLabel")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.downloadAudio.setText(_translate("Dialog", "Download audio"))
self.downloadVideo.setText(_translate("Dialog", "Download video"))
self.linkInput.setPlaceholderText(_translate("Dialog", "Enter URL here..."))
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>352</width>
<height>111</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QPushButton" name="downloadAudio">
<property name="geometry">
<rect>
<x>0</x>
<y>50</y>
<width>351</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Download audio</string>
</property>
</widget>
<widget class="QPushButton" name="downloadVideo">
<property name="geometry">
<rect>
<x>0</x>
<y>80</y>
<width>351</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Download video</string>
</property>
</widget>
<widget class="QLineEdit" name="linkInput">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>351</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>Enter URL here...</string>
</property>
</widget>
<widget class="QLabel" name="linkOkLabel">
<property name="geometry">
<rect>
<x>0</x>
<y>30</y>
<width>691</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
blessings (1.6.1)
docopt (0.6.2)
pip (8.1.1)
pkg-resources (0.0.0)
PyQt5 (5.10.1)
pyqt5-installer (5.7.0)
pytube (9.2.2)
setuptools (20.7.0)
sip (4.19.8)
wget (3.2)
import sys
import re
from pytube import YouTube
from urllib.error import HTTPError
from urllib.error import URLError
class youtubeDownloader:
def __init__(self):
pass
def _downloadAudio(self):
link = input("Enter youtube link")
if not self._isOkLink(link):
print('Please make sure the URL is valid')
else:
self._download(link, only_audio=True)
def _downloadVideo(self):
link = input("Enter youtube link")
if not self._isOkLink(link):
print('Please make sure the URL is valid')
else:
self._download(link)
def _download(self, link, only_audio=False):
print('Preparing to download...')
try:
yt = YouTube(link)
if only_audio:
yt.streams.filter(only_audio=True).first().download()
else:
yt.streams.filter(progressive=True, type='video')\
.order_by('resolution')\
.desc()\
.first()\
.download()
print('Finished downloading')
except (HTTPError, URLError):
print('Bad internet connection, try again...')
def _isOkLink(self, link):
# Add your country's domain name to the regex
return re.match(r'^https:\/\/www\.youtube\.(com|nl|be|de)\/watch\?v=[a-zA-Z_0-9-]{6,}$', link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment