# Time: 2020/06/02
#Author: Xiaohong
# 运行环境: OS: Windows 10
# Python: 3.7
# 功能: 通过you-get方式下载视频
主界面:
主程序(Tl_A02.py):需先下载 you-get (pip3 install you-get)
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDockWidget, QListWidget
from PyQt5.QtGui import *
from Ui_qt_A02 import Ui_Frm_youget_video
# 自己建一个mywindows类,QtWidgets.QMainWindow:继承该类方法
class c_tl_a02(QtWidgets.QMainWindow):
# __init__:析构函数,也就是类被创建后就会预先加载的项目。
# 马上运行,这个方法可以用来对你的对象做一些你希望的初始化。
def __init__(self, parent=None):
# 这里需要重载一下mywindow,同时也包含了QtWidgets.QMainWindow的预加载项。
super(c_tl_a02, self).__init__(parent)
self.child=Ui_Frm_youget_video()
self.child.setupUi(self)
self.child.btn_select.clicked.connect(self.sel_directory)
self.child.btn_download.clicked.connect(self.youget_dl_video01)
self.child.btn_open_path.clicked.connect(self.open_path)
self.child.t_directory.setText('e://')
def youget_dl_video01(self):
pass
t_website = self.child.t_youget_website.text()
t_directory = self.child.t_directory.text()
# print(t_keyword)
# print(t_directory)
# print(t_pic_count)
if t_website == '' or t_directory == '' :
reply = QMessageBox.information(
self, '提醒', '参数为空,请检查并补录完整', QMessageBox.Yes, QMessageBox.Yes)
else:
reply = QMessageBox.question(
self, '确认', '是否要用 you-get 下载视频,这将花费较长时间', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
print('下载')
command='you-get -o '+t_directory + ' '+ t_website
print(command)
os.system(command)
reply = QMessageBox.information(
self, '提醒', '下载完成', QMessageBox.Yes, QMessageBox.Yes)
else:
print('不下载')
def sel_directory(self):
pass
# 打开文件有以下3种:
# 1、单个文件打开 QFileDialog.getOpenFileName()
# 2、多个文件打开 QFileDialog.getOpenFileNames()
# 3、打开文件夹 QFileDialog.getExistingDirectory()
dir_path = QFileDialog.getExistingDirectory(self, "请选择文件夹路径", "/")
print(dir_path)
self.child.t_directory.setText(dir_path)
# print(dir_path)
def open_path(self):
dir_path=self.child.t_directory.text()
if os.path.exists(dir_path):
os.startfile(dir_path)
# 打开文件有以下3种:
# 1、单个文件打开 QFileDialog.getOpenFileName()
# 2、多个文件打开 QFileDialog.getOpenFileNames()
# 3、打开文件夹 QFileDialog.getExistingDirectory()
if __name__ == '__main__': # 如果整个程序是主程序
# QApplication相当于main函数,也就是整个程序(很多文件)的主入口函数。
# 对于GUI程序必须至少有一个这样的实例来让程序运行。
app = QtWidgets.QApplication(sys.argv)
# 生成 mywindow 类的实例。
window =c_tl_a02()
# 有了实例,就得让它显示,show()是QWidget的方法,用于显示窗口。
window.show()
# 调用sys库的exit退出方法,条件是app.exec_(),也就是整个窗口关闭。
# 有时候退出程序后,sys.exit(app.exec_())会报错,改用app.exec_()就没事
# https://stackoverflow.com/questions/25719524/difference-between-sys-exitapp-exec-and-app-exec
sys.exit(app.exec_())
QT5 界面(qt_a02.ui):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Frm_youget_video</class>
<widget class="QWidget" name="Frm_youget_video">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>655</width>
<height>388</height>
</rect>
</property>
<property name="windowTitle">
<string>视频下载(You-get 方式)</string>
</property>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>551</width>
<height>131</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>视频地址</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>文件夹路径</string>
</property>
</widget>
<widget class="QLineEdit" name="t_directory">
<property name="geometry">
<rect>
<x>110</x>
<y>60</y>
<width>371</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="t_youget_website">
<property name="geometry">
<rect>
<x>110</x>
<y>20</y>
<width>371</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="btn_select">
<property name="geometry">
<rect>
<x>480</x>
<y>60</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
<widget class="QPushButton" name="btn_download">
<property name="geometry">
<rect>
<x>110</x>
<y>94</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>下载</string>
</property>
</widget>
<widget class="QPushButton" name="btn_Close">
<property name="geometry">
<rect>
<x>290</x>
<y>94</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
<widget class="QPushButton" name="btn_open_path">
<property name="geometry">
<rect>
<x>190</x>
<y>94</y>
<width>91</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>打开下载目录</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>btn_Close</sender>
<signal>clicked()</signal>
<receiver>Frm_youget_video</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>382</x>
<y>115</y>
</hint>
<hint type="destinationlabel">
<x>489</x>
<y>216</y>
</hint>
</hints>
</connection>
<connection>
<sender>btn_select</sender>
<signal>clicked()</signal>
<receiver>Frm_youget_video</receiver>
<slot>sel_directory()</slot>
<hints>
<hint type="sourcelabel">
<x>520</x>
<y>83</y>
</hint>
<hint type="destinationlabel">
<x>514</x>
<y>216</y>
</hint>
</hints>
</connection>
<connection>
<sender>btn_download</sender>
<signal>clicked()</signal>
<receiver>Frm_youget_video</receiver>
<slot>baidu_dl_pic01()</slot>
<hints>
<hint type="sourcelabel">
<x>296</x>
<y>122</y>
</hint>
<hint type="destinationlabel">
<x>298</x>
<y>236</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>sel_directory()</slot>
<slot>baidu_dl_pic01()</slot>
</slots>
</ui>